5

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.

3 Answers 3

3

You can also do it with one 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 templates instead.


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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Martin, the one liner worked well, and I've added it to my code, however, now I am having errors when calling the variable. I've edited my original post to show the error I am getting.
@doggydogworld The first interface would have index 0, maybe you get out of range here? However, you could try to use the Get-AzureRemNetworkInterface cmdlet to retrieve the nic, see my edited answer.
0

You should be able to 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 the New-AzureRmNetworkInterface but works with other New- commands

Comments

0

You could use PowerShell Get-Variable cmdlet. One thing which I would like to point out is the for loop which you are using will not be limited till $numberOfNics, but as a matter of fact, it's length will depend upon $networkname variable. For example, you are having 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 something like this -

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

Individual elements can then be accessed by $NetworkInterface[0], $NetworkInterface[1]... and so on.

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.