Thursday 2 February 2017

Windows Azure Pack: Subscription usages info through PowerShell

Introduction:

Windows Azure pack is a great tool for your Private cloud. It brings the ability to run Azure services inside your own environment. It gives ability to use subscription based access and control like Azure public cloud does.
Since WAP uses plan and subscription based concept for private cloud resource. It’s very important to keep a track of usages or each subscription.
 This Article will help you to generate a nice report about the usage of each subscription.  It’ll also give useful information’s such as co-admin details, subscription status, sync status, creation date etc.
So let’s go it into the detail and see how we do that.
 
Prereqs.
Import MgmtSvcAdmin PowerShell module. Follow this url if you don’t have module installed
(https://technet.microsoft.com/en-in/library/dn282110.aspx )
Access to the WAP admin API
Address of WAP Admin API (https://ServerName:30004)
At least 1 plan/subscription configured in WAP for trial purpose

PowerShell commends:

you can download contents from here.  


  $AdminUri = 'https://ServerName  :30004'
   $AuthSiteUri = 'https://ServerName  :30072'
   $ClientRealm = 'http://azureservices/AdminSite  '
  
  $token = Get-MgmtSvcToken -Type Windows -AuthenticationSite $AuthSiteUri -ClientRealm $ClientRealm -DisableCertificateValidation
   $allSubscription = Get-MgmtSvcSubscription -Token $token -AdminUri $AdminUri -DisableCertificateValidation
$a = "<style>"
$a = $a + "BODY{}"
$a = $a + "TABLE{border-collapse: collapse;width: 100%;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: #ddd;padding: 10px;text-align: left;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: #ddd;padding: 10px;text-align: left;}"
$a = $a + "table tr:nth-child(odd) {background-color: #f2f2f2;}"
$a = $a + "TABLE tr td table{width: 50%;}"
$a = $a + "TABLE tr td table th{border: 1px solid #ddd;}"
$a = $a + "TABLE tr td table td{border: 1px solid #ddd;}"
$a = $a + "</style>"
 
 
$body = "<H2>Windows Azure Pack Subscription Information</H2><table><colgroup><col/><col/><col/></colgroup><tr><th>Subscription Name</th><th>Subscription ID</th><th>Status</th><th>Created Date</th><th>Admin</th><th>Sync Status</th></tr>"
 
foreach($Subscription in $allSubscription){
    $SubUsage = Get-MgmtSvcSubscriptionUsage -SubscriptionId $Subscription.SubscriptionID -AdminUri $AdminURI -Token $token -DisableCertificateValidation
    $SubUsage.RetrievedSuccessfully
 
    $body = $body + "<tr><td>"+$Subscription.SubscriptionName+"</td><td>"+$Subscription.SubscriptionID+"</td><td>"+$Subscription.State+"</td><td>"+$Subscription.Created+"</td><td>"+$Subscription.CoAdminNames+"</td><td>"+$SubUsage.RetrievedSuccessfully+"</td></tr>"
    $body = $body + "<tr><td colspan=""6"">Usage Information<br><br><table><tr><th>Service Type</th><th>Usage</th><th>Total Limit</th><th>Unit</th></tr>"
 
    $SubUsageList =  $SubUsage.Usages
    foreach($Usage in $SubUsageList){
        $body = $body + "<tr><td>"+$Usage.DisplayName+"</td><td>"+$Usage.CurrentValue+"</td><td>"+$Usage.Limit+"</td><td>"+$Usage.UnitDisplayName+"</td></tr>"
    }
    $body = $body + "</table></td></tr>"
}
$body = $body + "</table>"
 
ConvertTo-HTML -head $a -Body $body | Out-File "$env:USerProfile\desktop\WAP-Sub-Usages.html"
  
 
Invoke-Expression "$env:USerProfile\desktop\WAP-Sub-Usages.html"



Output

I’ll now explain more into technical detail of it.
Addresses and URI of require components.
$AdminUri = 'https://AdminAPIServer  :30004'
$AuthSiteUri = 'https://WindowsAuthSiteServer  :30072'


Note: You may have above components installed in different server in distributed environment.
You can use below command to see the address.
Get-MgmtSvcEndpoint | Format-Table -Property Name,Address


Generate a token:

The Get-MgmtSvcToken creates an identity token. Tokens are used by several of the Windows Azure Pack for Windows Server cmdlets. You can create a token and store it in a variable for use with other cmdlets. Find WAP subscription and take each for taking detailed information.

$token = Get-MgmtSvcToken -Type Windows -AuthenticationSite $AuthSiteUri -ClientRealm $ClientRealm -DisableCertificateValidation


I’m not expert in html code so I took help from my buddy "Tapan Gupta". However I am using html as an output for making it presentable. Hence you need listed codes.

You can modify it as per your requirement.

$a = "<style>"
$a = $a + "BODY{}"
$a = $a + "TABLE{border-collapse: collapse;width: 100%;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: #ddd;padding: 10px;text-align: left;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: #ddd;padding: 10px;text-align: left;}"
$a = $a + "table tr:nth-child(odd) {background-color: #f2f2f2;}"
$a = $a + "TABLE tr td table{width: 50%;}"
$a = $a + "TABLE tr td table th{border: 1px solid #ddd;}"
$a = $a + "TABLE tr td table td{border: 1px solid #ddd;}"
$a = $a + "</style>"
 
$body = "<H2>Subscription Information</H2><table><colgroup><col/><col/><col/></colgroup><tr><th>Subscription Name</th><th>Subscription ID</th><th>Status</th><th>Created Date</th><th>Admin</th><th>Sync Status</th></tr>"


Now it’s time for us to take each subscription one by one and have detailed information.
I’m using foreach loop to collect usages information’s.

foreach($Subscription in $allSubscription){
    $SubUsage = Get-MgmtSvcSubscriptionUsage -SubscriptionId $Subscription.SubscriptionID -AdminUri $AdminURI -Token $token -DisableCertificateValidation
    $SubAdmin = ""
    if($Subscription.CoAdminNames.Count -gt 0){
        $SubAdmin = $Subscription.CoAdminNames.Item(0)
    }
 
    $body = $body + "<tr><td>"+$Subscription.SubscriptionName+"</td><td>"+$Subscription.SubscriptionID+"</td><td>"+$Subscription.State+"</td><td>"+$Subscription.Created+"</td><td>"+$SubAdmin+"</td><td>"+$SubUsage.RetrievedSuccessfully+"</td></tr>"
    $body = $body + "<tr><td colspan=""6"">Usage Information<br><br><table><tr><th>Service Type</th><th>Usage</th><th>Total Limit</th><th>Unit</th></tr>"
 
    $SubUsageList =  $SubUsage.Usages
    foreach($Usage in $SubUsageList){
        $body = $body + "<tr><td>"+$Usage.DisplayName+"</td><td>"+$Usage.CurrentValue+"</td><td>"+$Usage.Limit+"</td><td>"+$Usage.UnitDisplayName+"</td></tr>"
    }
    $body = $body + "</table></td></tr>"
}
$body = $body + "</table>"



Final step is to convert output data and feed this informations to the html page which we created earlier.

ConvertTo-HTML -head $a -Body $body | Out-File "$env:USerProfile\desktop\WAP-Sub-Usages.html"
 Invoke-Expression "$env:USerProfile\desktop\WAP-Sub-Usages.html"


This script will list out all subscription and give the informations back in html format.


I hope this post will be informative.


Thanks

Digvijay Singh