Skip to content

Commit ee84f86

Browse files
committed
Added CRED parameter and Runbook setup steps
1 parent 7e7f22c commit ee84f86

3 files changed

Lines changed: 74 additions & 41 deletions

File tree

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

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services: Azure SQL
33
platforms: Azure
44
author: anosov1960
55
ms.author: sashan
6-
ms.date: 1/11/2021
6+
ms.date: 2/2/2021
77
---
88

99
# Overview
@@ -45,15 +45,14 @@ The following resources are in scope for the license utilization analysis:
4545

4646
The script accepts the following command line parameters:
4747

48-
| **Parameter**                                 | **Value** | **Description** |
48+
| **Parameter**                                 | **Value**                                                                       | **Description** |
4949
|:--|:--|:--|
50-
|-SubId|subscription_id *or* a file_name|Accepts a .csv file with the list of subscriptions<sup>1</sup>|
51-
|-UseInRunbook||Must be specified when executed as a Runbook|
52-
|-Server|[protocol:]server[instance_name][,port]|Required to save data to the database|
53-
|-Database|database_name|Required to save data to the database|
54-
|-Username|user_name|Required to save data to the database|
55-
|-Password|password|Required to save data to the database, must be passed as a *[SecureString]* variable|
56-
|-FilePath|csv_file_name|Required to save data in a .csv format. Ignored if database parameters are specified|
50+
|-SubId|subscription_id *or* a file_name|Optional: subscription id or a .csv file with the list of subscriptions<sup>1</sup>|
51+
|-UseInRunbook| \$True or \$False (default) |Optional: must be $True when executed as a Runbook|
52+
|-Server|[protocol:]server[instance_name][,port]|Optional: SQL Server connection endpoint to save data to the database.<br> Must be accompanied by -Database and -Cred |
53+
|-Database|database_name|Optional: database name where data will be saved.<br> Must be accompanied by -Server and -Cred|
54+
|-Cred|credential_object|Optional: value of type PSCredential to securely pass database user and password|
55+
|-FilePath|csv_file_name|Optional: filename where the data will be saved in a .csv format. Ignored if database parameters are specified|
5756

5857
<sup>1</sup>You can create a .csv file using the following command and then edit to remove the subscriptions you don't want to scan.
5958
```PowerShell
@@ -79,11 +78,24 @@ The following command will scan the subscription `<sub_id>` and save the results
7978

8079
## Example 3
8180

82-
The following command will scan all the subscriptions in the account and save the results in a SQL database `<db_name>` on a SQL Server instance `<sql_server_name>.database.windows.net`.
81+
The following command will scan all the subscriptions in the account and save the results in a SQL database `<db_name>` on a SQL Server instance `<sql_server_name>.database.windows.net`. It will prompt for the database user name and password.
8382

8483
```PowerShell
85-
$cred = Get-Credential -credential <user_name>
86-
.\sql-license-usage.ps1 -Server <server_name>.database.windows.net -Database <db_name> -Username $cred.Username -Password $cred.Password
84+
$cred = Get-Credential
85+
.\sql-license-usage.ps1 -Server <server_name>.database.windows.net -Database <db_name> -Cred $cred
86+
```
87+
88+
## Example 4
89+
90+
The following command uses the parameter splatting method to achieve the same outcome as Example 3.
91+
92+
```PowerShell
93+
$params =@{
94+
Server="my-westus2-server.database.windows.net";
95+
Database="sql-license-usage";
96+
Cred=Get-Credential;
97+
}
98+
.\sql-license-usage.ps1 @params
8799
```
88100

89101
# Running the script using Cloud Shell
@@ -108,17 +120,31 @@ Use the following steps to calculate the SQL Server license usage:
108120
> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS.
109121
> - The `curl` command will copy the script directly to the home folder associated with your Cloud Shell session.
110122

111-
# Tracking SQL license usage over time
112-
113-
You can track your license utilization over time by periodically running this script. To schedule automatic execution of the script, create a PowerShell runbook using an Azure Automation account. See the [Runbook tutorial](https://docs.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-powershell) for the details of how to create a PowerShell runbook. Because the script accesses the resources across multiple subscriptions, the runbook must be able to authenticate using the Run As account that was automatically created when you created your Automation account. The logic required for the Runbooks is part of the script.
123+
# Running the script as a Azure runbook
124+
125+
You can track your license utilization over time by running this script on schedule as a runbook. To set it up using Azure Portal, follow these steps.
126+
127+
1. [Create a new automation account](https://ms.portal.azure.com/#create/Microsoft.AutomationAccount) or use an existing one.
128+
1. Select *Credentials* in the **Shared resources** group and create a credential object with the database username and password. The script will use these to connect to the specified database and save data.
129+
1. Select *Modules* in the **Shared resources** group and make sure your automation account have the following PowerShell modules installed. If not, add them from the Gallery.
130+
- Az.Accounts
131+
- Az.Compute
132+
- Az.DataFactory
133+
- Az.Resources
134+
- Az.Sql
135+
- Az.SqlVirtualMachine
136+
1. Select *Runbooks* in the **Process automation** group and create a new PowerShell runbook. Open it and click on the *Edit* button.
137+
1. Open the [script file](https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1), copy the entire file and paste it into the editor, click on *Save* and then click on *Publish*.
138+
1. From the runbook blade, click on the *Link to schedule* button and select an existing schedule or create a new one with the desired frequency of runs and the expiration time.
139+
1. Click on *Parameters and run settings* and specify the following parameters:
140+
- SUBID. Leave it blank if you want to scan all the subscriptions
141+
- SERVER. Put in the SQL Server connection endpoint (e.g. my-westus2-sql-server.database.windows.net)
142+
- CRED. Put in the name of the credential object you created in Step 2.
143+
- DATABASE. Put in the database name where you want to save the license information.
144+
- USEINRUNBOOKS. Select True.
145+
146+
For more information about the runbooks, see the [Runbook tutorial](https://docs.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-powershell)
114147

115148
>[!IMPORTANT]
116-
> - When running the script as a runbook, use a database to ensure that the results can be analyzed outside of the runbook.
117-
> - You must specify a *-UseInRunbook* switch to ensure that the runbook is authenticated using the Run As account.
118-
> - Your automation account must have the following modules installed. If not, add them from the Gallery.
119-
> - Az.Accounts
120-
> - Az.Compute
121-
> - Az.DataFactory
122-
> - Az.Resources
123-
> - Az.Sql
124-
> - Az.SqlVirtualMachine
149+
> - Because the script accesses the resources across multiple subscriptions, the runbook must be able to authenticate using the Run As account that was automatically created when you created your Automation account. The logic required for the Runbooks is part of the script.
150+
> - When running the script as a runbook, it is b=necessary to use a database to ensure that the results can be analyzed outside of the runbook.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"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"
2+
"2021-02-02","16:47:24","geodr_m2_365640","10a238d6-a139-46d0-818d-d091394072b6","310","6","66","61","2","1","0","0","0","0"
3+
"2021-02-02","16:50:06","geodr_m2_365640","10a238d6-a139-46d0-818d-d091394072b6","310","6","66","61","2","1","0","0","0","0"

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,28 @@
3434
# -UseInRunbook [True] | [False] (Required when executed as a Runbook)
3535
# -Server [protocol:]server[instance_name][,port] (Required to save data to the database)
3636
# -Database [database_name] (Required to save data to the database)
37-
# -Username [user_name] (Required to save data to the database)
38-
# -Password [password] (Required to save data to the database, must be passed as secure string)
37+
# -Cred [credential_object] (Required to save data to the database)
3938
# -FilePath [csv_file_name] (Required to save data in a .csv format. Ignored if database parameters are specified)
4039
#
4140
#
4241

4342
param (
43+
[Parameter (Mandatory= $false)]
4444
[string] $SubId,
45+
[Parameter (Mandatory= $false)]
4546
[string] $Server,
46-
[string] $Username,
47-
[SecureString] $Password,
47+
[Parameter (Mandatory= $false)]
48+
[PSCredential] $Cred,
49+
[Parameter (Mandatory= $false)]
4850
[string] $Database,
51+
[Parameter (Mandatory= $false)]
4952
[string] $FilePath,
50-
[bool] $UseInRunbook,
51-
[bool] $IncludeEC
53+
[Parameter (Mandatory= $false)]
54+
[bool] $UseInRunbook = $false,
55+
[Parameter (Mandatory= $false)]
56+
[bool] $IncludeEC = $false
5257
)
5358

54-
5559
function Load-Module ($m) {
5660

5761
# This function ensures that the specified module is imported into the session
@@ -108,7 +112,7 @@ if ($UseInRunbook){
108112
$requiredModules = @(
109113
"Az.Accounts",
110114
"Az.Compute",
111-
"Az.DatraFactory",
115+
"Az.DataFactory",
112116
"Az.Resources",
113117
"Az.Sql",
114118
"Az.SqlVirtualMachine"
@@ -129,15 +133,15 @@ if ($SubId -like "*.csv") {
129133
$subscriptions = Get-AzSubscription
130134
}
131135

132-
[Boolean] $useDatabase = $PSBoundParameters.ContainsKey("Server") -and $PSBoundParameters.ContainsKey("Username") -and $PSBoundParameters.ContainsKey("Password") -and $PSBoundParameters.ContainsKey("Database")
136+
[bool] $useDatabase = $PSBoundParameters.ContainsKey("Server") -and $PSBoundParameters.ContainsKey("Cred") -and $PSBoundParameters.ContainsKey("Database")
133137

134138
#Initialize tables and arrays
135139

136140
if ($useDatabase){
137141

138142
#Database setup
139143

140-
$cred = New-Object System.Management.Automation.PSCredential($Username,$Password)
144+
#$cred = New-Object System.Management.Automation.PSCredential($Username,$Password)
141145

142146
[String] $tableName = "Usage-per-subscription"
143147
[String] $testSQL = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
@@ -178,8 +182,8 @@ if ($useDatabase){
178182
$propertiesToSplat = @{
179183
Database = $Database
180184
ServerInstance = $Server
181-
User = $cred.Username
182-
Password = $cred.GetNetworkCredential().Password
185+
User = $Cred.Username
186+
Password = $Cred.GetNetworkCredential().Password
183187
Query = $testSQL
184188
}
185189

@@ -413,7 +417,7 @@ foreach ($sub in $subscriptions){
413417

414418
$softwarePlan = Invoke-RestMethod @params
415419
if ($softwarePlan.Sku.Name -like "SQL*"){
416-
$size_info = $VM_SKUs | where {$_.ResourceType.Contains('hostGroups/hosts') -and $_.Name.Contains($vm_host.Sku.Name)} | Select-Object -First 1
420+
$size_info = $VM_SKUs | Where-Object {$_.ResourceType.Contains('hostGroups/hosts') -and $_.Name.Contains($vm_host.Sku.Name)} | Select-Object -First 1
417421
$cores= $size_info.Capabilities | Where-Object {$_.name -eq "Cores"}
418422
$subtotal.ahb_ent += $cores.Value
419423
}
@@ -426,12 +430,12 @@ foreach ($sub in $subscriptions){
426430

427431
$Date = Get-Date -Format "yyy-MM-dd"
428432
$Time = Get-Date -Format "HH:mm:ss"
429-
if ($IncludeEC -eq $null){
430-
$ahb_ec = 0
431-
$payg_ec = 0
432-
}else{
433+
if ($IncludeEC){
433434
$ahb_ec = ($subtotal.ahb_std + $subtotal.ahb_ent*4)
434435
$payg_ec = ($subtotal.payg_std + $subtotal.payg_ent*4)
436+
}else{
437+
$ahb_ec = 0
438+
$payg_ec = 0
435439
}
436440
if ($useDatabase){
437441
$propertiesToSplat.Query = $insertSQL -f $Date, $Time, $sub.Name, $sub.Id, $ahb_ec, $payg_ec, $subtotal.ahb_std, $subtotal.ahb_ent, $subtotal.payg_std, $subtotal.payg_ent, $subtotal.hadr_std, $subtotal.hadr_ent, $subtotal.developer, $subtotal.express

0 commit comments

Comments
 (0)