0

I want a loop that i can specify the number of increments that I want and i want to increment some parameters by 1 like $ipconfig $Ipconfigname $publicip and also 10.0.0.25

the result that I want:

#####################################
# Create a public IP address 1
$PublicIP1 = New-AzPublicIpAddress `
    -Name "q9r933209h1" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -AllocationMethod dynamic
#Create an IP configuration with a dynamic private IP address and assign the public IP address to it
$IpConfigName1 = "00iifcow5n1"
$IpConfig1 = New-AzNetworkInterfaceIpConfig `
    -Name $IpConfigName1 `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.25 `
    -PublicIpAddress $PublicIP1

#####################################   
# Create a public IP address 2
$PublicIP2 = New-AzPublicIpAddress `
    -Name "q9r933209h2" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -AllocationMethod dynamic
#Create an IP configuration with a dynamic private IP address and assign the public IP address to it
$IpConfigName2 = "00iifcow5n2"
$IpConfig2 = New-AzNetworkInterfaceIpConfig `
    -Name $IpConfigName2 `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.26 `
    -PublicIpAddress $PublicIP2

#####################################   
# Create a public IP address 3
$PublicIP3 = New-AzPublicIpAddress `
    -Name "q9r933209h3" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -AllocationMethod dynamic
#Create an IP configuration with a dynamic private IP address and assign the public IP address to it
$IpConfigName3 = "00iifcow5n3"
$IpConfig3 = New-AzNetworkInterfaceIpConfig `
    -Name $IpConfigName3 `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.27 `
    -PublicIpAddress $PublicIP3

i want a loop that create x number of this

#####################################
# Create a public IP address 1
$PublicIP1 = New-AzPublicIpAddress `
    -Name "q9r933209h1" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -AllocationMethod dynamic
#Create an IP configuration with a dynamic private IP address and assign the public IP address to it
$IpConfigName1 = "00iifcow5n1"
$IpConfig1 = New-AzNetworkInterfaceIpConfig `
    -Name $IpConfigName1 `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.25 `
    -PublicIpAddress $PublicIP1

end after loop I want to run this command

$NIC = New-AzNetworkInterface `
    -Name X `
    -ResourceGroupName $RgName `
    -Location $Location `
    -NetworkSecurityGroupId $NSG.Id `
    -IpConfiguration $IpConfig1, $IpConfig2, $IpConfig3, .....,$IpConfig(x time)
0

2 Answers 2

1

Something like this should work:

$x = 4
$publicIpParams = @{
    ResourceGroupName = $RgName
    Location = $Location
    AllocationMethod = 'Dynamic'
}
$ipConfiguration = 1..$x | ForEach-Object {
    $publicIP = New-AzPublicIpAddress @publicIpParams -Name ("q9r933209h{0}" -f $_)
    $ipcParams = @{
        Name = "00iifcow5n{0}" -f $_
        Subnet = $Subnet
        PrivateIpAddress = '10.0.0.{0}' -f (24 + $_)
        PublicIpAddress = $publicIp
    }
    New-AzNetworkInterfaceIpConfig @ipcParams
}

$niParams = @{
    Name = 'X'
    ResourceGroupName = $RgName
    Location = $Location
    NetworkSecurtyGroupId = $nsg.Id
    IpConfiguration = $ipConfiguration
}
New-AzNetworkInterface @niParams
Sign up to request clarification or add additional context in comments.

Comments

0

There is a few ways to loop. In this example I used creating an array and piping to a foreach-object. You dont have to use $($_) you can use just $_, but i added it for readability.

$Loop = 3
(1..$Loop) | foreach-object{
    #####################################
    # Create a public IP address 1
    $PublicIP1 = New-AzPublicIpAddress `
    -Name "q9r933209h$($_)" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -AllocationMethod dynamic
    #Create an IP configuration with a dynamic private IP address and assign the public IP address to it
    $IpConfigName1 = "00iifcow5n$($_)"
    $IpConfig1 = New-AzNetworkInterfaceIpConfig `
        -Name (Get-Variable -Name "IpConfigName$($_)" -ValueOnly) `
        -Subnet $Subnet `
        -PrivateIpAddress "10.0.0.$(25 + ($_ - 1))" `
        -PublicIpAddress (Get-Variable -Name "PublicIP$($_)" -ValueOnly) `
}

4 Comments

i want to change for each one $PublicIP1 to $PublicIP2 ... and for $ipconfigName1 to ipconfigName2 ... and $ipconfig1 to $ipconfig2 ... because I will use in the end this command : -IpConfiguration $IpConfig1, $IpConfig2, $IpConfig3, .... so can you help me with that too ? that -ipConfiguration $ipConfig1,......,$ipconfig($loop)
it does that already. It changes PublicIP1 to 2 and so on....(Get-Variable -Name "PublicIP$($_)" -ValueOnly)
But can you please see the last command that I want to run after loop. -IpConfiguration take all $ipconfig that are created and for example if we have $loop=3 ! $NIC = New-AzNetworkInterface -IpConfiguration $IpConfig1, $IpConfig2, $IpConfig3
OK so...the error you are showing is there is no variable named $IpConfigName3 or $PublicIP3

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.