我们平常在Azure部署多台虚拟机之后,可能希望虚拟机的IP就此固定,不要发生由于批量停、开机导致PIP变化的情况,使用下面的PowerShell脚本,我们可以很方便的将虚拟机PIP批量设置为静态。
先上个运行结果图:
以下为脚本内容:
#------------------------------------------------------------------------------
# User own the risk, otherwise exit.
#
# Azure PowerShell Version: 3.6.0
#
# Create by Zeno.
#------------------------------------------------------------------------------
#Login AzureChinaCloud
#Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $(Get-Credential -UserName admin@xxx.partner.onmschina.cn -Message Login_AzureChinaCloud) |Out-Null
$nics = Get-AzureRmNetworkInterface #-ResourceGroupName $nicrg -Name $nicname
foreach($nic in $nics)
{
$nic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"
#$nic.IpConfigurations[1].PrivateIpAllocationMethod = "Dynamic"
Set-AzureRmNetworkInterface -NetworkInterface $nic -ErrorAction Stop | Out-Null
Write-Host ("Private NetworkInterface {0}'s IpAddress is Allocated to {1} Successful" -f $nic.Name, $nic.IpConfigurations.PrivateIpAddress) -ForegroundColor Green
}
#显示所有虚拟网卡信息
Write-Host "`n`tDisplay all NICs current status:" -ForegroundColor Green
Get-AzureRmNetworkInterface |Get-AzureRmNetworkInterfaceIpConfig |`
Format-Table @{Name="NIC"; Expression={$_.Id.split('/')[-3]}},PrivateIpAllocationMethod,PrivateIpAddress,`
@{Name="Subnet"; Expression={$_.Subnet.Id.split('/')[-1]}},`
@{Name="AllcatedPIP"; Expression={$_.PublicIpAddress.Id.split('/')[-1]}}