none
Obtain Names / IP addresses for Azure resources in a VNet? RRS feed

  • Question

  • Is there a way in powershell to retrieve the Azure Object Name and IP address for each app contained within ILB ASE / VNet?

    Is there a way in powershell to retrieve the Azure Object Name and IP address for each Windows VM contained within a VNet?

    Wednesday, July 24, 2019 12:19 AM

Answers

  • Hi scott,

    Please use the below script:

    $vnet= Get-AzVirtualNetwork -Name <vnet_name>
    
    $subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet
    
    $Ids = $subnet.IpConfigurations.Id
    
    foreach ( $Id in $Ids)
    {
    	$nic=$Id.split("/")[8]
    	
    	$vmName = Split-Path -leaf (Get-AzNetworkInterface -name $nic).VirtualMachine.Id
    	
    	Write-output "VM Name: " $vmName
    	
    	Get-AzNetworkInterface -name $nic | Get-AzNetworkInterfaceIpConfig | Select-Object -Property PrivateIpAddress 
    	
    	$nicObj = Get-AzNetworkInterface -name $nic
    	
    	$pip = Split-Path -leaf (Get-AzNetworkInterfaceIpConfig -NetworkInterface $nicObj).PublicIpAddress.Id
    	
    	if ($pip)
    	{
    		Write-output "Public Ip Address: " (Get-AzPublicIpAddress -Name $pip).IpAddress
    	}
    }

    Thanks!

    Thursday, July 25, 2019 10:22 AM

All replies

  • Hi scott,

    Thanks for reaching out!

    In order to retrieve the VM name and IP address information within a VNet you can use the below code snippet:

    $vnet= Get-AzVirtualNetwork -Name <vnet_name>
    
    $subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet
    
    $Ids = $subnet.IpConfigurations.Id
    
    foreach ( $Id in $Ids)
    {
    	$nic=$Id.split("/")[8]
    	
    	Get-AzNetworkInterface -name $nic |  Select-Object -Property @{Name="VMName";Expression = {$_.VirtualMachine.Id.tostring().substring($_.VirtualMachine.Id.tostring().lastindexof('/')+1)}}
    	
    	
    	Get-AzNetworkInterface -name $nic | Get-AzNetworkInterfaceIpConfig | Select-Object Name,PrivateIpAddress,@{'label'='PublicIpAddress';Expression={Set-Variable -name pip -scope Global -value $(Split-Path -leaf $_.PublicIpAddress.Id);$pip}} 
    	(Get-AzPublicIpAddress -Name $pip).IpAddress
    	
    }
    Hope this helps!

    Wednesday, July 24, 2019 2:06 PM
  • For some reason it has error with $pip argument..

    Get-AzPublicIpAddress : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, 
    and then try the command again.
    At C:\DEV\Scripts\Powershell\GetAzureObjects.ps1:20 char:31
    +     (Get-AzPublicIpAddress -Name $pip).IpAddress
    +                                  ~~~~
        + CategoryInfo          : InvalidData: (:) [Get-AzPublicIpAddress], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Network.GetAzurePublicIpAddressCommand

    None of the VNet objects have public IP's.  Would this cause error?  How do you show VNet private IP address?

     
    Thursday, July 25, 2019 1:08 AM
  • Hi scott,

    Please use the below script:

    $vnet= Get-AzVirtualNetwork -Name <vnet_name>
    
    $subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet
    
    $Ids = $subnet.IpConfigurations.Id
    
    foreach ( $Id in $Ids)
    {
    	$nic=$Id.split("/")[8]
    	
    	$vmName = Split-Path -leaf (Get-AzNetworkInterface -name $nic).VirtualMachine.Id
    	
    	Write-output "VM Name: " $vmName
    	
    	Get-AzNetworkInterface -name $nic | Get-AzNetworkInterfaceIpConfig | Select-Object -Property PrivateIpAddress 
    	
    	$nicObj = Get-AzNetworkInterface -name $nic
    	
    	$pip = Split-Path -leaf (Get-AzNetworkInterfaceIpConfig -NetworkInterface $nicObj).PublicIpAddress.Id
    	
    	if ($pip)
    	{
    		Write-output "Public Ip Address: " (Get-AzPublicIpAddress -Name $pip).IpAddress
    	}
    }

    Thanks!

    Thursday, July 25, 2019 10:22 AM