通过Powershell获取与Azure ARM VM的NIC集关联的当前IP地址

Ear*_*ash 5 powershell azure azure-resource-manager

我正在尝试编写一些Powershell来获取Azure ARM vms(非经典)列表以及其NIC的当前关联IP地址.

在经典中,这是VM对象的一部分,但在ARM中它是一个单独的对象,我正在努力让Powershell以我想要的方式工作.

我有以下代码段:

$nic = (((Get-AzureRmVM).NetworkProfile).NetworkInterfaces).Id
ForEach ($i in $nic) {
  $nicname = $i.substring($i.LastIndexOf("/")+1)
  Get-AzureRmNetworkInterface -Name $nicname -ResourceGroupName RGTEST | Get-AzureRmNetworkInterfaceIpConfig | select-object  PrivateIpAddress,PrivateIpAllocationMethod
}
Run Code Online (Sandbox Code Playgroud)

哪个有效,但仅适用于指定资源组"RGTEST"中的VM.

似乎Get-AzureRmNetworkInterface只能在NIC Name和ResourceGroupName中传递时工作,但我似乎无法从VM传入RGname.

可能真的很容易,但我正在努力!

Fra*_*ois 9

我使用此代码获取所有ARM VM,它们的私有IP地址和分配方法,它适用于资源组.

$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
    Write-Output "$($vm.Name) : $prv , $alloc"
}
Run Code Online (Sandbox Code Playgroud)

示例输出:
proddc:10.0.0.4,静态
stagedc:10.1.0.4,静态