一.查看所有的动态ip
#获取所有的vm
$vms = get-azurermvm
#获取所有的网络接口
$nics = get-azurermnetworkinterface | where VirtualMachine -NE $null #skip Nics with no VM
#遍历每一个网络接口
foreach($nic in $nics)
{
$vm = $vms | where-object -Property Id -EQ $nic.VirtualMachine.id
$prv = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress
$alloc = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAllocationMethod
$gr = $vm.ResourceGroupName
#判断alloc 是否是Dynamic
if ($alloc -eq "Dynamic")
{
Write-Output "$($vm.Name) : $prv , $alloc ,$gr"
}
}
二.按照资源组修改
#定义资源组
$RG = "xxx"
$vms = get-azurermvm -ResourceGroupName $RG
$nics = get-azurermnetworkinterface -ResourceGroupName $RG | where VirtualMachine -NE $null #skip Nics with no VM
foreach($nic in $nics)
{
$vm = $vms | where-object -Property Id -EQ $nic.VirtualMachine.id
$prv = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress
$alloc = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAllocationMethod
if ($alloc -eq "Dynamic")
{
$nic.IpConfigurations[0].PrivateIpAllocationMethod = 'Static'
Set-AzureRmNetworkInterface -NetworkInterface $nic
$IP = $nic.IpConfigurations[0].PrivateIpAddress
}
}
三.修改所有动态
$vms = get-azurermvm
$nics = get-azurermnetworkinterface | where VirtualMachine -NE $null #skip Nics with no VM
foreach($nic in $nics)
{
$vm = $vms | where-object -Property Id -EQ $nic.VirtualMachine.id
$prv = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress
$alloc = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAllocationMethod
if ($alloc -eq "Dynamic")
{
$nic.IpConfigurations[0].PrivateIpAllocationMethod = 'Static'
Set-AzureRmNetworkInterface -NetworkInterface $nic
$IP = $nic.IpConfigurations[0].PrivateIpAddress
}
}