[英]Azure-Powershell Script error (ip address increment )
I hope you all doing well我希望你们一切顺利
can you please help me to solve this problem?你能帮我解决这个问题吗?
$ipnumber = 3
$RgName = "test"
$Location = "test"
for ($i = 0; $i -lt $ipnumber; $i++) {
$res1 = -join ((1..10) | ForEach-Object { get-random -minimum 97 -maximum 122 | ForEach-Object { [char]$_ } })
$res2 = -join ((1..10) | ForEach-Object { get-random -minimum 97 -maximum 122 | ForEach-Object { [char]$_ } })
$PublicIP = '$PublicIP'
$IpConfigName = '$IpConfigName'
$IpConfig = '$IpConfig'
$ip = '10.0.0.'
$x = 25
$PublicIP = $PublicIP + $i
$IpConfigName = $IpConfigName + $i
$IpConfig = $IpConfig + $i
$x = 25 + $i
$ip = $ip + $x
#####################################
# Create a public IP address
$PublicIP = New-AzPublicIpAddress `
-Name "$res1" `
-ResourceGroupName $RgName `
-Location $Location `
-AllocationMethod dynamic
#Create an IP configuration with a dynamic private IP address and assign the public IP address to it
$IpConfigName = "$res2"
$IpConfig = New-AzNetworkInterfaceIpConfig `
-Name $IpConfigName `
-Subnet $Subnet `
-PrivateIpAddress $ip `
-PublicIpAddress $PublicIP
}
$rand6 = -join ((1..10) | ForEach-Object { get-random -minimum 97 -maximum 122 | ForEach-Object { [char]$_ } })
$NIC = New-AzNetworkInterface `
-Name $rand6 `
-ResourceGroupName $RgName `
-Location $Location `
-NetworkSecurityGroupId $NSG.Id `
-IpConfiguration $IpConfig0, $IpConfig1, $IpConfig2
Error1 : enter image description here错误 1:在此处输入图像描述
Error2 : New-AzNetworkInterface: C:\\Users\\Marouane\\Desktop\\testpowershell\\test2.ps1:95:22 Line |错误 2:新的 AzNetworkInterface:C:\\Users\\Marouane\\Desktop\\testpowershell\\test2.ps1:95:22 行 | 95 |
95 | -IpConfiguration $IpConfig, $IpConfig0, $IpConfig1 |
-IpConfiguration $IpConfig, $IpConfig0, $IpConfig1 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Cannot bind parameter 'IpConfiguration'.
无法绑定参数“IpConfiguration”。 Cannot convert the "$IpConfig1" value of type "System.String" to type |
无法将“System.String”类型的“$IpConfig1”值转换为类型 | "Microsoft.Azure.Commands.Network.Models.PSNetworkInterfaceIPConfiguration".
“Microsoft.Azure.Commands.Network.Models.PSNetworkInterfaceIPConfiguration”。
I want to increment the IP by 1 Ex : 10.0.0.25 next 10.0.0.26 next 10.0.0.27 but it says that you can't put in 'IpConfiguration' String ips So what should i do ?我想将 IP 增加 1 Ex : 10.0.0.25 next 10.0.0.26 next 10.0.0.27 但它说你不能输入 'IpConfiguration' String ips 那么我该怎么办? please help me!!
请帮我!! Thank you
谢谢
I think Mainich you are trying do the array operation.我认为 Mainich 您正在尝试执行数组操作。
If I understand correctly, you are trying to create to create an array of the below variable :如果我理解正确,您正在尝试创建以下变量的数组:
$PublicIP = $PublicIP + $i
$IpConfigName = $IpConfigName + $i
$IpConfig = $IpConfig + $i
For instance,例如,
IpConfig1,Ipconfig2,Ipconfig3 - and store the value accordingly. IpConfig1、Ipconfig2、Ipconfig3 - 并相应地存储值。
Unfortunately, the implementation is slightly wrong不幸的是,实现有点错误
Please note : I have detailed the operation only for the $ipconfig variable.请注意:我只详细说明了 $ipconfig 变量的操作。 You can extend the same to other variables like
$IpConfigName,$PublicIP
您可以将相同的内容扩展到其他变量,如
$IpConfigName,$PublicIP
For all the iteration, $IpConfig gets updated & the variables you intended IpConfig1,Ipconfig2,Ipconfig3 are not created through the above logic.对于所有迭代,$IpConfig 都会更新,并且您想要的变量 IpConfig1、Ipconfig2、Ipconfig3 不是通过上述逻辑创建的。
AzNetworkInterfaceIpConfig AzNetworkInterfaceIpConfig
End of the iteration 1, $ipconfig is with the value AzNetworkInterfaceIpConfig ipconfig1 is non existant.迭代 1 结束时,$ipconfig 的值为 AzNetworkInterfaceIpConfig ipconfig1 不存在。
The below creates an empty array with a fixed number - in your case 3 as $ipnumber = 3下面创建一个带有固定数字的空数组 - 在你的情况下 3 为 $ipnumber = 3
$ipconfig = @() * $ipnumber
$ipconfig.append(New-AzNetworkInterfaceIpConfig `
-Name <>`
-Subnet<>`
-PrivateIpAddress <> `
-PublicIpAddress <>)
( or ) ( 或者 )
$ipconfig[0] = New-AzNetworkInterfaceIpConfig `
-Name <>`
-Subnet<>`
-PrivateIpAddress <> `
-PublicIpAddress <>
Now you can access the values through ipconfig[0], ipconfig[1],ipconfig[2] Or you can pass the $ipconfig directly - as the parameter Ip configuration accepts the array value现在您可以通过 ipconfig[0], ipconfig[1],ipconfig[2] 访问这些值,或者您可以直接传递 $ipconfig - 作为参数 Ip configuration 接受数组值
-IpConfiguration <PSNetworkInterfaceIPConfiguration[]>
https://docs.microsoft.com/en-us/powershell/module/az.network/new-aznetworkinterface?view=azps-5.0.0 https://docs.microsoft.com/en-us/powershell/module/az.network/new-aznetworkinterface?view=azps-5.0.0
Suggestion 2:建议2:
if you would prefer creating the variable on the fly like ipconfig1,ipconfig2,ipconfig3, then you could do the below :如果您更喜欢像 ipconfig1、ipconfig2、ipconfig3 一样动态创建变量,那么您可以执行以下操作:
New-Variable -Name "Ipconfig$i" -Value <>
New-Variable -Name "Ipconfig$i" -Value <>
Get-Variable -Name "Ipconfig$i" -ValueOnly
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.