|
1 | 1 | #
|
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. |
5 | 4 | #
|
6 | 5 | # You can specfy a single subscription to scan, or provide subscriptions as a .CSV file with the list of IDs.
|
7 | 6 | # If not specified, all subscriptions your role has access to are scanned.
|
8 | 7 | #
|
9 | 8 | # The script accepts the following command line parameters:
|
10 | 9 | #
|
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. |
12 | 11 | # 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 |
18 | 19 | #
|
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). |
20 | 21 | # The function was originally published on https://4sysops.com/archives/convert-json-to-a-powershell-hash-table/
|
21 | 22 | # and is used here with the author's permission.
|
22 | 23 | #
|
23 | 24 |
|
24 | 25 | param (
|
25 |
| - [Parameter (Mandatory=$false)] |
| 26 | + [Parameter (Mandatory=$true)] |
26 | 27 | [string] $SubId,
|
27 |
| - [Parameter (Mandatory= $false)] |
| 28 | + [Parameter (Mandatory= $true)] |
28 | 29 | [string] $ResourceGroup,
|
29 | 30 | [Parameter (Mandatory= $false)]
|
30 | 31 | [string] $MachineName,
|
31 | 32 | [Parameter (Mandatory= $true)]
|
32 | 33 | [ValidateSet("PAYG","Paid","LicenseOnly", IgnoreCase=$false)]
|
33 | 34 | [string] $LicenseType,
|
34 |
| - [Parameter (Mandatory= $false)] |
| 35 | + [Parameter (Mandatory= $true)] |
| 36 | + [ValidateSet("Yes","No", IgnoreCase=$false)] |
| 37 | + [string] $EnableESU, |
| 38 | + [Parameter (Mandatory= $true)] |
35 | 39 | [boolean] $Force=$false
|
36 | 40 | )
|
37 | 41 |
|
@@ -178,21 +182,49 @@ foreach ($sub in $subscriptions){
|
178 | 182 | ExtensionType = $r.extensionType
|
179 | 183 | }
|
180 | 184 |
|
| 185 | + $WriteSettings = $false |
181 | 186 | $settings = @{}
|
182 | 187 | $settings = $r.properties.settings | ConvertTo-Json | ConvertFrom-Json | ConvertTo-Hashtable
|
183 | 188 |
|
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 { |
187 | 202 | $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 |
190 | 204 | }
|
191 | 205 | }
|
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 | + } |
196 | 228 | }
|
197 | 229 | }
|
198 | 230 | }
|
|
0 commit comments