Azure云计算笔记经验菜鸟

快速分享:如何使用PowerShell/CloudShell查询每一个Azure VMSS实例的私有/公共IP

Banner_MS-Azure_logo_horiz_c-gray_rgb_andyx.net_.png插图给大家快速分享一些PowerShell/CloudShell代码,用于查询每一个Azure VMSS实例的私有IP:

查询每一个VMSS Instance的 名称+PrivateIP:
$vmssName = "VMSSNAME"
$rgName = "RESOURCEGROUP"
$VMs = Get-AzVmssVM -ResourceGroupName $rgName -VMScaleSetName $vmssName
$nicName = ($VMs[0].NetworkProfile.NetworkInterfaces[0].Id).Split('/')[-1]
 
foreach ($vm in $VMs)
{
    $resourceName = $vmssName + "/" + $VM.InstanceId + "/" + $nicName
    $target = Get-AzResource -ResourceGroupName $rgName -ResourceType Microsoft.Compute/virtualMachineScaleSets/virtualMachines/networkInterfaces -ResourceName $resourceName -ApiVersion 2017-03-30
    $vm.Name + " " + $target.Properties.ipConfigurations[0].properties.privateIPAddress
}
执行结果:
how_to_obtain_the_private_ip_address_from_each_instance_of_vmss_by_using_the_powershell_script_01.png插图(1)

 

仅查询每一个VMSS Instance的 PrivateIP:
$vmssName = "VMSSNAME"
$rgName = "RESOURCEGROUP"
$VMs = Get-AzVmssVM -ResourceGroupName $rgName -VMScaleSetName $vmssName
$nicName = ($VMs[0].NetworkProfile.NetworkInterfaces[0].Id).Split('/')[-1]

foreach ($vm in $VMs)
{
    $resourceName = $vmssName + "/" + $VM.InstanceId + "/" + $nicName
    $target = Get-AzResource -ResourceGroupName $rgName -ResourceType Microsoft.Compute/virtualMachineScaleSets/virtualMachines/networkInterfaces -ResourceName $resourceName -ApiVersion 2017-03-30
    $target.Properties.ipConfigurations[0].properties.privateIPAddress
}
执行结果:
how_to_obtain_the_private_ip_address_from_each_instance_of_vmss_by_using_the_powershell_script_02.png插图(2)

 

查询每个VMSS Instance的名称+ID+Public IP:
$vmssName = "VMSSNAME" 
$rgName = "RESOURCEGROUP"
$report = @()
$vmss = Get-AzVmss -ResourceGroupName $rgName -VMScaleSetName $vmssName
$instances = Get-AzVmssVM -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name
$pips = Get-AzPublicIpAddress -ResourceGroupName $vmss.ResourceGroupName -VirtualMachineScaleSetName $vmss.Name

foreach($instance in $instances)
{
    $info = "" | Select ResourceGroupName, VmssName, InstanceID, PublicIP

    foreach($pip in $pips)
  {
        if($pip.Id.Split("/")[10] -EQ $instance.InstanceId)
    {
            $info.PublicIP = $pip.IpAddress
        }
    
    }
    $info.ResourceGroupName = $vmss.ResourceGroupName
    $info.VmssName = $vmss.Name
    $info.InstanceID = $instance.InstanceId
    
    $report+=$info
}

$report | ft ResourceGroupName, VmssName, InstanceID, PublicIP

执行结果:

how_to_obtain_the_private_ip_address_from_each_instance_of_vmss_by_using_the_powershell_script_03.png插图(3)

 

仅查询每个VMSS Instance的Public IP:
$vmssName = "VMSSNAME" 
$rgName = "RESOURCEGROUP"
$report = @()
$vmss = Get-AzVmss -ResourceGroupName $rgName -VMScaleSetName $vmssName
$instances = Get-AzVmssVM -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name
$pips = Get-AzPublicIpAddress -ResourceGroupName $vmss.ResourceGroupName -VirtualMachineScaleSetName $vmss.Name

foreach($instance in $instances)
{
    $info = "" | Select PublicIP
    foreach($pip in $pips)
	{
        if($pip.Id.Split("/")[10] -EQ $instance.InstanceId)
		{
            $info.PublicIP = $pip.IpAddress
        }
    }
    $report+=$info
}

$report | ft PublicIP

执行结果:

how_to_obtain_the_private_ip_address_from_each_instance_of_vmss_by_using_the_powershell_script_04.png插图(4)
以上
(END)

 

[本文PrivateIP相关内容由AndyX撰写,PublicIP相关内容由Bond Han撰写,感谢Bond的强力支持!]

[本文的“Azure Cloudshell”测试为主,仅供参考。]

[本文为AndyX.Net原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明!]