[英]I want to loop this Script (Azure-powershell / Powershell)
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我想要一个循环,我可以指定我想要的增量数,我想将一些参数增加 1,比如 $ipconfig $Ipconfigname $publicip 和 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我想要一个循环来创建 x 个这个
#####################################
# 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)
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
There is a few ways to loop.有几种循环方式。 In this example I used creating an array and piping to a foreach-object.
在这个例子中,我使用了创建一个数组和管道到一个 foreach 对象。 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) `
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.