Skip to content

Commit ffd05b3

Browse files
author
Alexander (Sasha) Nosov
committed
Added -EnableESU option
1 parent a2bfcd2 commit ffd05b3

File tree

2 files changed

+78
-36
lines changed

2 files changed

+78
-36
lines changed

samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ ms.date: 6/4/2023
99

1010
# Overview
1111

12-
This script allows you to to set or change the license type on all Azure-connected SQL Servers
13-
on a specific resource, in a single resource group, a specific subscription, a list of subscriptions or all subscriptions to which you have access. By default, it sets the specified license type value on the servers where it is undefined. But you can request to set it on all servers in the selected scope.
12+
This script provides a scaleable solution to set or change the license type and/or enable or disable the ESU policy on all Azure-connected SQL Servers in a specified scope.
1413

1514
You can specify a single subscription to scan, or provide a list of subscriptions as a .CSV file.
1615
If not specified, all subscriptions your role has access to are scanned.
@@ -28,11 +27,12 @@ The script accepts the following command line parameters:
2827

2928
| **Parameter**                                         | **Value**                                                                       | **Description** |
3029
|:--|:--|:--|
31-
|-SubId|subscription_id *or* a file_name|Optional: subscription id or a .csv file with the list of subscriptions<sup>1</sup>. If not specified all subscriptions will be scanned|
32-
|-ResourceGroup |resource_group_name|Optional: Limit the scope to a specific resource group|
33-
|-MachineName |machine_name|Optional: Limit the scope to a specific machine|
34-
|-LicenceType | "Paid", "PAYG" or "LicenseOnly"| Required: Specifies the license type value |
35-
|-Force|\$True or \$False (default)|Optional. Set the new license type for all installed extensions. By default the value is set only if license type is undefined.|
30+
|-SubId|subscription_id *or* a file_name|Optional: Subscription id or a .csv file with the list of subscriptions<sup>1</sup>. If not specified all subscriptions will be scanned|
31+
|-ResourceGroup |resource_group_name|Optional: Limits the scope to a specific resource group|
32+
|-MachineName |machine_name|Optional: Limits the scope to a specific machine|
33+
|-LicenceType | "Paid", "PAYG" or "LicenseOnly"| Optional: Sets the license type to the specified value |
34+
|-EnableESU | "Yes", "No" | Optional. Enables the ESU policy the value is "Yrs" or disables it if the value is "No". To enable, the license type must be "Paid" or "PAYG"|
35+
|-Force|\$True or \$False (default)|Optional. Forces the change of the license type to the specified value on all installed extensions. If -Force is not specified, the -LicenseType value is set only if undefined. Ignored if -LicenseType is not specified|
3636

3737
<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.
3838
```PowerShell
@@ -81,11 +81,7 @@ This option is recommended because Cloud shell has the Azure PowerShell modules
8181
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 -o modify-license-type.ps1
8282
```
8383

84-
1. Run the script. The following command will set License Type to 'Paid" on all servers in all the subscriptions your role has access to.
85-
86-
```console
87-
.//modify-license-type.ps1 -LicenseType Paid -Force $true
88-
```
84+
1. Run the script.
8985

9086
> [!NOTE]
9187
> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS.
@@ -122,8 +118,22 @@ Use the following steps to run the script in a PowerShell session on your PC.
122118
Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId
123119
```
124120

125-
1. Run the script using the desired scope. The following command will set License Type to 'Paid" on all servers in the specified subscription.
121+
1. Run the script using the desired scope.
122+
123+
# Examples
124+
125+
1. The following command will set License Type to 'Paid" on all servers in all the subscriptions your role has access to.
126126

127127
```console
128-
.//modify-license-type.ps1 -SubId <sub_id> -LicenseType Paid -Force $true
128+
.//modify-license-type.ps1 -LicenseType Paid -EnableESU Yes -Force $true
129129
```
130+
1. The following command will set License Type to 'Paid" and enables ESU on all servers in the subscriptions <your_sub_id> and the resource group <your_rg> role has access to.
131+
132+
```console
133+
.//modify-license-type.ps1 -SubId <your_sub_id> -ResourceGroup <your_rg> -LicenseType Paid -EnableESU Yes -Force $true
134+
```
135+
1. The following command will disable ESU on all servers in the subscriptions <your_sub_id> and the resource group <your_rg> role has access to.
136+
137+
```console
138+
.//modify-license-type.ps1 -SubId <your_sub_id> -ResourceGroup <your_rg> -EnableESU No -Force $true
139+
```

samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
#
2-
# This script provides a scaleable solution to set or change the license type on all Azure-connected SQL Servers
3-
# in a specific subscription, a list of subscruiptions or the entire account. By default, it sets the new license
4-
# type value only on the servers where it is undefined.
2+
# This script provides a scaleable solution to set or change the license type and/or enable or disable the ESU policy
3+
# on all Azure-connected SQL Servers in a specified scope.
54
#
65
# You can specfy a single subscription to scan, or provide subscriptions as a .CSV file with the list of IDs.
76
# If not specified, all subscriptions your role has access to are scanned.
87
#
98
# The script accepts the following command line parameters:
109
#
11-
# -SubId [subscription_id] | [csv_file_name] (Limit scope to specific subscriptions. Accepts a .csv file with the list of subscriptions.
10+
# -SubId [subscription_id] | [csv_file_name] (Optional. Limits the scope to specific subscriptions. Accepts a .csv file with the list of subscriptions.
1211
# If not specified all subscriptions will be scanned)
13-
# -ResourceGroup [resource_goup] (Limit scope to a specific resoure group)
14-
# -MachineName [machine_name] (Limit scope to a specific machine)
15-
# -LicenseType [license_type_value] (Specific LT value)
16-
# -Force (Required. Set the new license type on all installed extensions.
17-
# By default the value is set only if license type is undefined undefined)
12+
# -ResourceGroup [resource_goup] (Optional. Limits the scope to a specific resoure group)
13+
# -MachineName [machine_name] (Optional. Limits the scope to a specific machine)
14+
# -LicenseType [license_type_value] (Optional. Sets the license type to the specified value)
15+
# -EnabelESU [Yes or No] (Optional. Enables the ESU policy the value is "Yes" or disables it if the value is "No"
16+
# To enable, the license type must be "Paid" or "PAYG"
17+
# -Force [$true or $false] (Optional. Forces the chnahge of the license type to the specified value on all installed extensions.
18+
# If Force is not specified, the -LicenseType value is set only if undefined. Ignored if -LicenseType is not specified
1819
#
19-
# The script uses a function ConvertTo-HashTable that was created by Adam Bertram (@adam-bertram).
20+
# This script uses a function ConvertTo-HashTable that was created by Adam Bertram (@adam-bertram).
2021
# The function was originally published on https://4sysops.com/archives/convert-json-to-a-powershell-hash-table/
2122
# and is used here with the author's permission.
2223
#
2324

2425
param (
25-
[Parameter (Mandatory=$false)]
26+
[Parameter (Mandatory=$true)]
2627
[string] $SubId,
27-
[Parameter (Mandatory= $false)]
28+
[Parameter (Mandatory= $true)]
2829
[string] $ResourceGroup,
2930
[Parameter (Mandatory= $false)]
3031
[string] $MachineName,
3132
[Parameter (Mandatory= $true)]
3233
[ValidateSet("PAYG","Paid","LicenseOnly", IgnoreCase=$false)]
3334
[string] $LicenseType,
34-
[Parameter (Mandatory= $false)]
35+
[Parameter (Mandatory= $true)]
36+
[ValidateSet("Yes","No", IgnoreCase=$false)]
37+
[string] $EnableESU,
38+
[Parameter (Mandatory= $true)]
3539
[boolean] $Force=$false
3640
)
3741

@@ -178,21 +182,49 @@ foreach ($sub in $subscriptions){
178182
ExtensionType = $r.extensionType
179183
}
180184

185+
$WriteSettings = $false
181186
$settings = @{}
182187
$settings = $r.properties.settings | ConvertTo-Json | ConvertFrom-Json | ConvertTo-Hashtable
183188

184-
if ($settings.ContainsKey("LicenseType")) {
185-
if ($Force) {
186-
if ($settings["LicenseType"] -ne $LicenseType ) {
189+
# set the license type or update (if -Force). ESU must be disabled to set to LicenseOnly.
190+
$LO_Allowed = (!$settings["enableExtendedSecurityUpdates"] -and !$EnableESU) -or ($EnableESU -eq "No")
191+
192+
if ($LicenseType) {
193+
if (($LicenseType -eq "LicenseOnly") -and !$LO_Allowed) {
194+
write-host "ESU must be disabled before license type can be set to $($LicenseType)"
195+
} else {
196+
if ($settings.ContainsKey("LicenseType")) {
197+
if ($Force) {
198+
$settings["LicenseType"] = $LicenseType
199+
$WriteSettings = $true
200+
}
201+
} else {
187202
$settings["LicenseType"] = $LicenseType
188-
Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])]"
189-
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null
203+
$WriteSettings = $true
190204
}
191205
}
192-
} else {
193-
$settings["LicenseType"] = $LicenseType
194-
Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])]"
195-
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null
206+
207+
}
208+
209+
# Enable ESU for qualified license types or disable
210+
if ($EnableESU) {
211+
if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($EnableESU -eq "No")) {
212+
$settings["enableExtendedSecurityUpdates"] = ($EnableESU -eq "Yes")
213+
$WriteSettings = $true
214+
} else {
215+
write-host "The configured license type does not support ESUs"
216+
}
217+
}
218+
219+
If ($WriteSettings) {
220+
Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])] : Enable ESU: [$($settings["enableExtendedSecurityUpdates"])]"
221+
try {
222+
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null
223+
} catch {
224+
write-host "The request to modify the extenion object failed with the following error:"
225+
write-host $_.Exception.Message
226+
{continue}
227+
}
196228
}
197229
}
198230
}

0 commit comments

Comments
 (0)