May 10 2022 10:59 PM
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
May 11 2022 02:50 AM
Solution
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
May 11 2022 04:55 PM
May 11 2022 05:03 PM
May 11 2022 05:16 PM - edited May 11 2022 05:19 PM
So, there's two paths you can take with the resource group question:
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.
May 11 2022 05:22 PM
May 11 2022 02:50 AM
Solution
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