SOLVED

Do you have any faster ps script to get azure private ip address?

Iron Contributor

Hi Community,

 

Do you have any faster ps script to get azure private ip address?

Extracting too many vms makes this script slow. Thanks.

 

My Code.

 

$nin = (Get-AzVM -Name "VMNAME").NetworkProfile.NetworkInterfaces.Id.Split("/")|Select -Last 1

$ips = (Get-AzNetworkInterface -Name $nin).IpConfigurations.PrivateIpAddress

$ips

5 Replies
best response confirmed by Alan2022 (Iron Contributor)
Solution

@Alan2022 

 

Hi, Alan.

 

Try the following.

 

It's not necessarily that much better as you are forced to do client-side filtering for IP information (at least with these commandlets) but it should be a bit faster through not having to make two REST calls (one for fetching the VM data, and a second for the interface data.)

 

Get-AzNetworkInterface | Where-Object { $_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress } | Select-Object -Property @{n="Name"; e={ $_.VirtualMachine.Id.Split("/")[-1] }}, @{n="PrivateIPAddress"; e= { $_.IpConfigurations.PrivateIPAddress }}

 

Cheers,

Lain

@LainRobertson

Hi Lain,

I tried your code it was really fast i was amazed. But its hard to read it. If i want to filter by the VM ResourceGroupName where should i do this? Thanks.
@LainRobertson

Hi Lain,

I get it just add it in the Where-Object.

Get-AzNetworkInterface | Where-Object { $_.ResourceGroupName -Match "ResourceGroupName" -and $_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress }

Thank you very much.
Keep up the good job. .☺☺☺

@Alan2022 

 

So, there's two paths you can take with the resource group question:

 

  1. Use the -ResourceGroup parameter on Get-AzNetworkInterface; or
  2. Use client-side filtering on the ResourceGroup parameter.

 

They are both useful in different circumstances.

 

Using the -ResourceGroup parameter

This is useful if your script is only going to work with this one specific resource group.

 

The benefit to this approach is that the filtering happens server-side making it faster to run.

 

Here's an example:

 

Get-AzNetworkInterface -ResourceGroup "SomeRgName" | Where-Object { $_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress } | Select-Object -Property ResourceGroupName, @{n="Name"; e={ $_.VirtualMachine.Id.Split("/")[-1] }}, @{n="PrivateIPAddress"; e= { $_.IpConfigurations.PrivateIPAddress }}

 

 

Using client-side filtering

Client-side filtering is useful where you may be looking to work with multiple resource groups, even if that's doing so one resource group at a time.

 

In this scenario, you might be better off pulling all the interfaces down locally once and assigning them to a variable, then repeatedly using client-side filtering so that you're not making unnecessary calls back to Azure.

 

Client-side filtering can be done a number of different ways. Here's two examples:

 

Filtering on the original call to Azure

 

Get-AzNetworkInterface | Where-Object { ($_.ResourceGroupName -in @("RgName01", "RgName02", "RgName03")) -and  ($_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress) } | Select-Object -Property ResourceGroupName, @{n="Name"; e={ $_.VirtualMachine.Id.Split("/")[-1] }}, @{n="PrivateIPAddress"; e= { $_.IpConfigurations.PrivateIPAddress }}

 

 

Assigning to a variable then filtering later

 

$PrivateIPAddressInfo = Get-AzNetworkInterface | Where-Object { $_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress } | Select-Object -Property ResourceGroupName, @{n="Name"; e={ $_.VirtualMachine.Id.Split("/")[-1] }}, @{n="PrivateIPAddress"; e= { $_.IpConfigurations.PrivateIPAddress }};

$PrivateIPAddressInfo | Where-Object { $_.ResourceGroupName -in @("RgName01", "RgName03") } | ForEach-Object {
   # Do stuff.
}

$PrivateIPAddressInfo | Where-Object { $_.ResourceGroupName -eq "RgName02" } | ForEach-Object {
   # Do different stuff.
}

 

 

This approach means you can keep re-using the data from the variable, which - as mentioned before - avoids having to make multiple calls to Azure, though it only makes sense where the variable's used a lot (my example is too basic to benefit from doing this.)

 

Cheers,

Lain

 

Edited for spelling and formatting.

@LainRobertson

Hi Lain,

Thank you very much.
I'm learning new technics again.
Good job & Have a nice day. ☺

1 best response

Accepted Solutions
best response confirmed by Alan2022 (Iron Contributor)
Solution

@Alan2022 

 

Hi, Alan.

 

Try the following.

 

It's not necessarily that much better as you are forced to do client-side filtering for IP information (at least with these commandlets) but it should be a bit faster through not having to make two REST calls (one for fetching the VM data, and a second for the interface data.)

 

Get-AzNetworkInterface | Where-Object { $_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress } | Select-Object -Property @{n="Name"; e={ $_.VirtualMachine.Id.Split("/")[-1] }}, @{n="PrivateIPAddress"; e= { $_.IpConfigurations.PrivateIPAddress }}

 

Cheers,

Lain

View solution in original post