使用没有 public IP 的 powershell 在 Azure 中创建 VM

Create VM in Azure with powershell with no public IP

我正在使用 powershell 从映像在 Azure 上创建 VM。

这是我正在使用的脚本。

$UserName = "username"
$Password = ConvertTo-SecureString "password@123" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)    
New-AzureRmVm `
    -ResourceGroupName "RSG" `
    -Name "VMName" `
    -ImageName "ImageName" `
    -Location "West US" `
    -VirtualNetworkName "VNName" `
    -SubnetName "default" `
    -Credential $psCred
    -PublicIpAddressName "None" `
    -OpenPorts 3389

但是,当我进入 Azure 门户并看到时,一些 Public IP 正在默认分配。我也试过不给 PublicIpAddressName 属性 假设,它不会分配任何 IP,但它仍然在分配。

我想要 Public IP none.Can 谁帮我实现 this.Thanks!

目前这个问题在官方 azure-powershell github 上仍处于 Open 状态。你可以参考它 here 。如果你仍然想绕过这个,你可以尝试使用 New-AzureReservedIP 或者在部署命令之后尝试自己删除 public ip Remove-AzureRmPublicIpAddress.

注意 : 我还没有测试过。只是一个想法。

参考:Docs

要设置没有 public 您拥有的 ip 地址可以将其定义为 "" ,在 powershell 中您将需要再次引用它,因此它将是 """" 。

如果您使用的是 PowerShell,则需要通过将“”更改为“”“”来转义所有空参数,以便将空字符串正确传递到命令中。如果没有这个,PowerShell 将不会传递空字符串,并且您会从命令中得到一个错误,指示它缺少一个参数。

$winVmCred = Get-Credential `
  -Message "Enter username and password for the Windows management virtual machine."

# Create a NIC for the VM.
$winVmNic = New-AzNetworkInterface -Name "winVMNIC01" `
  -ResourceGroupName $resourceGroup.ResourceGroupName `
  -Location $location `
  -SubnetId $targetVMSubnet.Id `
  -PrivateIpAddress "10.10.12.10"

# Configure the Windows management VM.
$winVmConfig = New-AzVMConfig -VMName $winVmName -VMSize $winVmSize | `
Set-AzVMOperatingSystem -Windows -ComputerName $winVmName -Credential $winVmCred | `
Set-AzVMSourceImage -PublisherName $winVmPublisher `
  -Offer $winVmOffer `
  -Skus $winVmSku `
  -Version $winVmVersion | `
  Add-AzVMNetworkInterface -Id $winVmNic.Id

# Create the VM.
$winVM = New-AzVM -ResourceGroupName $resourceGroup.ResourceGroupName `
  -Location $location `
  -VM $winVmConfig `
  -ErrorAction Stop