This article details a PowerShell runbook designed to run in Azure Automation. Its purpose is to retrieve license usage information from Application Workspace and upload it to an Azure Storage Account.
Exported file contains the following info:
PurchasedType
: The type of license purchased.
BillableCount
: The number of licenses that are billable.
PurchasedCount
: The total number of licenses purchased.
ExcludedCount
: Licenses excluded from billing.
OverUsage
: Instances where usage exceeds the purchased licenses.
BillableRefreshedAt
: The last refresh of the billable data.
The path to the exported file of the script is C:\Users\<YourUsername>\AppData\Local\Temp
What the script does
-
Set Variables:
- Defines Azure resource names (resource group, storage account, container).
- Sets the temporary folder using $env:Temp.
- Generates a filename based on the current date.
- Specifies Azure Key Vault secrets for credentials and URL.
-
Disable Azure Context Autosave:
- Prevents inheriting an existing Azure context in the runbook.
-
Authenticate with Azure:
- Uses Managed Identity to connect to Azure.
- Retrieves secrets (username, password, URL) from Azure Key Vault.
-
Connect to Application Workspace:
- Creates a credential object.
- Connects to the Application Workspace System using the retrieved credentials and URL.
-
Export License Info:
- Calls
Get-LiquitZone
to retrieve zone info. - Extracts the license data and exports it to a CSV file in the temp folder.
- Calls
-
Upload to Azure Blob Storage:
- Gets the storage account context.
- Uploads the CSV file to the specified container in Azure Blob Storage.
- Includes error handling in case the upload fails.
The script
Expand to show script
# Variables
# Prevent inheriting an AzContext in your runbook
Disable-AzContextAutosave -Scope Process | Out-Null
# Retrieving credentials with Azure Key Vault
Connect-AzAccount -Identity
$Key = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name
# Connect to Liquit Workspace
$Credentials = New-Object System.Management.Automation.PSCredential
Connect-LiquitWorkspace -URI $url -Credential $credentials
# Get License Info
# Send it to Azure Storage Account
$StorageAccount = Get-AzStorageAccount -Name $StorageAccount -ResourceGroupName $ResourceGroupName
try {
$null = Set-AzStorageBlobContent -File $TempFolder$CSVFileName -Container $Container -Blob $CSVFileName -Context $StorageAccount.Context -Force -ErrorAction Stop
}
catch {
Write-Error -Exception $_ -Message "Failed to upload $CSVFileName to Azure Blob Storage"
}
Further reading
Source article written by Donny van der Linde
Microsoft Azure Automation documentation
Microsoft Azure Blob Storage Documentation
Microsoft Azure Key Vault documentation