From a2bfcd2d705b084936453426bc5083908d10bb77 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Tue, 20 Jun 2023 08:24:07 -0700 Subject: [PATCH 01/14] Replaced -All in the examples --- .../modify-license-type/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md index 4f3b9e9b..ba09f396 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md @@ -52,7 +52,7 @@ The following command will scan all the subscriptions to which the user has acce The following command will scan the subscription `` and set the license type value to "Paid" on all servers. ```PowerShell -.\modify-license-type.ps1 -SubId -LicenseType Paid -All $True +.\modify-license-type.ps1 -SubId -LicenseType Paid -Force $True ``` ## Example 3 @@ -60,7 +60,7 @@ The following command will scan the subscription `` and set the license The following command will scan resource group in the subscription `` and set the license type value to "PAYG" on all servers. ```PowerShell -.\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType PAYG -All $True +.\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType PAYG -Force $True ``` # Running the script using Cloud Shell From 7dce481921559b1a66665c7d8907771b061926e9 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Tue, 20 Jun 2023 08:32:33 -0700 Subject: [PATCH 02/14] Update README.md Fixed examples --- .../modify-license-type/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md index 4f3b9e9b..302911ab 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md @@ -3,7 +3,7 @@ services: Azure Arc-enabled SQL Server platforms: Azure author: anosov1960 ms.author: sashan -ms.date: 6/4/2023 +ms.date: 6/21/2023 --- @@ -52,7 +52,7 @@ The following command will scan all the subscriptions to which the user has acce The following command will scan the subscription `` and set the license type value to "Paid" on all servers. ```PowerShell -.\modify-license-type.ps1 -SubId -LicenseType Paid -All $True +.\modify-license-type.ps1 -SubId -LicenseType Paid -Force $True ``` ## Example 3 @@ -60,7 +60,7 @@ The following command will scan the subscription `` and set the license The following command will scan resource group in the subscription `` and set the license type value to "PAYG" on all servers. ```PowerShell -.\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType PAYG -All $True +.\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType PAYG -Force $True ``` # Running the script using Cloud Shell From ffd05b3c03bbbe52bfeab9ebd1e7225e45bbe2c9 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 5 Jul 2023 14:01:22 -0700 Subject: [PATCH 03/14] Added -EnableESU option --- .../modify-license-type/README.md | 38 ++++++---- .../modify-license-type.ps1 | 76 +++++++++++++------ 2 files changed, 78 insertions(+), 36 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md index ba09f396..4c4a684c 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md @@ -9,8 +9,7 @@ ms.date: 6/4/2023 # Overview -This script allows you to to set or change the license type on all Azure-connected SQL Servers -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. +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. You can specify a single subscription to scan, or provide a list of subscriptions as a .CSV file. If not specified, all subscriptions your role has access to are scanned. @@ -28,11 +27,12 @@ The script accepts the following command line parameters: | **Parameter**                                         | **Value**                                                                       | **Description** | |:--|:--|:--| -|-SubId|subscription_id *or* a file_name|Optional: subscription id or a .csv file with the list of subscriptions1. If not specified all subscriptions will be scanned| -|-ResourceGroup |resource_group_name|Optional: Limit the scope to a specific resource group| -|-MachineName |machine_name|Optional: Limit the scope to a specific machine| -|-LicenceType | "Paid", "PAYG" or "LicenseOnly"| Required: Specifies the license type value | -|-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.| +|-SubId|subscription_id *or* a file_name|Optional: Subscription id or a .csv file with the list of subscriptions1. If not specified all subscriptions will be scanned| +|-ResourceGroup |resource_group_name|Optional: Limits the scope to a specific resource group| +|-MachineName |machine_name|Optional: Limits the scope to a specific machine| +|-LicenceType | "Paid", "PAYG" or "LicenseOnly"| Optional: Sets the license type to the specified value | +|-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"| +|-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| 1You can create a .csv file using the following command and then edit to remove the subscriptions you don't want to scan. ```PowerShell @@ -81,11 +81,7 @@ This option is recommended because Cloud shell has the Azure PowerShell modules 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 ``` -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. - - ```console - .//modify-license-type.ps1 -LicenseType Paid -Force $true - ``` +1. Run the script. > [!NOTE] > - 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. Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId ``` -1. Run the script using the desired scope. The following command will set License Type to 'Paid" on all servers in the specified subscription. +1. Run the script using the desired scope. + +# Examples + +1. The following command will set License Type to 'Paid" on all servers in all the subscriptions your role has access to. ```console - .//modify-license-type.ps1 -SubId -LicenseType Paid -Force $true + .//modify-license-type.ps1 -LicenseType Paid -EnableESU Yes -Force $true ``` +1. The following command will set License Type to 'Paid" and enables ESU on all servers in the subscriptions and the resource group role has access to. + + ```console + .//modify-license-type.ps1 -SubId -ResourceGroup -LicenseType Paid -EnableESU Yes -Force $true + ``` +1. The following command will disable ESU on all servers in the subscriptions and the resource group role has access to. + + ```console + .//modify-license-type.ps1 -SubId -ResourceGroup -EnableESU No -Force $true + ``` \ No newline at end of file diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index c758c8a7..0580522a 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -1,37 +1,41 @@ # -# This script provides a scaleable solution to set or change the license type on all Azure-connected SQL Servers -# in a specific subscription, a list of subscruiptions or the entire account. By default, it sets the new license -# type value only on the servers where it is undefined. +# 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. # # You can specfy a single subscription to scan, or provide subscriptions as a .CSV file with the list of IDs. # If not specified, all subscriptions your role has access to are scanned. # # The script accepts the following command line parameters: # -# -SubId [subscription_id] | [csv_file_name] (Limit scope to specific subscriptions. Accepts a .csv file with the list of subscriptions. +# -SubId [subscription_id] | [csv_file_name] (Optional. Limits the scope to specific subscriptions. Accepts a .csv file with the list of subscriptions. # If not specified all subscriptions will be scanned) -# -ResourceGroup [resource_goup] (Limit scope to a specific resoure group) -# -MachineName [machine_name] (Limit scope to a specific machine) -# -LicenseType [license_type_value] (Specific LT value) -# -Force (Required. Set the new license type on all installed extensions. -# By default the value is set only if license type is undefined undefined) +# -ResourceGroup [resource_goup] (Optional. Limits the scope to a specific resoure group) +# -MachineName [machine_name] (Optional. Limits the scope to a specific machine) +# -LicenseType [license_type_value] (Optional. Sets the license type to the specified value) +# -EnabelESU [Yes or No] (Optional. Enables the ESU policy the value is "Yes" or disables it if the value is "No" +# To enable, the license type must be "Paid" or "PAYG" +# -Force [$true or $false] (Optional. Forces the chnahge 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 # -# The script uses a function ConvertTo-HashTable that was created by Adam Bertram (@adam-bertram). +# This script uses a function ConvertTo-HashTable that was created by Adam Bertram (@adam-bertram). # The function was originally published on https://4sysops.com/archives/convert-json-to-a-powershell-hash-table/ # and is used here with the author's permission. # param ( - [Parameter (Mandatory=$false)] + [Parameter (Mandatory=$true)] [string] $SubId, - [Parameter (Mandatory= $false)] + [Parameter (Mandatory= $true)] [string] $ResourceGroup, [Parameter (Mandatory= $false)] [string] $MachineName, [Parameter (Mandatory= $true)] [ValidateSet("PAYG","Paid","LicenseOnly", IgnoreCase=$false)] [string] $LicenseType, - [Parameter (Mandatory= $false)] + [Parameter (Mandatory= $true)] + [ValidateSet("Yes","No", IgnoreCase=$false)] + [string] $EnableESU, + [Parameter (Mandatory= $true)] [boolean] $Force=$false ) @@ -178,21 +182,49 @@ foreach ($sub in $subscriptions){ ExtensionType = $r.extensionType } + $WriteSettings = $false $settings = @{} $settings = $r.properties.settings | ConvertTo-Json | ConvertFrom-Json | ConvertTo-Hashtable - if ($settings.ContainsKey("LicenseType")) { - if ($Force) { - if ($settings["LicenseType"] -ne $LicenseType ) { + # set the license type or update (if -Force). ESU must be disabled to set to LicenseOnly. + $LO_Allowed = (!$settings["enableExtendedSecurityUpdates"] -and !$EnableESU) -or ($EnableESU -eq "No") + + if ($LicenseType) { + if (($LicenseType -eq "LicenseOnly") -and !$LO_Allowed) { + write-host "ESU must be disabled before license type can be set to $($LicenseType)" + } else { + if ($settings.ContainsKey("LicenseType")) { + if ($Force) { + $settings["LicenseType"] = $LicenseType + $WriteSettings = $true + } + } else { $settings["LicenseType"] = $LicenseType - Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])]" - Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null + $WriteSettings = $true } } - } else { - $settings["LicenseType"] = $LicenseType - Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])]" - Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null + + } + + # Enable ESU for qualified license types or disable + if ($EnableESU) { + if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($EnableESU -eq "No")) { + $settings["enableExtendedSecurityUpdates"] = ($EnableESU -eq "Yes") + $WriteSettings = $true + } else { + write-host "The configured license type does not support ESUs" + } + } + + If ($WriteSettings) { + Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])] : Enable ESU: [$($settings["enableExtendedSecurityUpdates"])]" + try { + Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null + } catch { + write-host "The request to modify the extenion object failed with the following error:" + write-host $_.Exception.Message + {continue} + } } } } From 22fa4c81c67d5ea149ce861172b58b315d7110ae Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 5 Jul 2023 14:28:58 -0700 Subject: [PATCH 04/14] Mande parameters optional --- .../modify-license-type/modify-license-type.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 0580522a..1d3dd35b 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -23,16 +23,16 @@ # param ( - [Parameter (Mandatory=$true)] + [Parameter (Mandatory=$false)] [string] $SubId, - [Parameter (Mandatory= $true)] + [Parameter (Mandatory= $false)] [string] $ResourceGroup, [Parameter (Mandatory= $false)] [string] $MachineName, - [Parameter (Mandatory= $true)] + [Parameter (Mandatory= $false)] [ValidateSet("PAYG","Paid","LicenseOnly", IgnoreCase=$false)] [string] $LicenseType, - [Parameter (Mandatory= $true)] + [Parameter (Mandatory= $false)] [ValidateSet("Yes","No", IgnoreCase=$false)] [string] $EnableESU, [Parameter (Mandatory= $true)] From 7e51b78765c5a894de6055cf83b9e356c0508e94 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 5 Jul 2023 16:04:59 -0700 Subject: [PATCH 05/14] Convert -Force to switch --- .../modify-license-type/modify-license-type.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 1d3dd35b..9ea42929 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -36,7 +36,7 @@ param ( [ValidateSet("Yes","No", IgnoreCase=$false)] [string] $EnableESU, [Parameter (Mandatory= $true)] - [boolean] $Force=$false + [switch] $Force ) function CheckModule ($m) { From 354ecc45998d31c6c4f6b9b8549bc93ef8a5895c Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 5 Jul 2023 16:25:20 -0700 Subject: [PATCH 06/14] Fixed optionality --- .../modify-license-type/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md index 0d435598..60681bc5 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md @@ -3,7 +3,7 @@ services: Azure Arc-enabled SQL Server platforms: Azure author: anosov1960 ms.author: sashan -ms.date: 6/21/2023 +ms.date: 7/5/2023 --- @@ -32,7 +32,7 @@ The script accepts the following command line parameters: |-MachineName |machine_name|Optional: Limits the scope to a specific machine| |-LicenceType | "Paid", "PAYG" or "LicenseOnly"| Optional: Sets the license type to the specified value | |-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"| -|-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| +|-Force| |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| 1You can create a .csv file using the following command and then edit to remove the subscriptions you don't want to scan. ```PowerShell @@ -52,7 +52,7 @@ The following command will scan all the subscriptions to which the user has acce The following command will scan the subscription `` and set the license type value to "Paid" on all servers. ```PowerShell -.\modify-license-type.ps1 -SubId -LicenseType Paid -Force $True +.\modify-license-type.ps1 -SubId -LicenseType Paid -Force ``` ## Example 3 @@ -60,7 +60,7 @@ The following command will scan the subscription `` and set the license The following command will scan resource group in the subscription `` and set the license type value to "PAYG" on all servers. ```PowerShell -.\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType PAYG -Force $True +.\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType PAYG -Force ``` # Running the script using Cloud Shell From d82dfc0b38a4c83cad6d557f2b193d0c541bdeef Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 5 Jul 2023 16:25:35 -0700 Subject: [PATCH 07/14] Fixed optionality --- .../modify-license-type/modify-license-type.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 9ea42929..bd583e74 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -14,7 +14,7 @@ # -LicenseType [license_type_value] (Optional. Sets the license type to the specified value) # -EnabelESU [Yes or No] (Optional. Enables the ESU policy the value is "Yes" or disables it if the value is "No" # To enable, the license type must be "Paid" or "PAYG" -# -Force [$true or $false] (Optional. Forces the chnahge of the license type to the specified value on all installed extensions. +# -Force (Optional. Forces the chnahge 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 # # This script uses a function ConvertTo-HashTable that was created by Adam Bertram (@adam-bertram). @@ -35,7 +35,7 @@ param ( [Parameter (Mandatory= $false)] [ValidateSet("Yes","No", IgnoreCase=$false)] [string] $EnableESU, - [Parameter (Mandatory= $true)] + [Parameter (Mandatory= $false)] [switch] $Force ) From dd11280c05e5759534d7a140497da025ed825bf7 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 5 Jul 2023 16:39:53 -0700 Subject: [PATCH 08/14] Added ESU examples --- .../modify-license-type/README.md | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md index 60681bc5..323bf470 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md @@ -57,12 +57,28 @@ The following command will scan the subscription `` and set the license ## Example 3 -The following command will scan resource group in the subscription `` and set the license type value to "PAYG" on all servers. +The following command will scan resource group `` in the subscription `` and set the license type value to "PAYG" on all servers. ```PowerShell .\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType PAYG -Force ``` +## Example 4 + +The following command will set License Type to 'Paid" and enables ESU on all servers in the subscriptions `` and the resource group ``. + +```console +.//modify-license-type.ps1 -SubId -ResourceGroup -LicenseType Paid -EnableESU Yes -Force +``` + +## Example 5 + +The following command will disable ESU on all servers in the subscriptions ``. + +```console +.//modify-license-type.ps1 -SubId -EnableESU No +``` + # Running the script using Cloud Shell This option is recommended because Cloud shell has the Azure PowerShell modules pre-installed and you are automatically authenticated. Use the following steps to run the script in Cloud Shell. @@ -81,7 +97,7 @@ This option is recommended because Cloud shell has the Azure PowerShell modules 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 ``` -1. Run the script. +1. Run the script. > [!NOTE] > - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS. @@ -118,22 +134,4 @@ Use the following steps to run the script in a PowerShell session on your PC. Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId ``` -1. Run the script using the desired scope. - -# Examples - -1. The following command will set License Type to 'Paid" on all servers in all the subscriptions your role has access to. - - ```console - .//modify-license-type.ps1 -LicenseType Paid -EnableESU Yes -Force $true - ``` -1. The following command will set License Type to 'Paid" and enables ESU on all servers in the subscriptions and the resource group role has access to. - - ```console - .//modify-license-type.ps1 -SubId -ResourceGroup -LicenseType Paid -EnableESU Yes -Force $true - ``` -1. The following command will disable ESU on all servers in the subscriptions and the resource group role has access to. - - ```console - .//modify-license-type.ps1 -SubId -ResourceGroup -EnableESU No -Force $true - ``` \ No newline at end of file +1. Run the script. From 10cb0a2cd872df44ad75d3d71c7d9c939a02ee49 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 5 Jul 2023 16:43:42 -0700 Subject: [PATCH 09/14] Fixed command syntax in examples --- .../modify-license-type/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md index 323bf470..a7b79256 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md @@ -68,7 +68,7 @@ The following command will scan resource group `` in the su The following command will set License Type to 'Paid" and enables ESU on all servers in the subscriptions `` and the resource group ``. ```console -.//modify-license-type.ps1 -SubId -ResourceGroup -LicenseType Paid -EnableESU Yes -Force +.\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType Paid -EnableESU Yes -Force ``` ## Example 5 @@ -76,7 +76,7 @@ The following command will set License Type to 'Paid" and enables ESU on all ser The following command will disable ESU on all servers in the subscriptions ``. ```console -.//modify-license-type.ps1 -SubId -EnableESU No +.\modify-license-type.ps1 -SubId -EnableESU No ``` # Running the script using Cloud Shell From 6f74d4293916271e9ba10783698a5faf1700c80e Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Tue, 11 Jul 2023 14:51:36 -0700 Subject: [PATCH 10/14] Added ESU timestamp --- .../azure-arc-enabled-sql-server/modify-license-type/README.md | 2 +- .../modify-license-type/modify-license-type.ps1 | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md index a7b79256..4bbccb4d 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md @@ -66,7 +66,7 @@ The following command will scan resource group `` in the su ## Example 4 The following command will set License Type to 'Paid" and enables ESU on all servers in the subscriptions `` and the resource group ``. - + ```console .\modify-license-type.ps1 -SubId -ResourceGroup -LicenseType Paid -EnableESU Yes -Force ``` diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index bd583e74..723c9ffa 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -210,6 +210,7 @@ foreach ($sub in $subscriptions){ if ($EnableESU) { if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($EnableESU -eq "No")) { $settings["enableExtendedSecurityUpdates"] = ($EnableESU -eq "Yes") + $settings["esuLastUpdatedTimestamp"] = [DateTime]::UtcNow.ToString((Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern) $WriteSettings = $true } else { write-host "The configured license type does not support ESUs" From d7d6d3b65dfbb3e5c1252191427d0787f1d4be5d Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Tue, 11 Jul 2023 17:27:16 -0700 Subject: [PATCH 11/14] Corrected ESU timestamp --- .../modify-license-type/modify-license-type.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 723c9ffa..01673d28 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -210,7 +210,7 @@ foreach ($sub in $subscriptions){ if ($EnableESU) { if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($EnableESU -eq "No")) { $settings["enableExtendedSecurityUpdates"] = ($EnableESU -eq "Yes") - $settings["esuLastUpdatedTimestamp"] = [DateTime]::UtcNow.ToString((Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern) + $settings["esuLastUpdatedTimestamp"] = [DateTime]::UtcNow.ToString('yyyy-MM-ddTHH:mm:ss.fffZ') $WriteSettings = $true } else { write-host "The configured license type does not support ESUs" From e569d415aa3ea3dfa9ce87f62c43ebb05b4470bd Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Thu, 13 Jul 2023 15:21:37 -0700 Subject: [PATCH 12/14] Optimized LoadModule --- .../modify-license-type.ps1 | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 01673d28..65d74402 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -39,31 +39,58 @@ param ( [switch] $Force ) -function CheckModule ($m) { - # This function ensures that the specified module is imported into the session - # If module is already imported - do nothing +function LoadModule +{ + param ( + [parameter(Mandatory = $true)][string] $name + ) - if (!(Get-Module | Where-Object {$_.Name -eq $m})) { - # If module is not imported, but available on disk then import - if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) { - Import-Module $m + $retVal = $true + + if (!(Get-Module -Name $name)) + { + $retVal = Get-Module -ListAvailable | Where-Object {$_.Name -eq $name) + + if ($retVal) + { + try + { + Import-Module $name -ErrorAction SilentlyContinue + } + catch + { + write-host "The request to lload module $($name) failed with the following error:" + write-host $_.Exception.Message + $retVal = $false + } } else { # If module is not imported, not available on disk, but is in online gallery then install and import - if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) { - Install-Module -Name $m -Force -Verbose -Scope CurrentUser - Import-Module $m + if (Find-Module -Name $name) { + Install-Module -Name $name -Force -Verbose -Scope CurrentUser + try + { + Import-Module $name -ErrorAction SilentlyContinue + } + catch + { + write-host "The request to lload module $($name) failed with the following error:" + write-host $_.Exception.Message + $retVal = $false + } } else { # If module is not imported, not available and not in online gallery then abort - write-host "Module $m not imported, not available and not in online gallery, exiting." + write-host "Module $($name) not imported, not available and not in online gallery, exiting." EXIT 1 } } } + + return $retVal } function ConvertTo-Hashtable { @@ -117,7 +144,7 @@ $requiredModules = @( "Az.ConnectedMachine", "Az.ResourceGraph" ) -$requiredModules | Foreach-Object {CheckModule $_} +$requiredModules | Foreach-Object {LoadModule $_} # Subscriptions to scan From 7eaf3bd368ba71b66b029083c7efef00b4cb7ff9 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Thu, 13 Jul 2023 15:29:48 -0700 Subject: [PATCH 13/14] Optimized LoadModule --- .../modify-license-type.ps1 | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 65d74402..b0104cd4 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -39,7 +39,46 @@ param ( [switch] $Force ) +function ConvertTo-Hashtable { + [CmdletBinding()] + [OutputType('hashtable')] + param ( + [Parameter(ValueFromPipeline)] + $InputObject + ) + process { + ## Return null if the input is null. This can happen when calling the function + ## recursively and a property is null + if ($null -eq $InputObject) { + return $null + } + ## Check if the input is an array or collection. If so, we also need to convert + ## those types into hash tables as well. This function will convert all child + ## objects into hash tables (if applicable) + if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) { + $collection = @( + foreach ($object in $InputObject) { + ConvertTo-Hashtable -InputObject $object + } + ) + ## Return the array but don't enumerate it because the object may be pretty complex + Write-Output -NoEnumerate $collection + } elseif ($InputObject -is [psobject]) { + ## If the object has properties that need enumeration, cxonvert it to its own hash table and return it + $hash = @{} + foreach ($property in $InputObject.PSObject.Properties) { + $hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value + } + $hash + } else { + ## If the object isn't an array, collection, or other object, it's already a hash table + ## So just return it. + $InputObject + } + } +} +# This function checks if the specified module is imported into the session and if not installes and/or imports it function LoadModule { param ( @@ -93,45 +132,6 @@ function LoadModule return $retVal } -function ConvertTo-Hashtable { - [CmdletBinding()] - [OutputType('hashtable')] - param ( - [Parameter(ValueFromPipeline)] - $InputObject - ) - process { - ## Return null if the input is null. This can happen when calling the function - ## recursively and a property is null - if ($null -eq $InputObject) { - return $null - } - ## Check if the input is an array or collection. If so, we also need to convert - ## those types into hash tables as well. This function will convert all child - ## objects into hash tables (if applicable) - if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) { - $collection = @( - foreach ($object in $InputObject) { - ConvertTo-Hashtable -InputObject $object - } - ) - ## Return the array but don't enumerate it because the object may be pretty complex - Write-Output -NoEnumerate $collection - } elseif ($InputObject -is [psobject]) { - ## If the object has properties that need enumeration, cxonvert it to its own hash table and return it - $hash = @{} - foreach ($property in $InputObject.PSObject.Properties) { - $hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value - } - $hash - } else { - ## If the object isn't an array, collection, or other object, it's already a hash table - ## So just return it. - $InputObject - } - } -} - # # Suppress warnings # From 6e4829e6d00ea4b51d0a37e6e85bd7679346ad9e Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Thu, 20 Jul 2023 10:28:24 -0700 Subject: [PATCH 14/14] Fixed a typo --- .../modify-license-type/modify-license-type.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index b0104cd4..8c7e8a3f 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -89,7 +89,7 @@ function LoadModule if (!(Get-Module -Name $name)) { - $retVal = Get-Module -ListAvailable | Where-Object {$_.Name -eq $name) + $retVal = Get-Module -ListAvailable | Where-Object {$_.Name -eq $name} if ($retVal) {