My team had a few Azure projects in the past couple of weeks and as for every good project, we had to create a documentation.

This gave me an opportunity to write a script (ended as a function) that we could use in the future.

Let’s see what we get out-of-the-box. If we run “Get-AzureRMVM” the output would look similar to this:

 

You are guessing – that is not all we wanted to have in documentation 🙂

Besides all this, we wanted to see VM IPs – both Public and Private, Availability Set (we always create those), Virtual Network that VM belongs to, Subnet and Network Security Group.

I will not bother you with the investigation part, but please read the script carefully (NOTES) as you will see how to grab the info you need. That way you will easily add/change what you need.

And remember – “Get-Member” is your big friend 🙂

Function Get-AzureRMVMData {<br />
<#<br />
.Synopsis<br />
   Gets the info from Azure VMs.<br />
.DESCRIPTION<br />
   Gets the info from Azure VMs. It supports a pipeline input.<br />
.EXAMPLE<br />
   Get-AzureRMVMData # This will extract all VMs from the subscription<br />
.EXAMPLE<br />
   Get-AzureRMVMData -ResourceGroupName # This will extract all VMs from the specified Resource Group<br />
.EXAMPLE<br />
    "ResourceGroupName" | GetAzureRMVMData # This will extract all VMs from the specified Resource Group<br />
.EXAMPLE<br />
    (Get-AzureRmResourceGroup).ResourceGroupName | GetAzureRMVMData<br />
.NOTES<br />
    These are all objects that were initialy used and then implemented in to script</p>
<p>    #$NICObj = Get-AzureRmNetworkInterface | Where-Object {$_.id -eq $vm.NetworkProfile.NetworkInterfaces.id}<br />
    #$NSGObj = Get-AzureRmNetworkSecurityGroup | Where-Object {$_.id -eq $NSGid}<br />
    #$vNetObj = (Get-AzureRmVirtualNetwork | Where-Object {$_.subnets.id -eq $VNetworkSubnetID})<br />
    #$PIPobj = Get-AzureRmPublicIpAddress | Where-Object {$_.id -eq $PIPid}<br />
    #$subnetobj = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnetobj | Where-Object {$_.id -eq $VNetworkSubnetID}<br />
    #$OSTypeObj = $vm.StorageProfile.OsDisk<br />
    #$AVSetObj = Get-AzureRmAvailabilitySet | Where-Object {$_.id -eq $AVSetID}<br />
.FUNCTIONALITY<br />
    Azure<br />
#></p>
<p>    [CmdletBinding()]<br />
    param (<br />
        [Parameter(ValueFromPipeline=$true,<br />
                   ValueFromPipelineByPropertyName=$true)]<br />
        [Alias("ResourceGroupName")]<br />
        $Name = (Get-AzureRmResourceGroup).ResourceGroupName<br />
    )</p>
<p>    BEGIN<br />
    {<br />
        $Outputs = @()<br />
        $WarningPreference = "SilentlyContinue"<br />
    }<br />
    PROCESS<br />
    {</p>
<p>        foreach($ResourceGroup in $Name)<br />
        {<br />
            Try # If there are no VMs, RG will be skipped<br />
                {<br />
                    Write-Verbose "Processing $ResourceGroup"<br />
                    $VMs = @( Get-AzureRMVm -ResourceGroupName $ResourceGroup -ErrorAction Stop)<br />
                }<br />
            Catch<br />
                {<br />
                    #create an error message<br />
                    $errormsg="Failed retrieving VMs from $ResourceGroup. $($_.Exception.Message)"<br />
                    $errorlog = Join-Path $env:temp 'errors.txt'<br />
                    Write-Error $errormsg<br />
                    Write-Verbose "Logging errors to $errorlog"<br />
                    "$(Get-Date): $errormsg" | Out-File -FilePath $Errorlog -Append<br />
                    continue<br />
                }<br />
            foreach ($VM in $VMs){<br />
                #Name for report<br />
                $VMName = $VM.Name</p>
<p>                #RG Name for report<br />
                $RGName = $VM.ResourceGroupName</p>
<p>                $NSGid = (Get-AzureRmNetworkInterface | Where-Object {$_.id -eq $VM.NetworkProfile.NetworkInterfaces.id}).NetworkSecurityGroup.Id</p>
<p>                #NSG Name for report<br />
                $NSGName = (Get-AzureRmNetworkSecurityGroup | Where-Object {$_.id -eq $NSGid}).Name</p>
<p>                $NICObj = Get-AzureRmNetworkInterface | Where-Object {$_.id -eq $vm.NetworkProfile.NetworkInterfaces.id}<br />
                $VNetworkSubnetID = $NICObj.IpConfigurations.subnet.id</p>
<p>                #vNet name for report<br />
                $vNetName = (Get-AzureRmVirtualNetwork | Where-Object {$_.subnets.id -eq $VNetworkSubnetID}).name</p>
<p>                #Private IP for report<br />
                $IP = ((Get-AzureRmNetworkInterface | Where-Object {$_.id -eq $vm.NetworkProfile.NetworkInterfaces.id}).IpConfigurations).PrivateIpAddress</p>
<p>                $PIPid = (Get-AzureRmNetworkInterface | Where-Object {$_.id -eq $vm.NetworkProfile.NetworkInterfaces.id}).IpConfigurations.publicipaddress.Id</p>
<p>                #Public IP for report<br />
                $PIP = (Get-AzureRmPublicIpAddress | Where-Object {$_.id -eq $PIPid}).IpAddress</p>
<p>                #Subnet name for report<br />
                $vNetObj = (Get-AzureRmVirtualNetwork | Where-Object {$_.subnets.id -eq $VNetworkSubnetID})<br />
                $SubnetName = (Get-AzureRmVirtualNetworkSubnetConfig  -VirtualNetwork $vNetObj | Where-Object {$_.id -eq $VNetworkSubnetID}).Name</p>
<p>                $AVSetID = $vm.AvailabilitySetReference.id</p>
<p>                #AVSet name for report<br />
                $AVSetName = (Get-AzureRmAvailabilitySet | Where-Object {$_.id -eq $AVSetID}).Name</p>
<p>                #OS Type for report<br />
                $OSType = ($vm.StorageProfile.OsDisk).OsType</p>
<p>                $Output = New-Object PSObject -property @{<br />
                    Hostname = $VMName<br />
                    AVSet = $AVSetName<br />
                    OS = $OSType<br />
                    PublicIP = $PIP<br />
                    PrivateIP = $IP<br />
                    vNetwork = $vNetName<br />
                    Subnet = $SubnetName<br />
                    NSG = $NSGName<br />
                    ResourceGroup = $RGName<br />
                } | Select-Object Hostname, AVSet, OS, PublicIP, PrivateIP, vNetwork, Subnet, NSG, ResourceGroup # This line orderes the table the way we want<br />
                $Outputs += $Output<br />
            } # Close foreach VM<br />
        } # Close foreach RG<br />
    } # Close PROCESS</p>
<p>    END<br />
    {<br />
        [pscustomobject]$Outputs | Out-GridView<br />
    }<br />
} # Close function<br />

After running this script you will get an on-screen report like this:

 

We use it to C/P to our Wiki documentation, but you can easily output the report to Excel.

Have another great day in Admins life 🙂

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.