Skip to content

Commit af9a402

Browse files
authored
Added the consent tag for the CSP subs
1 parent 570aa22 commit af9a402

1 file changed

Lines changed: 30 additions & 66 deletions

File tree

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

Lines changed: 30 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -52,82 +52,52 @@ 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
5755
if ($null -eq $InputObject) {
5856
return $null
5957
}
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
58+
if ($InputObject -is [System.Collections.ICollection]) {
7359
$hash = @{}
74-
foreach ($property in $InputObject.PSObject.Properties) {
60+
foreach ($property in $InputObject) {
7561
$hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value
7662
}
7763
$hash
7864
} else {
79-
## If the object isn't an array, collection, or other object, it's already a hash table
80-
## So just return it.
8165
$InputObject
8266
}
8367
}
8468
}
8569

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-
{
70+
function LoadModule {
8971
param (
9072
[parameter(Mandatory = $true)][string] $name
9173
)
9274

9375
$retVal = $true
9476

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

99-
if ($retVal)
100-
{
101-
try
102-
{
80+
if ($retVal) {
81+
try {
10382
Import-Module $name -ErrorAction SilentlyContinue
10483
}
105-
catch
106-
{
107-
write-host "The request to lload module $($name) failed with the following error:"
84+
catch {
85+
write-host "The request to load module $($name) failed with the following error:"
10886
write-host $_.Exception.Message
10987
$retVal = $false
11088
}
111-
}
112-
else {
113-
114-
# If module is not imported, not available on disk, but is in online gallery then install and import
89+
} else {
11590
if (Find-Module -Name $name) {
11691
Install-Module -Name $name -Force -Verbose -Scope CurrentUser
117-
try
118-
{
119-
Import-Module $name -ErrorAction SilentlyContinue
92+
try {
93+
Import-Module $name -ErrorAction SilentlyContinue
12094
}
121-
catch
122-
{
95+
catch {
12396
write-host "The request to load module $($name) failed with the following error:"
12497
write-host $_.Exception.Message
12598
$retVal = $false
12699
}
127-
}
128-
else {
129-
130-
# If module is not imported, not available and not in online gallery then abort
100+
} else {
131101
write-host "Module $($name) not imported, not available and not in online gallery, exiting."
132102
EXIT 1
133103
}
@@ -137,12 +107,8 @@ function LoadModule
137107
return $retVal
138108
}
139109

140-
#
141-
# Suppress warnings
142-
#
143110
Update-AzConfig -DisplayBreakingChangeWarning $false
144111

145-
# Load required modules
146112
$requiredModules = @(
147113
"AzureAD",
148114
"Az.Accounts",
@@ -151,25 +117,19 @@ $requiredModules = @(
151117
)
152118
$requiredModules | Foreach-Object {LoadModule $_}
153119

154-
# Subscriptions to scan
155-
156120
$tenantID = (Get-AzureADTenantDetail).ObjectId
157121

158122
if ($SubId -like "*.csv") {
159123
$subscriptions = Import-Csv $SubId
160-
}elseif($SubId -ne ""){
124+
}elseif($SubId -ne "") {
161125
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription -TenantID $tenantID
162-
}else{
126+
}else {
163127
$subscriptions = Get-AzSubscription -TenantID $tenantID
164128
}
165129

166-
167130
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --")
168131

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

175135
try {
@@ -179,6 +139,18 @@ foreach ($sub in $subscriptions){
179139
{continue}
180140
}
181141

142+
if ($LicenseType -eq "PAYG") {
143+
$offers = @("MS-AZR-0145P", "MS-AZR-DE-0145P", "MS-AZR-0017G", "MS-AZR-159P", "MS-AZR-USGOV-0145P")
144+
$subscriptionOffers = Get-AzSubscription -SubscriptionId $sub.Id | Select-Object -ExpandProperty OfferId
145+
if ($subscriptionOffers -contains $offers) {
146+
$tags = Get-AzTag -ResourceId "/subscriptions/$($sub.Id)"
147+
if ($tags.Tags["SQLPerpetualPaygBilling"] -ne "Enabled") {
148+
write-host "Error: Subscription $($sub.Id) does not have the consent tag 'SQLPerpetualPaygBilling' enabled."
149+
continue
150+
}
151+
}
152+
}
153+
182154
$query = "
183155
resources
184156
| where type =~ 'microsoft.hybridcompute/machines/extensions'
@@ -203,7 +175,6 @@ foreach ($sub in $subscriptions){
203175

204176
$resources = Search-AzGraph -Query "$($query)"
205177
foreach ($r in $resources) {
206-
207178
$setID = @{
208179
MachineName = $r.MachineName
209180
Name = $r.extensionName
@@ -218,7 +189,6 @@ foreach ($sub in $subscriptions){
218189
$settings = @{}
219190
$settings = $r.properties.settings | ConvertTo-Json | ConvertFrom-Json | ConvertTo-Hashtable
220191

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

224194
if ($LicenseType) {
@@ -235,10 +205,8 @@ foreach ($sub in $subscriptions){
235205
$WriteSettings = $true
236206
}
237207
}
238-
239208
}
240209

241-
# Enable ESU for qualified license types or disable
242210
if ($EnableESU) {
243211
if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($EnableESU -eq "No")) {
244212
$settings["enableExtendedSecurityUpdates"] = ($EnableESU -eq "Yes")
@@ -249,7 +217,6 @@ foreach ($sub in $subscriptions){
249217
}
250218
}
251219

252-
# Enable UsePcoreLicense for qualified license types or disable
253220
if ($UsePcoreLicense) {
254221
if (($settings["LicenseType"] | select-string "Paid","PAYG") -or ($UsePcoreLicense -eq "No")) {
255222
$settings["UsePhysicalCoreLicense"] = @{
@@ -262,17 +229,14 @@ foreach ($sub in $subscriptions){
262229
}
263230
}
264231
If ($WriteSettings) {
265-
266232
try {
267-
Set-AzConnectedMachineExtension @setId -Settings $settings -NoWait | Out-Null
233+
Set-AzConnectedMachineExtension @setID -Settings $settings -NoWait | Out-Null
268234
Write-Host "Updated -- Resource group: [$($r.resourceGroup)], Connected machine: [$($r.MachineName)]"
269235
} catch {
270-
write-host "The request to modify the extenion object failed with the following error:"
236+
write-host "The request to modify the extension object failed with the following error:"
271237
write-host $_.Exception.Message
272238
{continue}
273239
}
274240
}
275241
}
276242
}
277-
278-

0 commit comments

Comments
 (0)