0

I have a PowerShell script I am using to automate creating volumes on a SAN. The script prompts the user to enter the pertinent information: number of volumes, volume size and naming pattern. Next it should run a command on the array to create the volumes using the information provided. Here is where I am stuck. Right now the script asks the user to specify the number of volumes to create. For example let's say the user enters the number 4. I a stuck on how to take 4 and make it an array 1,2,3,4 or 7 to 1,2,3,4,5,6,7. Maybe there is an easier way to do this via counting or some other function. I am new to scripting, so any help is greatly appreciated.

# Define the number of volumes to create
$num_vols = read-host "Please enter the number of volumes"

# Define naming pattern
$vol_name = read-host "Please enter the volume naming pattern"

What I wanted to do was create a function that would basically run something like:

# Run command to create volumes on array
foreach ($i in $num_vols){
    & "command to execute" $vol_name + $i
}

Thanks for the suggestions. I am now using:

# Run command to create volumes on array
foreach ($i in 1..$num_vols){
    & "command to execute" $vol_name"."$i
}

Works perfectly! I can now create X volumes named VolName.1, VolName.2 and so on, where X is the number of volumes entered by the user.

2
  • 2
    1..$num_vols | % { .... } Commented Oct 7, 2015 at 17:15
  • Based on your question I am not entirely sure what you are asking. Do you want to simply execute the command some number of times or do you actually need a number of array elements ? Commented Oct 7, 2015 at 17:51

1 Answer 1

2

You can use the range operator (..):

foreach ($i in 1..$num_vols) { ... }

Or the range operator like this:

1..$num_vols | ForEach-Object { ... }

Or you can use a traditional for loop:

for ($i = 0; $i -lt $num_vols; $i++) { ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.