Monday, May 25, 2020

Azure - Custom Power-Shell Script for Azure VM's Inventory

Below is the custom script to get Azure VM's inventory.



clear
$password = ConvertTo-SecureString 'PutPassword -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('PutLoginName', $password)

Login-AzureRmAccount -Credential $cred

$AZSub=Get-AzureRmSubscription


$Cdate = Get-Date

$text="<html>"
$text= $text + "<head>"
$text= $text + "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
$text= $text + "<title>O365 License Report</title>"
$text= $text + "</head>"
$text= $text + "<body><br>"
$text= $text + "<table width='50%' cellpadding=0 cellspacing=0 style='border:1px solid #000'>"
$text= $text + "<tr>"
$text= $text + "<td colspan='9' height='25' align='center'>"
$text= $text + "<font face='tahoma' color='#003399' size='4'><strong>Azure VM's Inventory - $Cdate </strong></font>"
$text= $text + "</td>"
$text= $text + "</tr>"
#$text= $text + "</table >"
#$text= $text + "<table width='50%' cellpadding=0 cellspacing=0 style='border:1px solid #000'>"
$text= $text + "<tr>"
$text= $text + "<td width='20%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>Subscription</td>"
$text= $text + "<td width='20%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>Name</td>"
$text= $text + "<td width='20%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>ResourceGroupName</td>"
$text= $text + "<td width='30%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>VmID</td>"
$text= $text + "<td width='20%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>Location</td>"
$text= $text + "<td width='20%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>ProvisioningState</td>"
$text= $text + "<td width='10%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>StatusCode</td>"
$text= $text + "<td width='10%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px; font-weight:bold'>PowerStatus</td></tr>"

foreach ($AZSub1 in $AZSub)
{


 
   if ($AZSub1.Name -eq 'Chennai-DC' -or $AZSub1.Name -eq 'Birlasoft Azure - ADFS')
    {
       $AZSubName = $AZSub1.Name
       Select-AzureRmSubscription -SubscriptionName $AZSub1.Name

       $VM=Get-AzureRmVM -Status | select PowerState,ResourceGroupName,VmId,Name,Location,ProvisioningState,StatusCode
       foreach ($vm1 in $vm)
        {
       $VMName=$vm1.Name
       $RSGName=$vm1.ResourceGroupName
       $VMid=$vm1.VmId
       $VMLoc=$vm1.Location

       $ProState=$vm1.ProvisioningState
       $VmStatus=$vm1.StatusCode
       $PState=$vm1.PowerState
       $text= $text + "<tr>"
        $text= $text + "<td width='20%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$AZSubName</td>"
$text= $text + "<td width='20%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$VMName</td>"
$text= $text + "<td width='10%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$RSGName</td>"
        $text= $text + "<td width='30%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$VMid</td>"
        $text= $text + "<td width='10%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$VMLoc</td>"

        $text= $text + "<td width='10%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$ProState</td>"
        $text= $text + "<td width='10%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$VmStatus</td>"
        $text= $text + "<td width='10%' align='center' style='border: 1px solid black;padding: 5px; font-family:tahoma;font-size:14px;'>$PState</td>"
        $text= $text + "</tr>"
        }


    }
   
}

$text= $text + "</body>"
$text= $text + "</html>"

$text | Set-Content 'C:\Users\sunilkumag\Desktop\AzureInventory.html'