【问题标题】:Azure VMs fails as public ip is allocated to other resourceAzure VM 失败,因为公共 ip 已分配给其他资源
【发布时间】:2022-11-03 20:29:20
【问题描述】:

我正在使用 powershell 脚本基于图像创建多个 Vms。第一个 Vm 没问题,但是在尝试第二个 Vm 时,我收到一条错误消息:

 | Resource /subscriptions/....../networkInterfaces/xxxxx/ipConfigurations/xxxxx is referencing public IP address
 | /subscriptions/xxxxxxxxx/providers/Microsoft.Network/publicIPAddresses/Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress that is already allocated to
 | resource /subscriptions/......./networkInterfaces/xxxxx/ipConfigurations/xxxxx.

这是我正在使用的脚本:

param(
        [string] $WeekNo="NoWeek",
        [int] $VmCount=0
        )
        
#$cred = Get-Credential -Message "Enter a username and password for the virtual machine."

## VM Account
# Credentials for Local Admin account you created in the sysprepped (generalized) vhd image
$VMLocalAdminUser = "xxxxx"
$VMLocalAdminSecurePassword = ConvertTo-SecureString "xxxxxxx" -AsPlainText -Force
$image = "/subscriptions/xxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Compute/images/xxxxxxxxx"
## Azure Account
$LocationName = "SwedenCentral"
$ResourceGroupName = "xxxx_" + $WeekNo

if( -Not( Get-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName -ErrorAction Ignore)) {

New-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName
Write-Host "ResourceGroup" $ResourceGroupName "created"

$VMSize = "Standard_B2ms"

## Networking
$NetworkName = "xxxxxx_" + $WeekNo + "_net" # "MyNet"


$SubnetName = "MySubnet"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"

$SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
$Vnet = New-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
}


$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword);
$VMName = "xxxx" + $WeekNo

##New-AzVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VirtualMachine -Verbose -Image $image
for($i=1; $i -le $VmCount; $i++){
$VMBaseName = "iCPSEDU" + $WeekNo + $i

$StorageAccount = "xxxxx" + $WeekNo + $i
$PublicIPAddressName = $VMBaseName  + "PIP$(Get-Random)"
$NICName = $VMBaseName + "NIC"
$DNSNameLabel = "xxxx" + $WeekNo + $i + "dns" # mydnsname.westus.cloudapp.azure.com

$PIP = New-AzPublicIpAddress -Name $PublicIPAddressName -DomainNameLabel $DNSNameLabel -ResourceGroupName $ResourceGroupName -Location $LocationName -AllocationMethod Dynamic
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $Vnet.Subnets[0].Id -PublicIpAddressId $PIP.Id


Write-Host "Creating VM " $VMBaseName

New-AzVm `
    -ResourceGroupName $ResourceGroupName `
    -Name $VMBaseName `
    -ImageName $image `
    -Location $LocationName `
    -VirtualNetworkName $Vnet `
    -SubnetName $SubnetName `
    -SecurityGroupName "myImageNSG" `
    -PublicIpAddressName $PIP -Credential $Credential -Size $VMSize -PublicIpSku Standard


Write-Host "VM " $VMBaseName " Created"

Stop-AzVM -ResourceGroupName $ResourceGroupName $VMBaseName -Force -NoWait

Write-Host "VM " $VMBaseName " Stopped"

    }

Write-Host "Done."`

对我来说,用于 PIP 的变量似乎在执行之间没有正确“刷新”,但我不知道如何做到这一点?

还是有其他原因导致错误?

我尝试添加一些延迟但没有效果。

【问题讨论】:

  • 我现在进一步研究了命令 New-AzVm,如果不可用,它会创建资源。在这种情况下这将很有用,但我仍然对上述创建失败的原因感到好奇。

标签: azure powershell virtual-machine infrastructure-as-code


【解决方案1】:
  1. 创建公共 IP 地址并指定 DNS 名称
  2. 创建 NSG
  3. 创建 NIC 并与创建的 pub IP 地址和 NSG 关联
  4. 创建虚拟机配置并分配网卡
  5. 使用配置创建 VM

    https://github.com/Azure/azure-docs-powershell-samples/blob/master/virtual-machine/create-vm-detailed/create-windows-vm-detailed.ps1

    重要步骤的粗略总结:

    $pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
      -Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
    
    $nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
      -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP
    
    $nic = New-AzNetworkInterface -Name myNic -ResourceGroupName $resourceGroup -Location $location `
      -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
    
    $vmConfig = New-AzVMConfig -VMName $vmName -VMSize Standard_D1 | `
    Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
    Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
    Add-AzVMNetworkInterface -Id $nic.Id
    
    New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig
    

    MS 正在为各种任务提供经过良好测试的 powershell 代码:

    我更喜欢 github 示例 https://github.com/Azure/azure-docs-powershell-samples 而不是 learn 和 doc.microsoft.com 中的步骤

    还可以深入了解 Azure CLI 示例和基于模板的部署。在我看来,MS 有点放弃 PS。

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多