Forum Discussion
Do you have any faster ps script to get azure private ip address?
- May 11, 2022
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
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.
So, there's two paths you can take with the resource group question:
- Use the -ResourceGroup parameter on Get-AzNetworkInterface; or
- 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.
- Alan2022May 12, 2022Iron ContributorLainRobertson
Hi Lain,
Thank you very much.
I'm learning new technics again.
Good job & Have a nice day. ☺