I am making a user input driven deployment script. Basically I am asking a user "How many VM's would you like to deploy" and depending on the answer I want to create and use so many variables.
So my code currently asks the user for number of VM's Then it loops to create a new variable and ask user to fill that variable however I don't know how to use the variable that is created.
$Vmnumber = Read-Host "Enter number iof VM's that you would like to create."
for($i = 1; $i -le $Vmnumber; $i++){
New-Variable -Name "vmip$i"
Write-Host "Please Enter IP Address for VM$i (Site A): " -ForegroundColor Yellow -NoNewline
do {
if(($vmip + $i = Read-Host "Press enter to accept default IP form PHPIPAM" $SiteAsorting[$i]"or type a differnet one.") -eq ''){$vmip + $i = $SiteAsorting[$1]}else{$vmip + $i}
$ok = $vmip + $1 -match $IPPattern
if ($ok -eq $false) {
Write-Warning ("'{0}' is not an IP address." -f $vmip + $i)
write-host -fore Yellow "Please Enter IP for VM$i : " -NoNewline
}
} until ( $ok )
}
'''
I would like the the code to create new variable and ask for user input every iteration.
Thanks in advance for any help
Read-Hostbecause it can't be automated. I would instead recommend to use command-line parameters (param()statement). Your script will be much more flexible, and you can have input validation more or less for free.$vmip), and then add the final value to an array, list or hashtableMathias R. Jessensaid about dynamically named $Vars ... don't do that. [grin] seriously, it is a really bad idea when there are structures already available to do what you want ... arrays, generic.lists, and lots of other collection types.