Skip to content

Commit 188848c

Browse files
author
Alexander (Sasha) Nosov
committed
Added prod sub example
1 parent be836e5 commit 188848c

File tree

3 files changed

+61
-798
lines changed

3 files changed

+61
-798
lines changed

samples/manage/azure-hybrid-benefit/modify-license-type/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,36 @@ The following command will scan all the subscriptions in tenant `<tenant_id>`, a
119119

120120
## Example 2
121121

122+
The following commands will create a list of production subscriptions in tenant `<tenant_id>`, and generates the list of the resources that would change the license type to "LicenseIncluded".
123+
124+
```PowerShell
125+
$tenantId = "<tenant-id>"
126+
Get-AzSubscription -TenantId $tenantId | Where-Object {
127+
$sub = $_
128+
$details = Get-AzSubscription -SubscriptionId $sub.Id -TenantId $tenantId
129+
if ($details -and $details.ExtendedProperties -and $details.ExtendedProperties.SubscriptionPolicies) {
130+
$quotaId = ($details.ExtendedProperties.SubscriptionPolicies | ConvertFrom-Json).quotaId
131+
return $quotaId -notmatch 'MSDN|DEV|VS|TEST'
132+
}
133+
return $false
134+
} | Export-Csv .\mysubscriptions.csv -NoTypeInformation
135+
.\modify-azure-sql-license-type.ps1 -TenantId <tenant_id> -SubId .\mysubscriptions.csv -LicenseType LicenseIncluded -ReportOnly
136+
```
137+
138+
## Example 3
139+
122140
The following command will scan resource group `<resource_group_name>` in the subscription `<sub_id>` within the current tenant, set the license type value to "LicenseIncluded" on each resource that has a different license type.
123141

124142
```PowerShell
125143
.\modify-azure-sql-license-type.ps1 -SubId <sub_id> -ResourceGroup <resource_group_name> -LicenseType LicenseIncluded
126144
```
127145

128-
## Example 3
146+
## Example 4
129147

130148
The following command will scan all subscriptions in the account using managed identity, set the license type value to "LicenseIncluded" on all resources in tenant <tenant_id> that have a different license type. The resources with the tag `Environment:Dev` will be excluded.
131149

132150
```PowerShell
133-
.\modify-azure-sql-license-type.ps1 -TenantId <tenant_id> -LicenseType LicenseIncluded -UseManagedIdentity -ExclusionTags {"Environment":"Dev"}
151+
.\modify-azure-sql-license-type.ps1 -TenantId <tenant_id> -LicenseType LicenseIncluded -UseManagedIdentity -ExclusionTags '{"Environment":"Dev"}'
134152
```
135153

136154
# Running the script using Cloud Shell

samples/manage/azure-hybrid-benefit/modify-license-type/modify-azure-sql-license-type.ps1

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ $finalStatus = @()
136136

137137
# Convert to hashtable explicitly
138138
$tagTable = @{}
139-
if(
140-
$null -ne $ExclusionTags){
139+
if($ExclusionTags){
141140
if($ExclusionTags.GetType().Name -eq "Hashtable"){
142141
$tagTable = $ExclusionTags
143142
}else{
@@ -391,15 +390,16 @@ foreach ($sub in $subscriptions) {
391390
}
392391

393392
# --- Section: Update SQL Databases and Elastic Pools ---
393+
394394
try {
395-
Write-Output "Querying SQL Servers within this subscription..."
395+
Write-Output "Querying SQL Servers within this subscription..."
396396

397397
# First, let's verify we're in the right subscription context
398398
$currentSubContext = az account show --query id -o tsv
399-
Write-Output "Currently in subscription context: $currentSubContext"
399+
Write-Output "Currently in subscription context: $currentSubContext"
400400

401401
if ($currentSubContext -ne $sub.id) {
402-
Write-Output "Subscription context mismatch! Re-setting context..."
402+
Write-Output "Subscription context mismatch! Re-setting context..."
403403
az account set --subscription $sub.id
404404
}
405405

@@ -427,7 +427,6 @@ foreach ($sub in $subscriptions) {
427427
}
428428
}
429429

430-
431430
# Add tag filter if specified
432431
if ($tagsFilter -and $filterAdded) {
433432
$serverQuery += "$tagsFilter"
@@ -442,45 +441,43 @@ foreach ($sub in $subscriptions) {
442441
}
443442

444443
# Output the query for debugging
445-
Write-Output "SQL Server query: $serverQuery"
444+
Write-Output "SQL Server query: $serverQuery"
446445

447-
<# Get all servers first as a fallback in case the query fails
446+
# Get all servers first as a fallback in case the query fails
448447
$allServers = az sql server list -o json | ConvertFrom-Json
449-
Write-Output "Found a total of $($allServers.Count) SQL Servers in subscription"
450-
#>
448+
Write-Output "Found a total of $($allServers.Count) SQL Servers in subscription"
451449

452450
# Now try the filtered query
453451
$servers = az sql server list --query "$serverQuery" -o json | ConvertFrom-Json
454452

455453
# Verify if we got any results
456454
if ($null -eq $servers -or $servers.Count -eq 0) {
457-
Write-Output "WARNING: No SQL Servers found with the specified filters."
458-
<# Write-Output "Available SQL Servers in subscription:"
455+
Write-Output "WARNING: No SQL Servers found with the specified filters."
456+
Write-Output "Available SQL Servers in subscription:"
459457
$allServers | ForEach-Object {
460-
Write-Output " - $($_.name) (Resource Group: $($_.resourceGroup))"
458+
Write-Output " - $($_.name) (Resource Group: $($_.resourceGroup))"
461459
}
462-
#>
463460

464461
# Use all servers if no specific resource name was provided
465462
if (-not $ResourceName) {
466-
Write-Output "Proceeding with all SQL Servers since no specific ResourceName was provided."
463+
Write-Output "Proceeding with all SQL Servers since no specific ResourceName was provided."
467464
$servers = $allServers
468465
}
469466
} else {
470-
Write-Output "Found $($servers.Count) SQL Servers matching the criteria."
467+
Write-Output "Found $($servers.Count) SQL Servers matching the criteria."
471468
$servers | ForEach-Object {
472-
Write-Output " - $($_.name) (Resource Group: $($_.resourceGroup))"
469+
Write-Output " - $($_.name) (Resource Group: $($_.resourceGroup))"
473470
}
474471
}
475472

476473
# Process each server
477474
foreach ($server in $servers) {
478475
# Update SQL Databases
479-
Write-Output "Scanning SQL Databases on server '$($server.name)' in resource group '$($server.resourceGroup)'..."
476+
Write-Output "Scanning SQL Databases on server '$($server.name)' in resource group '$($server.resourceGroup)'..."
480477

481478
# First get all databases to check if any exist
482479
$allDbs = az sql db list --resource-group $server.resourceGroup --server $server.name -o json | ConvertFrom-Json
483-
Write-Output "Found a total of $($allDbs.Count) databases on server '$($server.name)'"
480+
Write-Output "Found a total of $($allDbs.Count) databases on server '$($server.name)'"
484481

485482
# Build database query with better error handling
486483
$dbQuery = "[?licenseType!=null && licenseType!='$($LicenseType)'"
@@ -492,20 +489,20 @@ foreach ($sub in $subscriptions) {
492489

493490
$dbQuery += "].{name:name, licenseType:licenseType, location:location, resourceGroup:resourceGroup, id:id, ResourceType:type, State:status}"
494491

495-
Write-Output "Database query: $dbQuery"
492+
Write-Output "Database query: $dbQuery"
496493

497494
# Get databases with error handling
498495
try {
499496
$dbs = az sql db list --resource-group $server.resourceGroup --server $server.name --query "$dbQuery" -o json | ConvertFrom-Json
500497

501498
if ($null -eq $dbs) {
502-
Write-Output "No SQL Databases found on Server $($server.name) that require a license update."
499+
Write-Output "No SQL Databases found on Server $($server.name) that require a license update."
503500
} elseif ($dbs.Count -eq 0) {
504-
Write-Output "No SQL Databases found on Server $($server.name) that require a license update."
501+
Write-Output "No SQL Databases found on Server $($server.name) that require a license update."
505502
} else {
506-
Write-Output "Found $($dbs.Count) SQL Databases on Server $($server.name) that require a license update:"
503+
Write-Output "Found $($dbs.Count) SQL Databases on Server $($server.name) that require a license update:"
507504
$dbs | ForEach-Object {
508-
Write-Output " - $($_.name) (Current license: $($_.licenseType))"
505+
Write-Output " - $($_.name) (Current license: $($_.licenseType))"
509506
}
510507

511508
foreach ($db in $dbs) {
@@ -522,36 +519,36 @@ foreach ($sub in $subscriptions) {
522519
}
523520

524521
if (-not $ReportOnly) {
525-
Write-Output "Updating SQL Database '$($db.name)' on server '$($server.name)' to license type '$LicenseType'..."
522+
Write-Output "Updating SQL Database '$($db.name)' on server '$($server.name)' to license type '$LicenseType'..."
526523
try {
527524
$result = az sql db update --name $db.name --server $server.name --resource-group $server.resourceGroup --set licenseType=$LicenseType -o json | ConvertFrom-Json
528525
if ($result) {
529-
Write-Output "Successfully updated database '$($db.name)' license to '$LicenseType'"
526+
Write-Output "Successfully updated database '$($db.name)' license to '$LicenseType'"
530527
$finalStatus += $result
531528
} else {
532-
Write-Error "Failed to update database '$($db.name)' license. No result returned."
529+
Write-Output "Failed to update database '$($db.name)' license. No result returned."
533530
}
534531
} catch {
535-
Write-Error "Error updating database '$($db.name)': $_"
532+
Write-Output "Error updating database '$($db.name)': $_"
536533
}
537534
}
538535
}
539536
}
540537
} catch {
541-
Write-Error "Error querying databases on server '$($server.name)': $_"
538+
Write-Output "Error querying databases on server '$($server.name)': $_"
542539
}
543540

544541
# Update Elastic Pools with similar improved error handling
545542
try {
546-
Write-Output "Scanning Elastic Pools on server '$($server.name)'..."
543+
Write-Output "Scanning Elastic Pools on server '$($server.name)'..."
547544

548545
# First check if there are any elastic pools
549546
$allPools = az sql elastic-pool list --resource-group $server.resourceGroup --server $server.name --only-show-errors -o json 2>$null | ConvertFrom-Json -ErrorAction SilentlyContinue
550547

551548
if ($null -eq $allPools -or $allPools.Count -eq 0) {
552-
Write-Output "No Elastic Pools found on server '$($server.name)'."
549+
Write-Output "No Elastic Pools found on server '$($server.name)'."
553550
} else {
554-
Write-Output "Found $($allPools.Count) total Elastic Pools on server '$($server.name)'."
551+
Write-Output "Found $($allPools.Count) total Elastic Pools on server '$($server.name)'."
555552

556553
# Build elastic pool query with better formatting
557554
$elasticPoolQuery = "[?licenseType!=null && licenseType!='$($LicenseType)'"
@@ -563,16 +560,16 @@ foreach ($sub in $subscriptions) {
563560

564561
$elasticPoolQuery += "].{name:name, licenseType:licenseType, location:location, resourceGroup:resourceGroup, id:id, ResourceType:type, State:state}"
565562

566-
Write-Output "Elastic Pool query: $elasticPoolQuery"
563+
Write-Output "Elastic Pool query: $elasticPoolQuery"
567564

568565
$elasticPools = az sql elastic-pool list --resource-group $server.resourceGroup --server $server.name --query "$elasticPoolQuery" --only-show-errors -o json 2>$null | ConvertFrom-Json -ErrorAction SilentlyContinue
569566

570567
if ($null -eq $elasticPools -or $elasticPools.Count -eq 0) {
571-
Write-Output "No Elastic Pools found on Server $($server.name) that require a license update."
568+
Write-Output "No Elastic Pools found on Server $($server.name) that require a license update."
572569
} else {
573-
Write-Output "Found $($elasticPools.Count) Elastic Pools on Server $($server.name) that require a license update:"
570+
Write-Output "Found $($elasticPools.Count) Elastic Pools on Server $($server.name) that require a license update:"
574571
$elasticPools | ForEach-Object {
575-
Write-Output " - $($_.name) (Current license: $($_.licenseType))"
572+
Write-Output " - $($_.name) (Current license: $($_.licenseType))"
576573
}
577574

578575
foreach ($pool in $elasticPools) {
@@ -589,28 +586,28 @@ foreach ($sub in $subscriptions) {
589586
}
590587

591588
if (-not $ReportOnly) {
592-
Write-Output "Updating Elastic Pool '$($pool.name)' on server '$($server.name)' to license type '$LicenseType'..."
589+
Write-Output "Updating Elastic Pool '$($pool.name)' on server '$($server.name)' to license type '$LicenseType'..."
593590
try {
594591
$result = az sql elastic-pool update --name $pool.name --server $server.name --resource-group $server.resourceGroup --set licenseType=$LicenseType --only-show-errors -o json 2>$null | ConvertFrom-Json -ErrorAction SilentlyContinue
595592
if ($result) {
596-
Write-Output "Successfully updated elastic pool '$($pool.name)' license to '$LicenseType'"
593+
Write-Output "Successfully updated elastic pool '$($pool.name)' license to '$LicenseType'"
597594
$finalStatus += $result
598595
} else {
599-
Write-Error "Failed to update elastic pool '$($pool.name)' license. No result returned."
596+
Write-Output "Failed to update elastic pool '$($pool.name)' license. No result returned."
600597
}
601598
} catch {
602-
Write-Error "Error updating elastic pool '$($pool.name)': $_"
599+
Write-Output "Error updating elastic pool '$($pool.name)': $_"
603600
}
604601
}
605602
}
606603
}
607604
}
608605
} catch {
609-
Write-Error "Error processing Elastic Pools on server '$($server.name)': $_"
606+
Write-Output "Error processing Elastic Pools on server '$($server.name)': $_"
610607
}
611608
}
612609
} catch {
613-
Write-Error "An error occurred while processing SQL Databases or Elastic Pools: $_"
610+
Write-Output "An error occurred while processing SQL Databases or Elastic Pools: $_"
614611
}
615612

616613
# --- Section: Update SQL Instance Pools ---
@@ -675,7 +672,7 @@ foreach ($sub in $subscriptions) {
675672
Get-AzDataFactoryV2 |
676673
Where-Object {
677674
$_.ProvisioningState -eq "Succeeded" -and
678-
($null -eq $ResourceGroup -or $_.ResourceGroupName -eq $ResourceGroup)
675+
([string]::IsNullOrEmpty($ResourceGroup) -or $_.ResourceGroupName -eq $ResourceGroup)
679676
} |
680677
ForEach-Object {
681678
$df = $_
@@ -684,7 +681,7 @@ foreach ($sub in $subscriptions) {
684681
$_.Type -eq "Managed" -and
685682
$_.State -ne "Starting" -and
686683
$_.LicenseType -ne $LicenseType -and
687-
($null -eq $ResourceName -or $_.Name -eq $ResourceName)
684+
([string]::IsNullOrEmpty($ResourceName) -or $_.Name -eq $ResourceName)
688685
}
689686

690687
if ($IRs.Count -eq 0) {

0 commit comments

Comments
 (0)