You've successfully subscribed to Florin Loghiade
Great! Next, complete checkout for full access to Florin Loghiade
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.

PowerTip - Get a nicely formatted endpoint list for your Azure VMs

in

I was working on an Azure deployment recently and I needed a very quick way to get all the endpoints and local IP from the Azure VMs, rather than clicking threw the portal and copying the endpoints one by one so I opened up the PowerShell ISE, struggled a bit with the Azure cmdlets and presto, got a nice one liner that gives me all the information I need.

Here is is:

Get-AzureVM -ServiceName $ServiceName -PipelineVariable AzureVM | Get-AzureEndpoint | Select-Object @{n='VMName'; e={($AzureVM).InstanceName}},@{n='IPAddress'; e={$AzureVM.IpAddress}}, @{n='ServiceDNS'; e={($AzureVM).DNSName}}, Name, Port | Format-Table -AutoSize

And the output should be something like this:

VM-Format

Note: My VMs were offline while I was writing this blog post, that’s why the IPAddress column is not populated

Now that we have a working one liner, let’s break it up on multiple lines to read it better:

Get-AzureVM -ServiceName $ServiceName -PipelineVariable AzureVM |
Get-AzureEndpoint |
Select-Object -Property @{
    n = 'VMName'
    e = {
        ($AzureVM).InstanceName
    }
}, @{
    n = 'IPAddress'
    e = {
        $AzureVM.IpAddress
    }
}, @{
    n = 'ServiceDNS'
    e = {
        ($AzureVM).DNSName
    }
}, Port, Name |
Format-Table -AutoSize

So we are getting all the VMs that are in a service then we are defining a variable named ‘AzureVM’ that will contain all the returned objects from the Get-AzureVM command, then piping the output to Get-AzureEndpoint, and piping that output to the Select-Object command where I opened up three hash tables to create some calculated properties based on my needs then piped all that output to Format-Table and got everything that I needed in the form I needed.

Now in order to get some output from those calculated properties I needed the VM names and that information doesn’t get to the second pipeline thus the need for a PipelineVariable.

Happy PowerShelling!