From 03f08104bf8804aebae3e85977592769118eee83 Mon Sep 17 00:00:00 2001 From: Alexander Nosov Date: Thu, 17 Dec 2020 19:15:32 -0800 Subject: [PATCH] Removed prompt --- samples/manage/azure-hybrid-benefit/README.md | 26 +++---- .../sql-license-usage.ps1 | 76 +++++++++---------- 2 files changed, 48 insertions(+), 54 deletions(-) diff --git a/samples/manage/azure-hybrid-benefit/README.md b/samples/manage/azure-hybrid-benefit/README.md index 5aefffa2..534478b1 100644 --- a/samples/manage/azure-hybrid-benefit/README.md +++ b/samples/manage/azure-hybrid-benefit/README.md @@ -3,13 +3,14 @@ services: Azure SQL platforms: Azure author: anosov1960 ms.author: sashan -ms.date: 12/15/2020 +ms.date: 12/17/2020 --- # Overview -This script is provided to help you manage the SQL Server licenses that are consumed by the SQL Servers deployed to Azure. The script's output is a `sql-license-usage_.csv` file with the consolidated SQL Server license usage by all SQL resources in the specific subscriptions or the entire account. By periodically generating the usage report you can track your license utilization over time. The usage is broken into the following license categories: +This script is provided to help you manage the SQL Server licenses that are consumed by the SQL Servers deployed to Azure. The script writes the results `sql-license-usage.csv` file with the consolidated SQL Server license usage by all SQL resources in the specific subscriptions or the entire account. If the file with this name already exists, the new results will be appended. The reports includes the following data for each scanned subscription and a total number of vCores in each category: +- Date and time of scan - AHB Standard vCores - AHB Enterprise vCores - PAYG Standard vCores @@ -24,12 +25,11 @@ This script is provided to help you manage the SQL Server licenses that are cons > - For IaaS workloads, such as SQL Server in Virtual Machines or SSIS integration runtimes, each vCPU is counted as one vCore. > - For PaaS workloads, each vCore of Business Critical service tier is counted as one Enterprise vCore and each vCore of General Purpose service tier is counted as one Standard vCore. - # Running the script using Cloud Shell Use the following steps to calculate the SQL Server license usage: -1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs). +1. Launch the [Cloud Shell](https://shell.azure.com/). For details, read [PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs). 2. Upload the script to the shell using the following command: @@ -37,27 +37,21 @@ Use the following steps to calculate the SQL Server license usage: curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 -o sql-license-usage.ps1 ``` -3. Run the script in interactive mode. The script will prompt for a subscriptions ID or `*`. The latter will automatically scan all the subscriptions in you account. +3. Run the script with a specific subscriptions ID or the file name as the parameter. The file should be used if you need to scan a subset of the subscriptions. If the parameter is not specified, the script will scan all the subscriptions in your account. ```console - ./sql-license-usage.ps1 + ./sql-license-usage.ps1 or .csv ``` -If you need to scan a subset of the subscriptions, use the following steps: - -1. Create a `.csv` with the list off all subscriptions in your account using the following command. You can edit the file to remove the subscriptions you don't want to scan. +If the a file is specified, it must be a `.csv` file with the list of subscriptions. To create a file containing all subscriptions in your account, use the following command. You can then edit the file to remove the subscriptions you don't want to scan. ```console Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation ``` - -2. Run the script and specify the `.csv` file as a parameter. - ```console - ./sql-license-usage.ps1 .\mysubscriptions.csv - ``` - > [!NOTE] > - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS. > - The `curl` command will copy the script directly to the home folder associated with your Cloud Shell session. -> - The script will prompt for the resource group name and print a message when migration is completed. +# Tracking SQL license usage over time + +You can track your license utilization over time by periodically running this script. Each new scan will add the results to `sql-license-usage.csv`, which you can use for reporting the license usage over time in Excel or other tools. To this script on schedule using Azure automation, read [Create a PowerShell runbook tutorial](https://docs.microsoft.com/azure/automation/learn/automation-tutorial-runbook-textual-powershell). diff --git a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 index 92c35a02..24452f64 100644 --- a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 +++ b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 @@ -14,52 +14,49 @@ # # Sample script to calculate the consolidated SQL Server license usage by all of the SQL resources in a specific subscription or the entire the account. # -# This script accepts a .csv file as a parameter, which provides a list of subscriptions to be scanned for the license usage. You can create +# This script accepts a .csv file or a subscription ID as a parameter. The file must include a list of subscriptions to be scanned for the license usage. You can create # such a file by the following command and then edit to remove the subscriptions you don't want to scan: # > Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation # -# If no file is provided, the script will prompt for a subscriptiobn ID or `*`. The latter will automatically scan all the subscriptions you account +# If no parameter is provided, the script will prompt for a file or subscriptiobn ID. If no value is provided, the scipt will scan all the subscriptions you account # has access to. # # # NOTE: The script does not calculate usage for Azure SQL resources that use the DTU-based purchasing model # -# Import the subscription info +# Subscriptions to scan -if ($args[0] -ne $null) { - # Read subscription list from the .csv file +if ($args[0] -like "*.csv") { $subscriptions = Import-Csv $args[0] -} else { - # Promt for the subscription - $Id = read-host -Prompt "Enter Subscription ID" - if ($Id -eq "*") { - $subscriptions = Get-AzSubscription - } else { - $subscriptions = [PSCustomObject]@{SubscriptionId = $Id} | Get-AzSubscription - } +}elseif($args[0] -ne $null){ + $subscriptions = [PSCustomObject]@{SubscriptionId = $args[0]} | Get-AzSubscription +}else{ + $subscriptions = Get-AzSubscription } -#Initialize arrays + +#Initialize tables and arrays + [System.Collections.ArrayList]$usage = @() -if ($ec -eq $null){ - $usage += ,(@("Subscription Name", "Subscription ID", "AHB Std vCores", "AHB Ent vCores", "PAYG Std vCores", "PAYG Ent vCores", "HADR Std vCores", "HADR Ent vCores", "Developer vCores", "Express vCores")) +if ($includeEC -eq $null){ + $usage += ,(@("Date", "Time", "Subscription Name", "Subscription ID", "AHB Std vCores", "AHB Ent vCores", "PAYG Std vCores", "PAYG Ent vCores", "HADR Std vCores", "HADR Ent vCores", "Developer vCores", "Express vCores")) }else{ - $usage += ,(@("Subscription Name", "Subscription ID", "AHB ECs", "PAYG ECs", "AHB Std vCores", "AHB Ent vCores", "PAYG Std vCores", "PAYG Ent vCores", "HADR Std vCores", "HADR Ent vCores", "Developer vCores", "Express vCores")) + $usage += ,(@("Date", "Time", "Subscription Name", "Subscription ID", "AHB ECs", "PAYG ECs", "AHB Std vCores", "AHB Ent vCores", "PAYG Std vCores", "PAYG Ent vCores", "HADR Std vCores", "HADR Ent vCores", "Developer vCores", "Express vCores")) } $subtotal = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0} $total = [pscustomobject]@{} -foreach( $property in $subtotal.psobject.properties.name ){ - $total | Add-Member -MemberType NoteProperty -Name $property -Value 6 -} +$subtotal.psobject.properties.name | %{$total | Add-Member -MemberType NoteProperty -Name $_ -Value 0} + +#Save the VM SKU table for future use -#Save the VM SKU table $VM_SKUs = Get-AzComputeResourceSku -Write-Host ([Environment]::NewLine + "-- Scanning subscriptions...") +Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --") # Calculate usage for each subscription + foreach ($sub in $subscriptions){ if ($sub.State -ne "Enabled") {continue} @@ -72,10 +69,8 @@ foreach ($sub in $subscriptions){ } # Reset the subtotals - foreach( $property in $subtotal.psobject.properties.name ){ - $subtotal.$property = 0 - } - + $subtotal.psobject.properties.name | %{$subtotal.$_ = 0} + #Get all logical servers $servers = Get-AzSqlServer @@ -239,25 +234,30 @@ foreach ($sub in $subscriptions){ } # Increment the totals and add subtotals to the usage array - foreach( $property in $subtotal.psobject.properties.name ){ - $total.$property += $subtotal.$property - } - if ($ec -eq $null){ - $usage += ,(@($sub.Name, $sub.Id, $subtotal.ahb_std, $subtotal.ahb_ent, $subtotal.payg_std, $subtotal.payg_ent, $subtotal.hadr_std, $subtotal.hadr_ent, $subtotal.developer, $subtotal.express)) + + $subtotal.psobject.properties.name | %{$total.$_ += $subtotal.$_} + + if ($includeEC -eq $null){ + $usage += ,(@((Get-Date -Format d), (Get-Date -Format t), $sub.Name, $sub.Id, $subtotal.ahb_std, $subtotal.ahb_ent, $subtotal.payg_std, $subtotal.payg_ent, $subtotal.hadr_std, $subtotal.hadr_ent, $subtotal.developer, $subtotal.express)) }else{ - $usage += ,(@($sub.Name, $sub.Id, ($subtotal.ahb_std + $subtotal.ahb_ent*4), ($subtotal.payg_std + $subtotal.payg_ent*4), $subtotal.ahb_std, $subtotal.ahb_ent, $subtotal.payg_std, $subtotal.payg_ent, $subtotal.hadr_std, $subtotal.hadr_ent, $subtotal.developer, $subtotal.express)) + $usage += ,(@((Get-Date -Format d), (Get-Date -Format t), $sub.Name, $sub.Id, ($subtotal.ahb_std + $subtotal.ahb_ent*4), ($subtotal.payg_std + $subtotal.payg_ent*4), $subtotal.ahb_std, $subtotal.ahb_ent, $subtotal.payg_std, $subtotal.payg_ent, $subtotal.hadr_std, $subtotal.hadr_ent, $subtotal.developer, $subtotal.express)) } } -if ($ec -eq $null){ - $usage += ,(@("Total", $null, $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express)) +# Add the total numbers to the usage array + +if ($includeEC -eq $null){ + $usage += ,(@((Get-Date -Format d), (Get-Date -Format t), "Total", $null, $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express)) }else{ - $usage += ,(@("Total", $null, ($total.ahb_std + $total.ahb_ent*4), ($total.payg_std + $total.payg_ent*4), $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express)) + $usage += ,(@((Get-Date -Format d), (Get-Date -Format t), "Total", $null, ($total.ahb_std + $total.ahb_ent*4), ($total.payg_std + $total.payg_ent*4), $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express)) } +# Append to '.\sql-license-usage.csv' + $table = ConvertFrom-Csv ($usage | %{ $_ -join ','} ) $table | Format-table -$fileName = '.\sql-license-usage_' + (Get-Date -f yyyy-MM-dd_HH-mm-ss) + '.csv' -$table | Export-Csv .\sql-license-usage.csv -NoTypeInformation +#$fileName = '.\sql-license-usage_' + (Get-Date -f yyyy-MM-dd_HH-mm-ss) + '.csv' +$fileName = '.\sql-license-usage.csv' +$table | Export-Csv $fileName -NoTypeInformation -Append -Write-Host ([Environment]::NewLine + "-- The usage data is saved to .\sql-license-usage.csv") +Write-Host ([Environment]::NewLine + "-- The usage data is saved to " + $fileName + "--")