【问题标题】:Create VM in Azure with powershell with no public IP使用没有公共 IP 的 powershell 在 Azure 中创建 VM
【发布时间】:2018-08-27 04:01:41
【问题描述】:

我正在使用 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 门户并查看时,默认情况下会分配一些公共 IP。我也试过不给PublicIpAddressName属性假设,它不会分配任何IP,但它仍然在分配。

我希望公共 IP 为空。谁能帮我实现这一点。谢谢!

【问题讨论】:

  • 其实“None”的意思是不指定名字,是默认值。 Azure 将为它提供一个名称。
  • @CharlesXu-MSFT,那么如何让它空白?
  • 很遗憾,没有公网 IP 似乎无法创建虚拟机。

标签: azure powershell azure-virtual-machine azure-public-ip


【解决方案1】:

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

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

参考:Docs

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这不是真的。我实际上已经尝试过(但是这个建议听起来很愚蠢),使用New-AzVM ... -PublicIpAddressName """" ... 并且我得到了错误:New-AzVM : Resource name " is invalid. The name can be up to 80 characters long. It must begin with a word character, a nd it must end with a word character or with '_'. The name may contain word characters or '.', '-', '_'. 当名称指定为'""' 时它也不起作用。也许它适用于 az 命令,但不适用于 PowerShell cmdlet。
    • 这是一个可能已经修复的错误。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:
      $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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        • 2021-07-12
        • 1970-01-01
        • 2017-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多