Skip to content

Commit e34a0b1

Browse files
author
Alexander (Sasha) Nosov
committed
Final cleanup
1 parent ed771c7 commit e34a0b1

File tree

2 files changed

+90
-42
lines changed

2 files changed

+90
-42
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ ms.date: 2/09/2023
1111

1212

1313
This script allows you to to set or change the license type on all Azure-connected SQL Servers
14-
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.
14+
on a specific resource, in a single resource group, 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 the selected scope.
1515

1616
You can specify a single subscription to scan, or provide a list of subscriptions as a .CSV file.
17-
If not specified, all subscriptions your role has access to are scanned.
17+
If not specified, all subscriptions your role has access to are scanned.
18+
19+
If the license type is not specified, the value "Paid" is used.
1820

1921

2022
# Required permissions
2123

22-
You must be at least a *Contributor* of each subscription you modify.
24+
You must have at least a *Contributor* role in each subscription you modify.
2325

2426
# Launching the script
2527

@@ -28,6 +30,8 @@ The script accepts the following command line parameters:
2830
| **Parameter**                                         | **Value**                                                                       | **Description** |
2931
|:--|:--|:--|
3032
|-SubId|subscription_id *or* a file_name|Optional: subscription id or a .csv file with the list of subscriptions<sup>1</sup>|
33+
|-ResourceGroup |resource_group_name|Optional: Limit the scope to a specific resource group|
34+
|-MachineName |machine_name|Optional: Limit the scope to a specific machine|
3135
|-LicenceType | "Paid" (default), "PAYG" or "LicenseOnly"| Specifies the license type value |
3236
|-All|\$True or \$False (default)|Optional: Set the new license type value only if undefined|
3337

@@ -66,7 +70,7 @@ Use the following steps to run the script in Cloud Shell.
6670
3. Run the script.
6771

6872
```console
69-
.//modify-license-type.ps1 -LicenseType "Paid"
73+
.//modify-license-type.ps1 -LicenseType "PAYG"
7074
```
7175

7276
> [!NOTE]

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

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
#
99
# The script accepts the following command line parameters:
1010
#
11-
# -SubId [subscription_id] | [csv_file_name] (Accepts a .csv file with the list of subscriptions)
12-
# -LicenceType [license_type_value] (Specific LT value)
11+
# -SubId [subscription_id] | [csv_file_name] (Limit scope to specific subscriptions. Accepts a .csv file with the list of subscriptions)
12+
# -ResourceGroup [resource_goup] (Limit scope to a specific resoure group)
13+
# -MachineName [machine_name] (Limit scope to a specific machine)
14+
# -LicenseType [license_type_value] (Specific LT value)
1315
# -All (Optional. Set the new license type value only if undefined)
1416
#
1517

1618
param (
1719
[Parameter (Mandatory= $false)]
1820
[string] $SubId,
21+
[Parameter (Mandatory= $false)]
22+
[string] $ResourceGroup,
23+
[Parameter (Mandatory= $false)]
24+
[string] $MachineName,
1925
[Parameter (Mandatory= $false)]
2026
[string] $LicenseType="Paid",
2127
[Parameter (Mandatory= $false)]
22-
[string] $SkipServers,
23-
[Parameter (Mandatory= $false)]
2428
[boolean] $All=$false
2529
)
2630

@@ -51,6 +55,44 @@ function CheckModule ($m) {
5155
}
5256
}
5357

58+
function ObjectToHashtable {
59+
[CmdletBinding()]
60+
[OutputType('hashtable')]
61+
param (
62+
[Parameter(ValueFromPipeline)]
63+
$InputObject
64+
)
65+
process {
66+
## Return null if the input is null. This can happen when calling the function
67+
## recursively and a property is null
68+
if ($null -eq $InputObject) {
69+
return $null
70+
}
71+
## Check if the input is an array or collection. If so, we also need to convert
72+
## those types into hash tables as well. This function will convert all child
73+
## objects into hash tables (if applicable)
74+
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
75+
$collection = @(
76+
foreach ($object in $InputObject) {
77+
ObjectToHashtable -InputObject $object
78+
}
79+
)
80+
## Return the array but don't enumerate it because the object may be pretty complex
81+
Write-Output -NoEnumerate $collection
82+
} elseif ($InputObject -is [psobject]) {
83+
## If the object has properties that need enumeration, cxonvert it to its own hash table and return it
84+
$hash = @{}
85+
foreach ($property in $InputObject.PSObject.Properties) {
86+
$hash[$property.Name] = ObjectToHashtable -InputObject $property.Value
87+
}
88+
$hash
89+
} else {
90+
## If the object isn't an array, collection, or other object, it's already a hash table
91+
## So just return it.
92+
$InputObject
93+
}
94+
}
95+
}
5496

5597
#
5698
# Suppress warnings
@@ -87,51 +129,53 @@ foreach ($sub in $subscriptions){
87129
try {
88130
Set-AzContext -SubscriptionId $sub.Id
89131
}catch {
90-
write-host "Invalid subscription: " $sub.Id
132+
write-host "Invalid subscription: $($sub.Id)"
91133
{continue}
92134
}
93135

94136
$query = "
95-
resources
96-
| where type =~ 'microsoft.hybridcompute/machines/extensions'
97-
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type)
98-
| where extensionPublisher =~ 'Microsoft.AzureData'
99-
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
100-
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
137+
resources
138+
| where type =~ 'microsoft.hybridcompute/machines/extensions'
139+
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type), provisioningState = tostring(properties.provisioningState)
140+
| where extensionPublisher =~ 'Microsoft.AzureData'
141+
| where provisioningState =~ 'Succeeded'
142+
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
143+
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
101144
"
102145

103-
Search-AzGraph -Query $query | ForEach-Object {
104-
146+
if ($MachineName) {$query += "| where machineName =~ '$($MachineName)'"}
147+
if ($ResourceGroup) {$query += "| where resourceGroup =~ '$($ResourceGroup)'"}
148+
149+
$resources = Search-AzGraph -Query "$($query) | where subscriptionId =~ '$($sub.Id)'"
150+
foreach ($r in $resources) {
151+
105152
$setID = @{
106-
MachineName = $_.MachineName
107-
Name = $_.extensionName
108-
ResourceGroup = $_.resourceGroup
109-
Location = $_.location
110-
SubscriptionId = $_.subscriptionId
111-
Publisher = $_.extensionPublisher
112-
ExtensionType = $_.extensionType
113-
}
114-
$getID = @{
115-
Name = $_.extensionName
116-
MachineName = $_.MachineName
117-
ResourceGroup = $_.resourceGroup
118-
SubscriptionId = $_.subscriptionId
153+
MachineName = $r.MachineName
154+
Name = $r.extensionName
155+
ResourceGroup = $r.resourceGroup
156+
Location = $r.location
157+
SubscriptionId = $r.subscriptionId
158+
Publisher = $r.extensionPublisher
159+
ExtensionType = $r.extensionType
119160
}
120-
$old = Get-AzConnectedMachineExtension @getID
161+
121162
$settings = @{}
122-
foreach( $property in $_.properties.settings.psobject.properties.name ){ $settings[$property] = $_.properties.settings.$property }
123-
if (-not $all) {
124-
if (-not $settings.ContainsKey("LicenseType")) { $settings["LicenseType"] = $LicenseType }
125-
} else {
126-
$settings["LicenseType"] = $LicenseType
127-
}
128-
if ($_.properties.provisioningState -ne "Succeeded") {
129-
Write-Warning "Skipping extension on server $($_.machineName) because it's state is $($_.properties.provisioningState)";
163+
$settings = $r.properties.settings | ConvertTo-Json | ConvertFrom-Json | ObjectToHashtable
164+
165+
if ($settings.ContainsKey("LicenseType")) {
166+
if ($All) {
167+
if ($settings["LicenseType"] -ne $LicenseType ) {
168+
$settings["LicenseType"] = $LicenseType
169+
Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])]"
170+
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait
171+
}
172+
}
130173
} else {
131-
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait
132-
$new = Get-AzConnectedMachineExtension @getID
174+
$settings["LicenseType"] = $LicenseType
175+
Write-Host "Resource group: [$($r.resourceGroup)] Connected machine: [$($r.MachineName)] : License type: [$($settings["LicenseType"])]"
176+
Set-AzConnectedMachineExtension @setId -Settings $settings
133177
}
134-
}
178+
}
135179
}
136180

137181

0 commit comments

Comments
 (0)