Skip to content

Commit 03f0810

Browse files
Alexander NosovAlexander Nosov
authored andcommitted
Removed prompt
1 parent d788bb0 commit 03f0810

File tree

2 files changed

+48
-54
lines changed

2 files changed

+48
-54
lines changed

samples/manage/azure-hybrid-benefit/README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ services: Azure SQL
33
platforms: Azure
44
author: anosov1960
55
ms.author: sashan
6-
ms.date: 12/15/2020
6+
ms.date: 12/17/2020
77
---
88

99
# Overview
1010

11-
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_<timestamp>.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:
11+
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:
1212

13+
- Date and time of scan
1314
- AHB Standard vCores
1415
- AHB Enterprise vCores
1516
- PAYG Standard vCores
@@ -24,40 +25,33 @@ This script is provided to help you manage the SQL Server licenses that are cons
2425
> - For IaaS workloads, such as SQL Server in Virtual Machines or SSIS integration runtimes, each vCPU is counted as one vCore.
2526
> - 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.
2627
27-
2828
# Running the script using Cloud Shell
2929

3030
Use the following steps to calculate the SQL Server license usage:
3131

32-
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
32+
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, read [PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
3333

3434
2. Upload the script to the shell using the following command:
3535

3636
```console
3737
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 -o sql-license-usage.ps1
3838
```
3939

40-
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.
40+
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.
4141

4242
```console
43-
./sql-license-usage.ps1
43+
./sql-license-usage.ps1 <subscription ID> or <filename>.csv
4444
```
4545

46-
If you need to scan a subset of the subscriptions, use the following steps:
47-
48-
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.
46+
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.
4947

5048
```console
5149
Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
5250
```
53-
54-
2. Run the script and specify the `.csv` file as a parameter.
55-
```console
56-
./sql-license-usage.ps1 .\mysubscriptions.csv
57-
```
58-
5951
> [!NOTE]
6052
> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS.
6153
> - The `curl` command will copy the script directly to the home folder associated with your Cloud Shell session.
62-
> - The script will prompt for the resource group name and print a message when migration is completed.
6354

55+
# Tracking SQL license usage over time
56+
57+
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).

samples/manage/azure-hybrid-benefit/sql-license-usage.ps1

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,49 @@
1414
#
1515
# 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.
1616
#
17-
# 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
17+
# 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
1818
# such a file by the following command and then edit to remove the subscriptions you don't want to scan:
1919
# > Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
2020
#
21-
# If no file is provided, the script will prompt for a subscriptiobn ID or `*`. The latter will automatically scan all the subscriptions you account
21+
# 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
2222
# has access to.
2323
#
2424
#
2525
# NOTE: The script does not calculate usage for Azure SQL resources that use the DTU-based purchasing model
2626
#
2727

28-
# Import the subscription info
28+
# Subscriptions to scan
2929

30-
if ($args[0] -ne $null) {
31-
# Read subscription list from the .csv file
30+
if ($args[0] -like "*.csv") {
3231
$subscriptions = Import-Csv $args[0]
33-
} else {
34-
# Promt for the subscription
35-
$Id = read-host -Prompt "Enter Subscription ID"
36-
if ($Id -eq "*") {
37-
$subscriptions = Get-AzSubscription
38-
} else {
39-
$subscriptions = [PSCustomObject]@{SubscriptionId = $Id} | Get-AzSubscription
40-
}
32+
}elseif($args[0] -ne $null){
33+
$subscriptions = [PSCustomObject]@{SubscriptionId = $args[0]} | Get-AzSubscription
34+
}else{
35+
$subscriptions = Get-AzSubscription
4136
}
4237

43-
#Initialize arrays
38+
39+
#Initialize tables and arrays
40+
4441
[System.Collections.ArrayList]$usage = @()
45-
if ($ec -eq $null){
46-
$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"))
42+
if ($includeEC -eq $null){
43+
$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"))
4744
}else{
48-
$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"))
45+
$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"))
4946
}
5047

5148
$subtotal = [pscustomobject]@{ahb_std=0; ahb_ent=0; payg_std=0; payg_ent=0; hadr_std=0; hadr_ent=0; developer=0; express=0}
5249
$total = [pscustomobject]@{}
53-
foreach( $property in $subtotal.psobject.properties.name ){
54-
$total | Add-Member -MemberType NoteProperty -Name $property -Value 6
55-
}
50+
$subtotal.psobject.properties.name | %{$total | Add-Member -MemberType NoteProperty -Name $_ -Value 0}
51+
52+
#Save the VM SKU table for future use
5653

57-
#Save the VM SKU table
5854
$VM_SKUs = Get-AzComputeResourceSku
5955

60-
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions...")
56+
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --")
6157

6258
# Calculate usage for each subscription
59+
6360
foreach ($sub in $subscriptions){
6461

6562
if ($sub.State -ne "Enabled") {continue}
@@ -72,10 +69,8 @@ foreach ($sub in $subscriptions){
7269
}
7370

7471
# Reset the subtotals
75-
foreach( $property in $subtotal.psobject.properties.name ){
76-
$subtotal.$property = 0
77-
}
78-
72+
$subtotal.psobject.properties.name | %{$subtotal.$_ = 0}
73+
7974
#Get all logical servers
8075
$servers = Get-AzSqlServer
8176

@@ -239,25 +234,30 @@ foreach ($sub in $subscriptions){
239234
}
240235

241236
# Increment the totals and add subtotals to the usage array
242-
foreach( $property in $subtotal.psobject.properties.name ){
243-
$total.$property += $subtotal.$property
244-
}
245-
if ($ec -eq $null){
246-
$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))
237+
238+
$subtotal.psobject.properties.name | %{$total.$_ += $subtotal.$_}
239+
240+
if ($includeEC -eq $null){
241+
$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))
247242
}else{
248-
$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))
243+
$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))
249244
}
250245
}
251246

252-
if ($ec -eq $null){
253-
$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))
247+
# Add the total numbers to the usage array
248+
249+
if ($includeEC -eq $null){
250+
$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))
254251
}else{
255-
$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))
252+
$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))
256253
}
257254

255+
# Append to '.\sql-license-usage.csv'
256+
258257
$table = ConvertFrom-Csv ($usage | %{ $_ -join ','} )
259258
$table | Format-table
260-
$fileName = '.\sql-license-usage_' + (Get-Date -f yyyy-MM-dd_HH-mm-ss) + '.csv'
261-
$table | Export-Csv .\sql-license-usage.csv -NoTypeInformation
259+
#$fileName = '.\sql-license-usage_' + (Get-Date -f yyyy-MM-dd_HH-mm-ss) + '.csv'
260+
$fileName = '.\sql-license-usage.csv'
261+
$table | Export-Csv $fileName -NoTypeInformation -Append
262262

263-
Write-Host ([Environment]::NewLine + "-- The usage data is saved to .\sql-license-usage.csv")
263+
Write-Host ([Environment]::NewLine + "-- The usage data is saved to " + $fileName + "--")

0 commit comments

Comments
 (0)