@@ -136,8 +136,7 @@ $finalStatus = @()
136
136
137
137
# Convert to hashtable explicitly
138
138
$tagTable = @ {}
139
- if (
140
- $null -ne $ExclusionTags ){
139
+ if ($ExclusionTags ){
141
140
if ($ExclusionTags.GetType ().Name -eq " Hashtable" ){
142
141
$tagTable = $ExclusionTags
143
142
}else {
@@ -391,15 +390,16 @@ foreach ($sub in $subscriptions) {
391
390
}
392
391
393
392
# --- Section: Update SQL Databases and Elastic Pools ---
393
+
394
394
try {
395
- Write-Output " Querying SQL Servers within this subscription..."
395
+ Write-Output " Querying SQL Servers within this subscription..."
396
396
397
397
# First, let's verify we're in the right subscription context
398
398
$currentSubContext = az account show -- query id - o tsv
399
- Write-Output " Currently in subscription context: $currentSubContext "
399
+ Write-Output " Currently in subscription context: $currentSubContext "
400
400
401
401
if ($currentSubContext -ne $sub.id ) {
402
- Write-Output " Subscription context mismatch! Re-setting context..."
402
+ Write-Output " Subscription context mismatch! Re-setting context..."
403
403
az account set -- subscription $sub.id
404
404
}
405
405
@@ -427,7 +427,6 @@ foreach ($sub in $subscriptions) {
427
427
}
428
428
}
429
429
430
-
431
430
# Add tag filter if specified
432
431
if ($tagsFilter -and $filterAdded ) {
433
432
$serverQuery += " $tagsFilter "
@@ -442,45 +441,43 @@ foreach ($sub in $subscriptions) {
442
441
}
443
442
444
443
# Output the query for debugging
445
- Write-Output " SQL Server query: $serverQuery "
444
+ Write-Output " SQL Server query: $serverQuery "
446
445
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
448
447
$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"
451
449
452
450
# Now try the filtered query
453
451
$servers = az sql server list -- query " $serverQuery " - o json | ConvertFrom-Json
454
452
455
453
# Verify if we got any results
456
454
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:"
459
457
$allServers | ForEach-Object {
460
- Write-Output " - $($_.name) (Resource Group: $($_.resourceGroup))"
458
+ Write-Output " - $ ( $_.name ) (Resource Group: $ ( $_.resourceGroup ) )"
461
459
}
462
- #>
463
460
464
461
# Use all servers if no specific resource name was provided
465
462
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."
467
464
$servers = $allServers
468
465
}
469
466
} else {
470
- Write-Output " Found $ ( $servers.Count ) SQL Servers matching the criteria."
467
+ Write-Output " Found $ ( $servers.Count ) SQL Servers matching the criteria."
471
468
$servers | ForEach-Object {
472
- Write-Output " - $ ( $_.name ) (Resource Group: $ ( $_.resourceGroup ) )"
469
+ Write-Output " - $ ( $_.name ) (Resource Group: $ ( $_.resourceGroup ) )"
473
470
}
474
471
}
475
472
476
473
# Process each server
477
474
foreach ($server in $servers ) {
478
475
# 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 ) '..."
480
477
481
478
# First get all databases to check if any exist
482
479
$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 ) '"
484
481
485
482
# Build database query with better error handling
486
483
$dbQuery = " [?licenseType!=null && licenseType!='$ ( $LicenseType ) '"
@@ -492,20 +489,20 @@ foreach ($sub in $subscriptions) {
492
489
493
490
$dbQuery += " ].{name:name, licenseType:licenseType, location:location, resourceGroup:resourceGroup, id:id, ResourceType:type, State:status}"
494
491
495
- Write-Output " Database query: $dbQuery "
492
+ Write-Output " Database query: $dbQuery "
496
493
497
494
# Get databases with error handling
498
495
try {
499
496
$dbs = az sql db list -- resource- group $server.resourceGroup -- server $server.name -- query " $dbQuery " - o json | ConvertFrom-Json
500
497
501
498
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."
503
500
} 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."
505
502
} 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:"
507
504
$dbs | ForEach-Object {
508
- Write-Output " - $ ( $_.name ) (Current license: $ ( $_.licenseType ) )"
505
+ Write-Output " - $ ( $_.name ) (Current license: $ ( $_.licenseType ) )"
509
506
}
510
507
511
508
foreach ($db in $dbs ) {
@@ -522,36 +519,36 @@ foreach ($sub in $subscriptions) {
522
519
}
523
520
524
521
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 '..."
526
523
try {
527
524
$result = az sql db update -- name $db.name -- server $server.name -- resource- group $server.resourceGroup -- set licenseType= $LicenseType - o json | ConvertFrom-Json
528
525
if ($result ) {
529
- Write-Output " Successfully updated database '$ ( $db.name ) ' license to '$LicenseType '"
526
+ Write-Output " Successfully updated database '$ ( $db.name ) ' license to '$LicenseType '"
530
527
$finalStatus += $result
531
528
} 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."
533
530
}
534
531
} catch {
535
- Write-Error " Error updating database '$ ( $db.name ) ': $_ "
532
+ Write-Output " Error updating database '$ ( $db.name ) ': $_ "
536
533
}
537
534
}
538
535
}
539
536
}
540
537
} catch {
541
- Write-Error " Error querying databases on server '$ ( $server.name ) ': $_ "
538
+ Write-Output " Error querying databases on server '$ ( $server.name ) ': $_ "
542
539
}
543
540
544
541
# Update Elastic Pools with similar improved error handling
545
542
try {
546
- Write-Output " Scanning Elastic Pools on server '$ ( $server.name ) '..."
543
+ Write-Output " Scanning Elastic Pools on server '$ ( $server.name ) '..."
547
544
548
545
# First check if there are any elastic pools
549
546
$allPools = az sql elastic- pool list -- resource- group $server.resourceGroup -- server $server.name -- only- show-errors - o json 2> $null | ConvertFrom-Json - ErrorAction SilentlyContinue
550
547
551
548
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 ) '."
553
550
} 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 ) '."
555
552
556
553
# Build elastic pool query with better formatting
557
554
$elasticPoolQuery = " [?licenseType!=null && licenseType!='$ ( $LicenseType ) '"
@@ -563,16 +560,16 @@ foreach ($sub in $subscriptions) {
563
560
564
561
$elasticPoolQuery += " ].{name:name, licenseType:licenseType, location:location, resourceGroup:resourceGroup, id:id, ResourceType:type, State:state}"
565
562
566
- Write-Output " Elastic Pool query: $elasticPoolQuery "
563
+ Write-Output " Elastic Pool query: $elasticPoolQuery "
567
564
568
565
$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
569
566
570
567
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."
572
569
} 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:"
574
571
$elasticPools | ForEach-Object {
575
- Write-Output " - $ ( $_.name ) (Current license: $ ( $_.licenseType ) )"
572
+ Write-Output " - $ ( $_.name ) (Current license: $ ( $_.licenseType ) )"
576
573
}
577
574
578
575
foreach ($pool in $elasticPools ) {
@@ -589,28 +586,28 @@ foreach ($sub in $subscriptions) {
589
586
}
590
587
591
588
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 '..."
593
590
try {
594
591
$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
595
592
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 '"
597
594
$finalStatus += $result
598
595
} 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."
600
597
}
601
598
} catch {
602
- Write-Error " Error updating elastic pool '$ ( $pool.name ) ': $_ "
599
+ Write-Output " Error updating elastic pool '$ ( $pool.name ) ': $_ "
603
600
}
604
601
}
605
602
}
606
603
}
607
604
}
608
605
} catch {
609
- Write-Error " Error processing Elastic Pools on server '$ ( $server.name ) ': $_ "
606
+ Write-Output " Error processing Elastic Pools on server '$ ( $server.name ) ': $_ "
610
607
}
611
608
}
612
609
} 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: $_ "
614
611
}
615
612
616
613
# --- Section: Update SQL Instance Pools ---
@@ -675,7 +672,7 @@ foreach ($sub in $subscriptions) {
675
672
Get-AzDataFactoryV2 |
676
673
Where-Object {
677
674
$_.ProvisioningState -eq " Succeeded" -and
678
- ($null -eq $ ResourceGroup -or $_.ResourceGroupName -eq $ResourceGroup )
675
+ ([ string ]::IsNullOrEmpty( $ ResourceGroup) -or $_.ResourceGroupName -eq $ResourceGroup )
679
676
} |
680
677
ForEach-Object {
681
678
$df = $_
@@ -684,7 +681,7 @@ foreach ($sub in $subscriptions) {
684
681
$_.Type -eq " Managed" -and
685
682
$_.State -ne " Starting" -and
686
683
$_.LicenseType -ne $LicenseType -and
687
- ($null -eq $ ResourceName -or $_.Name -eq $ResourceName )
684
+ ([ string ]::IsNullOrEmpty( $ ResourceName) -or $_.Name -eq $ResourceName )
688
685
}
689
686
690
687
if ($IRs.Count -eq 0 ) {
0 commit comments