批量修改azure內網地址為靜態

weixin_34378969發表於2018-04-17

一.檢視所有的動態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
    }
}

相關文章