Auflisten von HUB/BYOL VMs on Azure

Zunächst einmal, mit HUB (Azure Hybrid Use Benefit) und BYOL (Bring your own license) mehr Infos dazu unter Hybrid use Benefit

Kurz gesagt, durch das nutzen der eigenen bereits vorhandenen Lizenzen lässt sich eine Menge Geld sparen.
Andererseits hat man auch selbst die Wartung und Verwaltung der Images an der Backe, zumindest noch…
Nun ist es so, dass ich ggf. herausfinden möchte, welche meiner bereits vorhandenen VMs HUB nutzen und welche ich durch solche ersetzten könnte um Kosten einzusparen. Um genau das heraus zu finden kann ich folgendermaßen vorgehen und mir die Azure-Cmdlet mit PowerShell zu nutze machen.

Zunächst logge ich mich in meinen Azure Account ein.
Wenn ich mehr als eine Subscription habe, werde ich aufgefordert, für diese Session eine Default Subscription auszuwählen.

# To login to Azure Resource Manager
$SubscriptionTemp = Add-AzureRmAccount

# To select a subscriptions for your account
# If there is only one subscription, it will be selected
$m = Get-AzureRmSubscription |
Tee-Object -Variable SubscriptionTemp |
Measure-Object
if ($m.Count -gt 1)
{
  # To select a default subscription for your current session
  $SubscriptionTemp = $SubscriptionTemp |
  Out-GridView -PassThru -Title 'Select the Azure subscription you want to use:' |
  Set-AzureRmContext
}

Anschließend kann ich über folgenden Befehl, schön formatiert, alle meine VMs aus meiner Subscription auflisten und deren Provisioning State, sowie den Lizenz Status sehen.

$overallrecords = Get-AzureRmVM |
Select-Object -Property ResourceGroupName, Name, ProvisioningState, @{
  label      = 'HUB/BYOL'
  Expression = {
    $_.LicenseType -ne $Null -and $_.LicenseType -ne ''
  }
}

An sich wäre ich jetzt fertig und hätte eine schöne Auflistung meiner Azure VMs und deren Status in meiner $overallrecords Variable stehen.
Das ist bei einer Hand voll VMs auch schön und gut.
Wenn ich jetzt allerdings eine ganze VM Farm habe macht es durchaus Sinn, die Daten zu exportieren oder zumindest schöner auszugeben.
Das mache ich durch folgenden Code.

#Getting Output Options
$ExportOptions = $False
Do
{
  Clear-Host
  Out-Host -InputObject '-------------------------------------------------------'
  Out-Host -InputObject 'How would you like to see the results?'
  Out-Host -InputObject '1) Grid View'
  Out-Host -InputObject '2) CSV File'
  Out-Host -InputObject '3) Screen'
  Out-Host -InputObject 'STRG-C To Exit'
  $ExportOptions = Read-Host -Prompt 'Please Enter your choice'
  switch ($ExportOptions.ToUpper())
  {
    1 
    {
      Out-Host -InputObject 'You chose GridView'
      $overallrecords |
      Select-Object -Property * |
      Out-GridView -Title 'Get HUB/BYOL VMs'
    }
    2 
    {
      Out-Host -InputObject 'You chose CSV'
      #Define your FilePrefix
      $FilePrefix = 'AzureHUBVMs'
      $FileDate = '{0:yyyy_MM_dd-HH_mm}' -f (Get-Date)
      $FileName = $env:TEMP+'\'+$FilePrefix+$FileDate+'.csv'
      $overallrecords | Export-Csv -Path $FileName -NoTypeInformation
      & "$env:windir/explorer.exe" ('/select,{0}' -f $FileName)
    }
    3 
    {
      Out-Host -InputObject 'You chose Screen'
      $overallrecords |
      Format-Table -AutoSize
    }
    default 
    {
      $ExportOptions = $False
    }
  }
}
While (!$ExportOptions)

Über den Input wird abgefragt welche Ausgabe genutzt werden soll.
Beim CSV Export, werden die Daten in das Windows Temp Verzeichnis exportiert und dieses wird im Anschluss versucht zu öffnen.

Den Letzten Part kann man auch weglassen, macht die Übersicht der VMs aber wesentlich lesbarer.

Im ganzen sieht das Script dann so aus.

# To login to Azure Resource Manager
$SubscriptionTemp = Add-AzureRmAccount

# To select a subscriptions for your account
# If there is only one subscription, it will be selected
$m = Get-AzureRmSubscription |
Tee-Object -Variable SubscriptionTemp |
Measure-Object
if ($m.Count -gt 1)
{
  # To select a default subscription for your current session
  $SubscriptionTemp = $SubscriptionTemp |
  Out-GridView -PassThru -Title 'Select the Azure subscription you want to use:' |
  Set-AzureRmContext
}

$overallrecords = Get-AzureRmVM |
Select-Object -Property ResourceGroupName, Name, ProvisioningState, @{
  label      = 'HUB/BYOL'
  Expression = {
    $_.LicenseType -ne $Null -and $_.LicenseType -ne ''
  }
}

#Getting Output Options
$ExportOptions = $False
Do
{
  Clear-Host
  Out-Host -InputObject '-------------------------------------------------------'
  Out-Host -InputObject 'How would you like to see the results?'
  Out-Host -InputObject '1) Grid View'
  Out-Host -InputObject '2) CSV File'
  Out-Host -InputObject '3) Screen'
  Out-Host -InputObject 'STRG-C To Exit'
  $ExportOptions = Read-Host -Prompt 'Please Enter your choice'
  switch ($ExportOptions.ToUpper())
  {
    1 
    {
      Out-Host -InputObject 'You chose GridView'
      $overallrecords |
      Select-Object -Property * |
      Out-GridView -Title 'Get HUB/BYOL VMs'
    }
    2 
    {
      Out-Host -InputObject 'You chose CSV'
      #Define your FilePrefix
      $FilePrefix = 'AzureHUBVMs'
      $FileDate = '{0:yyyy_MM_dd-HH_mm}' -f (Get-Date)
      $FileName = $env:TEMP+'\'+$FilePrefix+$FileDate+'.csv'
      $overallrecords | Export-Csv -Path $FileName -NoTypeInformation
      & "$env:windir/explorer.exe" ('/select,{0}' -f $FileName)
    }
    3 
    {
      Out-Host -InputObject 'You chose Screen'
      $overallrecords |
      Format-Table -AutoSize
    }
    default 
    {
      $ExportOptions = $False
    }
  }
}
While (!$ExportOptions)