Azure虚拟机批量设置静态IP

我们平常在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]}}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值