Skip to content

Commit 79e88a9

Browse files
authored
reverted to Dec 1, 2024
1 parent 45276b4 commit 79e88a9

2 files changed

Lines changed: 68 additions & 40 deletions

File tree

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ The script accepts the following command line parameters:
3838
```PowerShell
3939
Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
4040
```
41-
> [!IMPORTANT]
42-
> If you are setting License type to "PAYG" in the CSP subscription(s), the subscription resource must have a consent tag enabling perpetual pay-as-you-go billing. Without the tag, the command will fail. The consent tag is `SQLPerpetualPaygBilling`:`Enabled` and can be added using [Azure portal](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources-portal), [PowerShell](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources-powershell) or [CLI](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources-cli).
43-
4441
## Example 1
4542

46-
The following command will scan all the subscriptions to which the user has access to, and set the license type to "Paid" on all servers where license type is undefined.
43+
The following command will scan all the subscriptions to which the user has access to, and set the license type to "PAYG" on all servers where license type is undefined.
4744

4845
```PowerShell
49-
.\modify-license-type.ps1 -LicenseType Paid
46+
.\modify-license-type.ps1 -LicenseType PAYG
5047
```
5148

5249
## Example 2

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

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,52 +52,82 @@ function ConvertTo-Hashtable {
5252
$InputObject
5353
)
5454
process {
55+
## Return null if the input is null. This can happen when calling the function
56+
## recursively and a property is null
5557
if ($null -eq $InputObject) {
5658
return $null
5759
}
58-
if ($InputObject -is [System.Collections.ICollection]) {
60+
## Check if the input is an array or collection. If so, we also need to convert
61+
## those types into hash tables as well. This function will convert all child
62+
## objects into hash tables (if applicable)
63+
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
64+
$collection = @(
65+
foreach ($object in $InputObject) {
66+
ConvertTo-Hashtable -InputObject $object
67+
}
68+
)
69+
## Return the array but don't enumerate it because the object may be pretty complex
70+
Write-Output -NoEnumerate $collection
71+
} elseif ($InputObject -is [psobject]) {
72+
## If the object has properties that need enumeration, cxonvert it to its own hash table and return it
5973
$hash = @{}
60-
foreach ($property in $InputObject) {
74+
foreach ($property in $InputObject.PSObject.Properties) {
6175
$hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value
6276
}
6377
$hash
6478
} else {
79+
## If the object isn't an array, collection, or other object, it's already a hash table
80+
## So just return it.
6581
$InputObject
6682
}
6783
}
6884
}
6985

70-
function LoadModule {
86+
# This function checks if the specified module is imported into the session and if not installes and/or imports it
87+
function LoadModule
88+
{
7189
param (
7290
[parameter(Mandatory = $true)][string] $name
7391
)
7492

7593
$retVal = $true
7694

77-
if (!(Get-Module -Name $name)) {
95+
if (!(Get-Module -Name $name))
96+
{
7897
$retVal = Get-Module -ListAvailable | Where-Object {$_.Name -eq $name}
7998

80-
if ($retVal) {
81-
try {
99+
if ($retVal)
100+
{
101+
try
102+
{
82103
Import-Module $name -ErrorAction SilentlyContinue
83104
}
84-
catch {
85-
write-host "The request to load module $($name) failed with the following error:"
105+
catch
106+
{
107+
write-host "The request to lload module $($name) failed with the following error:"
86108
write-host $_.Exception.Message
87109
$retVal = $false
88110
}
89-
} else {
111+
}
112+
else {
113+
114+
# If module is not imported, not available on disk, but is in online gallery then install and import
90115
if (Find-Module -Name $name) {
91116
Install-Module -Name $name -Force -Verbose -Scope CurrentUser
92-
try {
93-
Import-Module $name -ErrorAction SilentlyContinue
117+
try
118+
{
119+
Import-Module $name -ErrorAction SilentlyContinue
94120
}
95-
catch {
121+
catch
122+
{
96123
write-host "The request to load module $($name) failed with the following error:"
97124
write-host $_.Exception.Message
98125
$retVal = $false
99126
}
100-
} else {
127+
}
128+
else {
129+
130+
# If module is not imported, not available and not in online gallery then abort
101131
write-host "Module $($name) not imported, not available and not in online gallery, exiting."
102132
EXIT 1
103133
}
@@ -107,8 +137,12 @@ function LoadModule {
107137
return $retVal
108138
}
109139

140+
#
141+
# Suppress warnings
142+
#
110143
Update-AzConfig -DisplayBreakingChangeWarning $false
111144

145+
# Load required modules
112146
$requiredModules = @(
113147
"AzureAD",
114148
"Az.Accounts",
@@ -117,19 +151,25 @@ $requiredModules = @(
117151
)
118152
$requiredModules | Foreach-Object {LoadModule $_}
119153

154+
# Subscriptions to scan
155+
120156
$tenantID = (Get-AzureADTenantDetail).ObjectId
121157

122158
if ($SubId -like "*.csv") {
123159
$subscriptions = Import-Csv $SubId
124-
}elseif($SubId -ne "") {
160+
}elseif($SubId -ne ""){
125161
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription -TenantID $tenantID
126-
}else {
162+
}else{
127163
$subscriptions = Get-AzSubscription -TenantID $tenantID
128164
}
129165

166+
130167
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --")
131168

132-
foreach ($sub in $subscriptions) {
169+
# Scan arc-enabled servers in each subscription
170+
171+
foreach ($sub in $subscriptions){
172+
133173
if ($sub.State -ne "Enabled") {continue}
134174

135175
try {
@@ -139,23 +179,6 @@ foreach ($sub in $subscriptions) {
139179
{continue}
140180
}
141181

142-
# Consent tag enforcement on the CSP subscriptions
143-
if ($LicenseType -eq "PAYG") {
144-
$offers = @("MS-AZR-0145P", "MS-AZR-DE-0145P", "MS-AZR-0017G", "MS-AZR-159P", "MS-AZR-USGOV-0145P")
145-
$subscriptionOffers = Get-AzSubscription -SubscriptionId $sub.Id | Select-Object -ExpandProperty OfferId
146-
if ($subscriptionOffers -contains $offers) {
147-
if ($tags.Tags.ContainsKey("SQLPerpetualPaygBilling")) {
148-
if ($tags.Tags["SQLPerpetualPaygBilling"] -ne "Enabled") {
149-
write-host "Error: Subscription $($sub.Id) has an incorrect value $($tags.Tags["SQLPerpetualPaygBilling"]) of the consent tag 'SQLPerpetualPaygBilling' ."
150-
continue
151-
}
152-
} else {
153-
write-host "Error: Subscription $($sub.Id) does not have the consent tag 'SQLPerpetualPaygBilling'."
154-
continue
155-
}
156-
}
157-
}
158-
159182
$query = "
160183
resources
161184
| where type =~ 'microsoft.hybridcompute/machines/extensions'
@@ -180,6 +203,7 @@ foreach ($sub in $subscriptions) {
180203

181204
$resources = Search-AzGraph -Query "$($query)"
182205
foreach ($r in $resources) {
206+
183207
$setID = @{
184208
MachineName = $r.MachineName
185209
Name = $r.extensionName
@@ -194,6 +218,7 @@ foreach ($sub in $subscriptions) {
194218
$settings = @{}
195219
$settings = $r.properties.settings | ConvertTo-Json | ConvertFrom-Json | ConvertTo-Hashtable
196220

221+
# set the license type or update (if -Force). ESU must be disabled to set to LicenseOnly.
197222
$LO_Allowed = (!$settings["enableExtendedSecurityUpdates"] -and !$EnableESU) -or ($EnableESU -eq "No")
198223

199224
if ($LicenseType) {
@@ -210,8 +235,10 @@ foreach ($sub in $subscriptions) {
210235
$WriteSettings = $true
211236
}
212237
}
238+
213239
}
214240

241+
# Enable ESU for qualified license types or disable
215242
if ($EnableESU) {
216243
if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($EnableESU -eq "No")) {
217244
$settings["enableExtendedSecurityUpdates"] = ($EnableESU -eq "Yes")
@@ -222,6 +249,7 @@ foreach ($sub in $subscriptions) {
222249
}
223250
}
224251

252+
# Enable UsePcoreLicense for qualified license types or disable
225253
if ($UsePcoreLicense) {
226254
if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($UsePcoreLicense -eq "No")) {
227255
$settings["UsePhysicalCoreLicense"] = @{
@@ -234,14 +262,17 @@ foreach ($sub in $subscriptions) {
234262
}
235263
}
236264
If ($WriteSettings) {
265+
237266
try {
238-
Set-AzConnectedMachineExtension @setID -Settings $settings -NoWait | Out-Null
267+
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null
239268
Write-Host "Updated -- Resource group: [$($r.resourceGroup)], Connected machine: [$($r.MachineName)]"
240269
} catch {
241-
write-host "The request to modify the extension object failed with the following error:"
270+
write-host "The request to modify the extenion object failed with the following error:"
242271
write-host $_.Exception.Message
243272
{continue}
244273
}
245274
}
246275
}
247276
}
277+
278+

0 commit comments

Comments
 (0)