批量修改azure内网地址为静态

一.查看所有的动态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
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。