PowerShell variable in a variable

I am trying to add the variable $numberOfNics , which adds nics based on the number. This is my code right now, and I'm a little confused about how to put it in a loop. I know that an array is probably suitable, but not quite sure how to make an array in this sense, which simply calls variables instead of them.

old code:

 $numberOfNics=1 $networkname1 = "ondemandport1" $networkname2 = "ondemandport2" $networkname3 = "ondemandport3" $networkname4 = "ondemandport4" $networkname5 = "ondemandport5" $networkname6 = "ondemandport6" $networkname7 = "ondemandport7" $networkname8 = "ondemandport8" $networkname9 = "ondemandport9" $networkInterface1 = New-AzureRmNetworkInterface -Name $networkname1 $networkInterface2 = New-AzureRmNetworkInterface -Name $networkname2 $networkInterface3 = New-AzureRmNetworkInterface -Name $networkname3 $networkInterface4 = New-AzureRmNetworkInterface -Name $networkname4 $networkInterface5 = New-AzureRmNetworkInterface -Name $networkname5 

To remove this, I would like to make a loop like this

 for ($i = 0; $i -lt $numberOfNics; $i++) { $networkInterface$i = New-AzureRmNetworkInterface -Name $networkname$i } 

EDIT: Thanks to Martin, but now it's hard for me to call a variable.

 $networkInterfaces = 1 .. $numberOfNics | ForEach-Object { if ($a -eq 0){ New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Name "ondemandport$_" -Location $locationName -SubnetId $virtualNetwork.Subnets[$a].Id -PublicIpAddressId $publicIp.Id -force } ElseIf ($a=1){ New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Name "ondemandport$_" -Location $locationName -SubnetId $virtualNetwork.Subnets[$a].Id -force } Else{ New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Name "ondemandport$_" -Location $locationName -SubnetId $virtualNetwork.Subnets[2].Id -force } } 

# also trying to do this in an array loop

 $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[1].Id -Primary $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[2].Id $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[3].Id $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[4].Id $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[5].Id $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[6].Id $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[7].Id $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[8].Id $vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterfaces[9].Id 

Error message: Add-AzureRmVMNetworkInterface: Unable to check argument in parameter 'Id'. The argument is empty or empty. Specify an argument that is not null or empty, and then try the command again.

+5
source share
3 answers

You can also do this with a single line:

 $networkInterfaces = 1 .. $numberOfNics | ForEach-Object { New-AzureRmNetworkInterface -Name "ondemandport$_" } 

Now you can access the interfaces by index, for example:

 $networkInterfaces[2] 

By the way, consider using ARM patterns.


Edit:

 $networkInterfaces = 1 .. $numberOfNics | ForEach-Object { $isPrimary = $_ -eq 0 # set the first one as primary $nic = Get-AzureRmNetworkInterface -Name "ondemandport$_" -ResourceGroupName $resourceGroupName Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id -Primary:$isPrimary } 
+3
source

You should do something like this:

 $numberOfNics= 10 $networkname = "ondemandport" $networkInterface = @() for ($i = 0; $i -le $numberOfNics; $i++) { $networkInterface += ,@( New-AzureRmNetworkInterface -Name ($networkname + $i )) } 

Not sure if it will work with New-AzureRmNetworkInterface, but works with other New teams

0
source

You can use the PowerShell cmdlet Get-Variable . One thing I would like to point out is that the for loop that you are using will not be limited to $numberOfNics , but in fact the length will depend on the $networkname variable. For example, you have networkname variables from $networkname1 to $networkname9 , so your loop will look something like this:

 $NetworkInfo = @() for($i = 1; $i -le 9; $i++) {$NetworkInfo += Get-Variable("networkname" + $i)} 

Now $NetworkInfo has

 Name Value ---- ----- networkname1 ondemandport1 networkname2 ondemandport2 networkname3 ondemandport3 networkname4 ondemandport4 networkname5 ondemandport5 networkname6 ondemandport6 networkname7 ondemandport7 networkname8 ondemandport8 networkname9 ondemandport9 

You can put another for loop like this:

 $NetworkInterface = @() for ($i = 0; $i -lt $NetworkInfo.Length; $i++) { $NetworkInterface += New-AzureRmNetworkInterface -Name $NetworkInfo.Value[$i] } 

Access to individual elements can be obtained using $NetworkInterface[0], $NetworkInterface[1] ... etc.

0
source

Source: https://habr.com/ru/post/1275184/


All Articles