initial draft

This commit is contained in:
Alexander (Sasha) Nosov
2023-02-09 16:44:32 -08:00
parent 6a63a15592
commit ed771c7203
2 changed files with 212 additions and 0 deletions
@@ -0,0 +1,75 @@
---
services: Azure Arc-enabled SQL Server
platforms: Azure
author: anosov1960
ms.author: sashan
ms.date: 2/09/2023
---
# Overview
This script allows you to to set or change the license type on all Azure-connected SQL Servers
in a specific subscription, a list of subscriptions or the entire account. 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 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.
# Required permissions
You must be at least a *Contributor* of each subscription you modify.
# Launching the script
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 subscriptions<sup>1</sup>|
|-LicenceType | "Paid" (default), "PAYG" or "LicenseOnly"| Specifies the license type value |
|-All|\$True or \$False (default)|Optional: Set the new license type value only if undefined|
<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.
```PowerShell
Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
```
## Example 1
The following command will scan all the subscriptions to which the user has access to and set the license type to "PAYG".
```PowerShell
.\update-license-type.ps1 -LicenseType "PAYG" -All
```
## Example 2
The following command will scan the subscription `<sub_id>` and set the license type value to "Paid" on the servers where it is undefined.
```PowerShell
.\update-license-type.ps1 -SubId <sub_id> -LicenseType "Paid"
```
# Running the script using Cloud Shell
Use the following steps to run the script in Cloud Shell.
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
2. Upload the script to the shell using the following command:
```console
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
```
3. Run the script.
```console
.//modify-license-type.ps1 -LicenseType "Paid"
```
> [!NOTE]
> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS.
> - The script will be uploaded directly to the home folder associated with your Cloud Shell session.
@@ -0,0 +1,137 @@
#
# 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.
#
# 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] (Accepts a .csv file with the list of subscriptions)
# -LicenceType [license_type_value] (Specific LT value)
# -All (Optional. Set the new license type value only if undefined)
#
param (
[Parameter (Mandatory= $false)]
[string] $SubId,
[Parameter (Mandatory= $false)]
[string] $LicenseType="Paid",
[Parameter (Mandatory= $false)]
[string] $SkipServers,
[Parameter (Mandatory= $false)]
[boolean] $All=$false
)
function CheckModule ($m) {
# This function ensures that the specified module is imported into the session
# If module is already imported - do nothing
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
}
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
}
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."
EXIT 1
}
}
}
}
#
# Suppress warnings
#
Update-AzConfig -DisplayBreakingChangeWarning $false
# Load required modules
$requiredModules = @(
"Az.Accounts",
"Az.ConnectedMachine",
"Az.ResourceGraph"
)
$requiredModules | Foreach-Object {CheckModule $_}
# Subscriptions to scan
if ($SubId -like "*.csv") {
$subscriptions = Import-Csv $SubId
}elseif($SubId -ne $null){
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
}else{
$subscriptions = Get-AzSubscription
}
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --")
# Scan arc-enabled servers in each subscription
foreach ($sub in $subscriptions){
if ($sub.State -ne "Enabled") {continue}
try {
Set-AzContext -SubscriptionId $sub.Id
}catch {
write-host "Invalid subscription: " $sub.Id
{continue}
}
$query = "
resources
| where type =~ 'microsoft.hybridcompute/machines/extensions'
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type)
| where extensionPublisher =~ 'Microsoft.AzureData'
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
"
Search-AzGraph -Query $query | ForEach-Object {
$setID = @{
MachineName = $_.MachineName
Name = $_.extensionName
ResourceGroup = $_.resourceGroup
Location = $_.location
SubscriptionId = $_.subscriptionId
Publisher = $_.extensionPublisher
ExtensionType = $_.extensionType
}
$getID = @{
Name = $_.extensionName
MachineName = $_.MachineName
ResourceGroup = $_.resourceGroup
SubscriptionId = $_.subscriptionId
}
$old = Get-AzConnectedMachineExtension @getID
$settings = @{}
foreach( $property in $_.properties.settings.psobject.properties.name ){ $settings[$property] = $_.properties.settings.$property }
if (-not $all) {
if (-not $settings.ContainsKey("LicenseType")) { $settings["LicenseType"] = $LicenseType }
} else {
$settings["LicenseType"] = $LicenseType
}
if ($_.properties.provisioningState -ne "Succeeded") {
Write-Warning "Skipping extension on server $($_.machineName) because it's state is $($_.properties.provisioningState)";
} else {
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait
$new = Get-AzConnectedMachineExtension @getID
}
}
}