Sunday 15 May 2016

Use PowerShell to Create an Azure VM report.

Hi,

This post is to simplify things on getting Azure classic VM report in a nice format. I wrote PowerShell script to collect azure VM information in html format. 




You can download script from here.
https://onedrive.live.com/redir?resid=2976E7AE701BC07C!158&authkey=!AE-BWeCcsiGHwvo&ithint=file%2cps1 

 Make sure you have Azure PowerShell module installed.


1. To check the module


Get-Module -ListAvailable

You should be able to see Azure module
ModuleType Version    Name                                ExportedCommands                                                                                                                                  
---------- -------    ----                                ----------------                                                                                                                                  
Manifest   1.2.3      Azure                               {Disable-AzureServiceProjectRemoteDesktop, Enable-AzureServiceProjectRemoteDesktop, Get-AzureServiceProjectRoleRuntime, Save-AzureServiceProject...


2. Login to Azure classic subscription.


Run
Add-AzureAccount


You will be popped up with login windows. Provide your Credential




You will see your Azure subscription ID and tenants ID.


3. Select-AzureSubscription -SubscriptionName 'your sub. name'

     4. Before extracting VM report you will have to know the name of your cloud service.
    Check your cloud service you want to take VM report from.



     5. Create folder structure C:\temp\Reports\Azure_VM_Info” this is the location where will we save your report.


6. Change you cloud service name and run your PowerShell command to get the nice report.

$cloudsname = 'Your azure cloud service name'

$getvm = Get-AzureVM -ServiceName $cloudsname | select -Property Name,IpAddress,InstanceSize,Status | ConvertTo-Html -Fragment


$getvm = $getvm | foreach {

      $_ -replace "<td>ReadyRole</td>","<td style='color:green'>Running</td>"

}


$getvm = $getvm | foreach {

      $_ -replace "<td>StoppedDeallocated</td>","<td style='color:red'>Stopped</td>"

      }



$date = Get-Date -UFormat "%m-%d-%y"
$filename = "AzureVM_Info"+"$date"


$HTML = ConvertTo-Html -Body "<H2> AzureVMInfo - </H2>", "$getvm" -Head "<style> body {background-color: lightblue; } table {background-color: white; margin 5px; float: left; top: 0px; display: inline-block; padding: 5px; border: 1px solid black} tr:nth-child(odd) {background-color: lightgray} </style>"

$HTML | Out-File C:\temp\Reports\Azure_VM_Info\$filename.html

Invoke-Item C:\temp\Reports\Azure_VM_Info\$filename.html




Hope this post will be informative to you.

“Have a nice day”

Digvijay


No comments:

Post a Comment