I am trying to add a variable $numberOfNics which adds nics based on the number. Right now this is my code, and I am a little confused as to how to put it into a loop. I know an array is probably the way to go but not quite sure how to make an array in this sense that just calls variables instead of sets 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 tidy this up, I would like to make a loop something like this
for ($i = 0; $i -lt $numberOfNics; $i++)
{
$networkInterface$i = New-AzureRmNetworkInterface -Name $networkname$i
}
EDIT: Thanks Martin, but now I am having trouble calling the 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 looking to make this into a 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 : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.