使用 PowerShell 创建具有静态专用 IP 地址的虚拟机Create a virtual machine with a static private IP address using PowerShell

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

可以创建具有静态专用 IP 地址的虚拟机 (VM)。You can create a virtual machine (VM) with a static private IP address. 若要从子网中选择分配给 VM 的具体地址,请分配静态专用 IP 地址而非动态地址。Assign a static private IP address, rather than a dynamic address, if you want to select which address from a subnet is assigned to a VM. 详细了解静态专用 IP 地址Learn more about static private IP addresses. 若要将分配给现有 VM 的专用 IP 地址从动态更改为静态,或者要使用公共 IP 地址,请参阅添加、更改或删除 IP 地址To change a private IP address assigned to an existing VM from dynamic to static, or to work with public IP addresses, see Add, change, or remove IP addresses.

创建虚拟机Create a virtual machine

可以从本地计算机完成以下步骤。You can complete the following steps from your local computer. 若要使用本地计算机,请确保安装了 Azure PowerShellTo use your local computer, ensure you have the Azure PowerShell installed.

  1. 打开命令会话并使用 Connect-AzAccount -Environment AzureChinaCloud 登录到 Azure。Open a command session and sign into Azure with Connect-AzAccount -Environment AzureChinaCloud.

  2. 使用 New-AzResourceGroup 命令创建资源组。Create a resource group with the New-AzResourceGroup command. 以下示例在“中国东部”Azure 区域中创建一个资源组:The following example creates a resource group in the China East Azure region:

    $RgName = "myResourceGroup"
    $Location = "chinaeast"
    New-AzResourceGroup -Name $RgName -Location $Location
    
  3. 使用 New-AzVirtualNetworkSubnetConfigNew-AzVirtualNetwork 命令创建子网配置和虚拟网络:Create a subnet configuration and virtual network with the New-AzVirtualNetworkSubnetConfig and New-AzVirtualNetwork commands:

    # Create a subnet configuration
    $SubnetConfig = New-AzVirtualNetworkSubnetConfig `
    -Name MySubnet `
    -AddressPrefix 10.0.0.0/24
    
    # Create a virtual network
    $VNet = New-AzVirtualNetwork `
    -ResourceGroupName $RgName `
    -Location $Location `
    -Name MyVNet `
    -AddressPrefix 10.0.0.0/16 `
    -Subnet $subnetConfig
    
    # Get the subnet object for use in a later step.
    $Subnet = Get-AzVirtualNetworkSubnetConfig -Name $SubnetConfig.Name -VirtualNetwork $VNet
    
  4. 使用 New-AzNetworkInterfaceIpConfigNew-AzNetworkInterface 命令在虚拟网络中创建一个网络接口,并将专用 IP 地址从子网分配给该网络接口:Create a network interface in the virtual network and assign a private IP address from the subnet to the network interface with the New-AzNetworkInterfaceIpConfig and New-AzNetworkInterface commands:

    $IpConfigName1 = "IPConfig-1"
    $IpConfig1     = New-AzNetworkInterfaceIpConfig `
     -Name $IpConfigName1 `
     -Subnet $Subnet `
     -PrivateIpAddress 10.0.0.4 `
     -Primary
    
    $NIC = New-AzNetworkInterface `
     -Name MyNIC `
     -ResourceGroupName $RgName `
     -Location $Location `
     -IpConfiguration $IpConfig1
    
  5. 使用 New-AzVMConfig 创建 VM 配置,然后使用 New-AzVM 创建 VM。Create a VM configuration with New-AzVMConfig, and then create the VM with New-AzVM. 出现提示时,请提供用作 VM 登录凭据的用户名和密码:When prompted, provide a username and password to be used as the sign in credentials for the VM:

    $VirtualMachine = New-AzVMConfig -VMName MyVM -VMSize "Standard_DS3"
    $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName MyServerVM -ProvisionVMAgent -EnableAutoUpdate
    $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
    $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version latest
    New-AzVM -ResourceGroupName $RgName -Location $Location -VM $VirtualMachine -Verbose
    

警告

虽然可以向操作系统添加专用 IP 地址设置,但建议不要在阅读向操作系统添加专用 IP 地址之前这样做。Though you can add private IP address settings to the operating system, we recommend not doing so until after reading Add a private IP address to an operating system.

重要

若要从 Internet 访问 VM,必须为该 VM 分配公共 IP 地址。To access the VM from the internet, you must assign a public IP address to the VM. 也可将动态专用 IP 地址分配更改为静态分配。You can also change a dynamic private IP address assignment to a static assignment. 有关详细信息,请参阅添加或更改 IP 地址For details, see Add or change IP addresses. 另外,建议将一个网络安全组关联到网络接口和/或创建网络接口时所在的子网,限制从网络到 VM 的流量。Additionally, it's recommended that you limit the network traffic to your VM by associating a network security group to the network interface, the subnet you created the network interface in, or both. 有关详细信息,请参阅管理网络安全组For details, see Manage network security groups.

清理资源Clean up resources

如果不再需要资源组及其包含的所有资源,请使用 Remove-AzResourceGroup 将其删除:When no longer needed, you can use Remove-AzResourceGroup to remove the resource group and all of the resources it contains:

Remove-AzResourceGroup -Name myResourceGroup -Force

后续步骤Next steps