如何使用 PowerShell 设置静态内部专用 IP 地址(经典版)

在大多数情况下,无需为虚拟机指定静态内部 IP 地址。 虚拟网络中的 VM 将自动从指定的范围接收内部 IP 地址。 但在某些情况下,为特定 VM 指定静态 IP 地址是有意义的。 例如,如果 VM 将运行 DNS 或将成为域控制器。 静态内部 IP 地址与 VM 保持一致,即使处于停止/取消预配状态。

重要

Azure 具有用于创建和处理资源的两个不同的部署模型:资源管理器部署模型和经典部署模型。 本文介绍使用经典部署模型的情况。 Microsoft建议大多数新部署使用 Resource Manager 部署模型

安装 Azure PowerShell 服务管理模块

在运行以下命令之前,请确保计算机上安装 了 Azure PowerShell 服务管理模块 。 有关 Azure PowerShell 服务管理模块的版本历史记录,请参阅 PowerShell 库中的 Azure 模块

如何验证特定 IP 地址是否可用

若要验证 IP 地址 10.0.0.7 是否在名为 TestVnet 的 vnet 中可用,请运行以下 PowerShell 命令并验证 IsAvailable 的值。

Test-AzureStaticVNetIP –VNetName TestVNet –IPAddress 10.0.0.7 

IsAvailable          : True
AvailableAddresses   : {}
OperationDescription : Test-AzureStaticVNetIP
OperationId          : fd3097e1-5f4b-9cac-8afa-bba1e3492609
OperationStatus      : Succeeded

注释

如果要在安全环境中测试上述命令,请按照 创建虚拟网络(经典) 中的准则创建名为 TestVnet 的 vnet ,并确保它使用 10.0.0.0/8 地址空间。

如何在创建 VM 时指定静态内部 IP

以下 PowerShell 脚本创建名为 TestService 的新云服务,然后从 Azure 检索映像,然后使用检索的映像在新云服务中创建名为 TestVM 的 VM,将 VM 设置为子网 1 中的 VM,并将 10.0.0.7 设置为 VM 的静态内部 IP:

New-AzureService -ServiceName TestService -Location "Central US"
$image = Get-AzureVMImage|?{$_.ImageName -like "*RightImage-Windows-2012R2-x64*"}
New-AzureVMConfig -Name TestVM -InstanceSize Small -ImageName $image.ImageName `
| Add-AzureProvisioningConfig -Windows -AdminUsername adminuser -Password MyP@ssw0rd!! `
| Set-AzureSubnet –SubnetNames Subnet-1 `
| Set-AzureStaticVNetIP -IPAddress 10.0.0.7 `
| New-AzureVM -ServiceName "TestService" –VNetName TestVnet

如何检索 VM 的静态内部 IP 信息

若要查看使用上述脚本创建的 VM 的静态内部 IP 信息,请运行以下 PowerShell 命令并观察 IpAddress 的值:

Get-AzureVM -Name TestVM -ServiceName TestService

DeploymentName              : TestService
Name                        : TestVM
Label                       : 
VM                          : Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVM
InstanceStatus              : Provisioning
IpAddress                   : 10.0.0.7
InstanceStateDetails        : Windows is preparing your computer for first use...
PowerState                  : Started
InstanceErrorCode           : 
InstanceFaultDomain         : 0
InstanceName                : TestVM
InstanceUpgradeDomain       : 0
InstanceSize                : Small
HostName                    : rsR2-797
AvailabilitySetName         : 
DNSName                     : http://testservice000.cloudapp.net/
Status                      : Provisioning
GuestAgentStatus            : Microsoft.WindowsAzure.Commands.ServiceManagement.Model.GuestAgentStatus
ResourceExtensionStatusList : {Microsoft.Compute.BGInfo}
PublicIPAddress             : 
PublicIPName                : 
NetworkInterfaces           : {}
ServiceName                 : TestService
OperationDescription        : Get-AzureVM
OperationId                 : 34c1560a62f0901ab75cde4fed8e8bd1
OperationStatus             : OK

如何从 VM 中删除静态内部 IP

若要删除在上述脚本中添加到 VM 的静态内部 IP,请运行以下 PowerShell 命令:

Get-AzureVM -ServiceName TestService -Name TestVM `
| Remove-AzureStaticVNetIP `
| Update-AzureVM

如何将静态内部 IP 添加到现有 VM

若要将静态内部 IP 添加到使用上述脚本创建的 VM,请运行以下命令:

Get-AzureVM -ServiceName TestService000 -Name TestVM `
| Set-AzureStaticVNetIP -IPAddress 10.10.0.7 `
| Update-AzureVM

后续步骤

保留 IP

Instance-Level 公共 IP (ILPIP)

保留IP REST API接口