简体   繁体   English

Azure-Powershell 脚本错误(IP 地址增量)

[英]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

What Exactly is Happening究竟发生了什么

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 不是通过上述逻辑创建的。

  1. You are initializing the variable ipConfig as a string您正在将变量 ipConfig 初始化为字符串
  2. Overwriting the value of the variable ipconfig with the string value $ipconfig1 (for the first iteration)用字符串值 $ipconfig1 覆盖变量 ipconfig 的值(第一次迭代)
  3. Overwriting the string value of the variable ipconfig with object.用对象覆盖变量 ipconfig 的字符串值。

AzNetworkInterfaceIpConfig AzNetworkInterfaceIpConfig

End of the iteration 1, $ipconfig is with the value AzNetworkInterfaceIpConfig ipconfig1 is non existant.迭代 1 结束时,$ipconfig 的值为 AzNetworkInterfaceIpConfig ipconfig1 不存在。

Suggestion :建议 :

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.

相关问题 同时使用Azure-Powershell部署虚拟机 - Deploy Virtual Machine with Azure-Powershell concurrently Azure-Powershell 对存储帐户启用文件服务加密 - Azure-Powershell Enable File Service Encryption on storage account 为Azure-PowerShell SDK制作python包装器是一个愚蠢的主意吗? - Is making a python wrapper for the Azure-PowerShell SDK a stupid idea? 用于更新 Azure CosmosDB 中 IP 地址的 Powershell 命令 - Powershell command to update IP address in Azure CosmosDB 在 Azure Powershell 中创建 NIC 时出错,只是因为私有 IP 地址无效, - Error while creating NIC in Azure Powershell, just because of invalid Private IP address, 如何使用https://github.com/Azure/azure-powershell源代码? - How to use https://github.com/Azure/azure-powershell source code? 使用PowerShell为Azure中的现有nic分配公共IP地址 - Assigning a public ip address to an existing nic in Azure using powershell 如何使用 Powershell 7 将保留的 IP 地址分配给 Azure 上的云服务? - How to assign a reserved IP address to a cloud service on Azure using Powershell 7? Azure Powershell脚本:解析错误 - Azure Powershell Script: Parse Error 如何通过Powershell获取新创建的Azure VM的IP地址 - How to get the IP address of the newly created Azure VM via Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM