Azure虚拟机批量设置静态IP

来源:互联网 发布:php正则表达式 编辑:程序博客网 时间:2024/06/05 21:12

我们平常在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 $nicnameforeach($nic in $nics){$nic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"#$nic.IpConfigurations[1].PrivateIpAllocationMethod = "Dynamic"Set-AzureRmNetworkInterface -NetworkInterface $nic -ErrorAction Stop | Out-NullWrite-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 GreenGet-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]}}


原创粉丝点击