Skip to content

Instantly share code, notes, and snippets.

@jeffpatton1971
Last active November 30, 2015 20:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jeffpatton1971/917f42a589cafbf1a5e5 to your computer and use it in GitHub Desktop.
This script will return the assigned private ips of each machine in a given subnet. If no subnet is provided it will return the assigned private ips for every machine in all subnets with a configuration.
Param
(
[Parameter(Mandatory=$true)]
[string]$VirtualNetworkName,
[Parameter(Mandatory=$false)]
[string]$SubnetName = '',
[Parameter(Mandatory=$true)]
[string]$Location,
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName
)
Begin
{
$ResourceGroup = Get-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
$VirtualNetwork = Get-AzureRmVirtualNetwork -Name $VirtualNetworkName -ResourceGroupName $ResourceGroup.ResourceGroupName
}
Process
{
foreach ($SubnetConfig in (Get-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $VirtualNetwork))
{
if ($SubnetConfig.IpConfigurations)
{
foreach ($IpConfiguration in $SubnetConfig.IpConfigurations)
{
if ($IpConfiguration.Id.Contains('loadBalancers'))
{
$LoadBalancer = Get-AzureRmLoadBalancer -Name $IpConfiguration.Id.Split('/')[8] -ResourceGroupName $IpConfiguration.Id.Split('/')[4]
New-Object -TypeName psobject -Property @{
Name = ($LoadBalancer.Name)
Type = 'LoadBalancer'
PrivateIpAddress = ($LoadBalancer.FrontendIpConfigurations |ForEach-Object {$_.PrivateIpAddress})
}
}
else
{
$NetworkInterfaceName = $IpConfiguration.Id.Split('/')[8]
$NetworkInterfaceResourceGroupName = $IpConfiguration.Id.Split('/')[4]
$NetworkInterface = Get-AzureRmNetworkInterface -Name $NetworkInterfaceName -ResourceGroupName $NetworkInterfaceResourceGroupName
New-Object -TypeName psobject -Property @{
Name = ($NetworkInterface.VirtualMachine.Id.Split('/')[8])
Type = 'VirtualMachine'
PrivateIpAddress = ($NetworkInterface.IpConfigurations |ForEach-Object {$_.PrivateIpAddress})
}
}
}
}
else
{
}
}
}
End
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment