Skip to content

Commit fe7c8d7

Browse files
author
Alexander (Sasha) Nosov
committed
Improved DR filtering
1 parent 05af924 commit fe7c8d7

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ foreach ($sub in $subscriptions) {
264264
Write-Output "Seeking SQL Virtual Machines that require a license update to $SqlVmLicenseType..."
265265

266266
# Build SQL VM query
267-
$sqlVmQuery = "[?sqlServerLicenseType!='${SqlVmLicenseType}' && sqlServerLicenseType!= 'DR'"
267+
$sqlVmQuery = "[?sqlServerLicenseType!='${SqlVmLicenseType}' && sqlServerLicenseType!='DR'"
268268

269269
# Add resource group filter if specified
270270
if ($rgFilter) {
@@ -296,7 +296,7 @@ foreach ($sub in $subscriptions) {
296296
if($null -ne (az vm list --query "[?name=='$($sqlvm.name)' && resourceGroup=='$($sqlvm.resourceGroup)' $tagsFilter]"))
297297
{
298298
$vmStatus = az vm get-instance-view --resource-group $sqlvm.resourceGroup --name $sqlvm.name --query "{Name:name, ResourceGroup:resourceGroup, PowerState:instanceView.statuses[?starts_with(code, 'PowerState/')].displayStatus | [0]}" -o json | ConvertFrom-Json
299-
if ($vmStatus.PowerState -eq "VM running") {
299+
if (($vmStatus.PowerState -eq "VM running") -and ($sqlvm.sqlServerLicenseType -ne "DR")) {
300300

301301
# Collect data before modification
302302
$modifiedResources += [PSCustomObject]@{
@@ -313,11 +313,9 @@ foreach ($sub in $subscriptions) {
313313

314314

315315
if (-not $ReportOnly) {
316-
if ($sqlvm.sqlServerLicenseType -ne "DR") { #should not modify a DR replica
317-
Write-Output "Updating SQL VM '$($sqlvm.name)' in RG '$($sqlvm.resourceGroup)' to license type '$SqlVmLicenseType'..."
318-
$result = az sql vm update -n $sqlvm.name -g $sqlvm.resourceGroup --license-type $SqlVmLicenseType -o json | ConvertFrom-Json
319-
$finalStatus += $result
320-
}
316+
Write-Output "Updating SQL VM '$($sqlvm.name)' in RG '$($sqlvm.resourceGroup)' to license type '$SqlVmLicenseType'..."
317+
$result = az sql vm update -n $sqlvm.name -g $sqlvm.resourceGroup --license-type $SqlVmLicenseType -o json | ConvertFrom-Json
318+
$finalStatus += $result
321319
}
322320
}
323321
}
@@ -342,7 +340,7 @@ foreach ($sub in $subscriptions) {
342340
Write-Output "Processing SQL Managed Instances that are running to $LicenseType..."
343341

344342
# Build Managed Instance query
345-
$miRunningQuery = "[?licenseType!='${LicenseType}' && hybridSecondaryUsage!='Passive' && state=='Ready'"
343+
$miRunningQuery = "[?licenseType!='${LicenseType}' && licenseType!='DR' && hybridSecondaryUsage!='Passive' && state=='Ready'"
346344

347345
# Add resource group filter if specified
348346
if ($rgFilter) {
@@ -383,11 +381,9 @@ foreach ($sub in $subscriptions) {
383381
}
384382

385383
if (-not $ReportOnly) {
386-
if ($mi.licenseType -ne "DR"){ #should not modify a DR replica
387-
Write-Output "Updating SQL Managed Instance '$($mi.name)' in RG '$($mi.resourceGroup)' to license type '$LicenseType'..."
388-
$result = az sql mi update --name $mi.name --resource-group $mi.resourceGroup --license-type $LicenseType -o json | ConvertFrom-Json
389-
$finalStatus += $result
390-
}
384+
Write-Output "Updating SQL Managed Instance '$($mi.name)' in RG '$($mi.resourceGroup)' to license type '$LicenseType'..."
385+
$result = az sql mi update --name $mi.name --resource-group $mi.resourceGroup --license-type $LicenseType -o json | ConvertFrom-Json
386+
$finalStatus += $result
391387
}
392388
}
393389
}
@@ -623,8 +619,8 @@ foreach ($sub in $subscriptions) {
623619
try {
624620
Write-Output "Searching for SQL Instance Pools that require a license update..."
625621

626-
# Build instance pool query
627-
$instancePoolsQuery = "[?licenseType!='${LicenseType}'"
622+
# Build instance pool query (skip the passive replicas)
623+
$instancePoolsQuery = "[?licenseType!='${LicenseType}' && licenseType!='DR' && hybridSecondaryUsage!='Passive' && state=='Ready'"
628624

629625
# Add resource group filter if specified
630626
if ($rgFilter) {
@@ -664,11 +660,9 @@ foreach ($sub in $subscriptions) {
664660
Location = $pool.location
665661
}
666662
if (-not $ReportOnly) {
667-
if ($pool.licenseType -ne "DR"){ #should not modify a DR replica
668-
Write-Output "Updating SQL Instance Pool '$($pool.name)' in RG '$($pool.resourceGroup)' to license type '$LicenseType'..."
669-
$result = az sql instance-pool update --name $pool.name --resource-group $pool.resourceGroup --license-type $LicenseType -o json | ConvertFrom-Json
670-
$finalStatus += $result
671-
}
663+
Write-Output "Updating SQL Instance Pool '$($pool.name)' in RG '$($pool.resourceGroup)' to license type '$LicenseType'..."
664+
$result = az sql instance-pool update --name $pool.name --resource-group $pool.resourceGroup --license-type $LicenseType -o json | ConvertFrom-Json
665+
$finalStatus += $result
672666
}
673667
}
674668
}

0 commit comments

Comments
 (0)