下文介绍利用PowerShell脚本批量统计所有虚拟机相关信息,例如:位置、规格、操作系统、内网IP、公网IP等信息
Login-AzureRmAccount -EnvironmentName Azurechinacloud
Select-AzureRmSubscription -SubscriptionName WATSTest02
$AllNic=[System.Collections.ArrayList]@()
$AllRG=(Get-AzureRmResourceGroup).ResourceGroupName
foreach(
$Nics=Get-AzureRmNetworkInterface -ResourceGroupName $RG
foreach($nic in $Nics){
$d=@{}
if( $nic.VirtualMachine )
{
$d.Add("Vmname",$nic.VirtualMachine[0].Id.Split("/")[8])
$vm=Get-AzureRmVM -ResourceGroupName $nic.VirtualMachine[0].Id.Split("/")[4] -Name $nic.VirtualMachine[0].Id.Split("/")[8]
$d.Add("Vmsize",$vm.HardwareProfile.VmSize)
if( $vm.OSProfile.WindowsConfiguration ){
$d.Add("OStype","Windows")
}
else
{
$d.Add("OStype","Linux")
}
$d.Add("NicName",$nic.Name)
$d.Add("PrivateIP",$nic.IpConfigurations[0].PrivateIpAddress)
$d.Add("PrivateIPAllocationMethod",$nic.IpConfigurations[0].PrivateIpAllocationMethod)
if( $nic.IpConfigurations[0].PublicIpAddress )
{
$pubip=Get-AzureRmPublicIpAddress -Name $nic.IpConfigurations[0].PublicIpAddress.id.Split("/")[8] -ResourceGroupName $nic.IpConfigurations[0].PublicIpAddress.id.Split("/")[4]
$d.Add("PublicIP",$pubip.IpAddress)
$d.Add("PublicIpAllocationMethod",$pubip.PublicIpAllocationMethod)
}
else
{
$d.Add("PublicIP","Null")
$d.Add("PublicIpAllocationMethod","Null")
}
$d.Add("Location",$nic.Location )
$AllNic.Add($d)
}
}
}
AllNic | select @{Name="Vmname";Expression={_["Vmname"]}},@{Name="Vmsize";Expression={_["Vmsize"]}},@{Name="OStype";Expression={_["OStype"]}},@{Name="NicName";Expression={_["NicName"]}},@{Name="PrivateIP";Expression={_["PrivateIP"]}},@{Name="PrivateIpAllocationMethod";Expression={_["PrivateIpAllocationMethod"]}},@{Name="PublicIP";Expression={_["PublicIP"]}},@{Name="PublicIpAllocationMethod";Expression={_["PublicIpAllocationMethod"]}},@{Name="Location";Expression={_[“Location”]}} | Out-GridView