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.
source share