Skip to content

Commit d815c20

Browse files
authored
added resourc group option to Azure SQL modify-license-type
1 parent 8dc1b71 commit d815c20

1 file changed

Lines changed: 77 additions & 72 deletions

File tree

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

Lines changed: 77 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ function CheckModule ($m) {
3535
# If module is already imported - do nothing
3636

3737
if (!(Get-Module | Where-Object {$_.Name -eq $m})) {
38-
# If module is not imported, but available on disk then import
38+
# If module is not imported, but available on disk then import
3939
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
4040
Import-Module $m
4141
}
4242
else {
43-
4443
# If module is not imported, not available on disk, but is in online gallery then install and import
4544
if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
4645
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
4746
Import-Module $m
4847
}
4948
else {
50-
5149
# If module is not imported, not available and not in online gallery then abort
5250
write-host "Module $m not imported, not available and not in online gallery, exiting."
5351
EXIT 1
@@ -73,12 +71,14 @@ $requiredModules | Foreach-Object {CheckModule $_}
7371

7472
# Subscriptions to scan
7573

74+
$tenantID = (Get-AzureADTenantDetail).ObjectId
75+
7676
if ($SubId -like "*.csv") {
7777
$subscriptions = Import-Csv $SubId
78-
}elseif($SubId -ne $null){
79-
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
80-
}else{
81-
$subscriptions = Get-AzSubscription
78+
}elseif($SubId -ne "") {
79+
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription -TenantID $tenantID
80+
} else {
81+
$subscriptions = Get-AzSubscription -TenantID $tenantID
8282
}
8383

8484
# Record the start time
@@ -87,93 +87,98 @@ Write-Host ("Script execution started at: $startTime")
8787

8888
# Calculate usage for each subscription
8989

90-
foreach ($sub in $subscriptions){
90+
foreach ($sub in $subscriptions) {
9191

92-
if ($sub.State -ne "Enabled") {continue}
92+
if ($sub.State -ne "Enabled") { continue }
9393

9494
try {
9595
Set-AzContext -SubscriptionId $sub.Id
96-
}catch {
96+
} catch {
9797
write-host "Invalid subscription: " $sub.Id
9898
continue
9999
}
100100

101-
# Get all resource groups in the subscription
102-
$rgs = Get-AzResourceGroup
101+
# Get the resource groups
102+
if ($ResourceGroup) {
103+
$rgs = Get-AzResourceGroup -Name $ResourceGroup
104+
} else {
105+
$rgs = Get-AzResourceGroup
106+
}
103107

104-
# Get all logical servers
105-
$servers = Get-AzSqlServer
108+
foreach ($rg in $rgs) {
109+
# Get all logical servers
110+
$servers = Get-AzSqlServer -ResourceGroupName $rg.ResourceGroupName
106111

107-
# Scan all vCore-based SQL database resources in the subscription
108-
$servers | Get-AzSqlDatabase | Where-Object { $_.SkuName -ne "ElasticPool" -and $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
109-
if ($_.LicenseType -ne $LicenseType) {
110-
Set-AzSqlDatabase -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -DatabaseName $_.DatabaseName -LicenseType $LicenseType
111-
Write-Host ([Environment]::NewLine + "-- Database $_.DatabaseName is set to $LicenseType")
112+
# Scan all vCore-based SQL database resources in the subscription
113+
$servers | Get-AzSqlDatabase | Where-Object { $_.SkuName -ne "ElasticPool" -and $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
114+
if ($_.LicenseType -ne $LicenseType) {
115+
Set-AzSqlDatabase -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -DatabaseName $_.DatabaseName -LicenseType $LicenseType
116+
Write-Host ([Environment]::NewLine + "-- Database $_.DatabaseName is set to $LicenseType")
117+
}
112118
}
113-
}
114-
[system.gc]::Collect()
119+
[system.gc]::Collect()
115120

116-
# Scan all vCore-based SQL elastic pool resources in the subscription
117-
$servers | Get-AzSqlElasticPool | Where-Object { $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
118-
if ($_.LicenseType -ne $LicenseType) {
119-
Set-AzSqlElasticPool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -ElasticPoolName $_.ElasticPoolName -LicenseType $LicenseType
120-
Write-Host ([Environment]::NewLine + "-- ElasticPool $_.ElasticPoolName is set to $LicenseType")
121+
# Scan all vCore-based SQL elastic pool resources in the subscription
122+
$servers | Get-AzSqlElasticPool | Where-Object { $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
123+
if ($_.LicenseType -ne $LicenseType) {
124+
Set-AzSqlElasticPool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -ElasticPoolName $_.ElasticPoolName -LicenseType $LicenseType
125+
Write-Host ([Environment]::NewLine + "-- ElasticPool $_.ElasticPoolName is set to $LicenseType")
126+
}
127+
}
128+
[system.gc]::Collect()
129+
130+
# Scan all SQL managed instance resources in the subscription
131+
Get-AzSqlInstance | Where-Object { $_.InstancePoolName -eq $null } | ForEach-Object {
132+
if ($_.LicenseType -ne $LicenseType) {
133+
Set-AzSqlInstance -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
134+
Write-Host ([Environment]::NewLine + "-- Instance $_.InstanceName is set to $LicenseType")
135+
}
121136
}
122-
}
123-
[system.gc]::Collect()
124-
125-
# Scan all SQL managed instance resources in the subscription
126-
Get-AzSqlInstance | Where-Object { $_.InstancePoolName -eq $null } | ForEach-Object {
127-
if ($_.LicenseType -ne $LicenseType) {
128-
Set-AzSqlInstance -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
129-
Write-Host ([Environment]::NewLine + "-- Instance $_.InstanceName is set to $LicenseType")
130-
}
131-
}
132-
[system.gc]::Collect()
137+
[system.gc]::Collect()
133138

134-
# Scan all instance pool resources in the subscription
135-
Get-AzSqlInstancePool | Foreach-Object {
136-
if ($_.LicenseType -ne $LicenseType) {
137-
Set-AzSqlInstancePool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
138-
Write-Host ([Environment]::NewLine + "-- InstancePool $_.InstanceName is set to $LicenseType")
139+
# Scan all instance pool resources in the subscription
140+
Get-AzSqlInstancePool | Foreach-Object {
141+
if ($_.LicenseType -ne $LicenseType) {
142+
Set-AzSqlInstancePool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
143+
Write-Host ([Environment]::NewLine + "-- InstancePool $_.InstanceName is set to $LicenseType")
144+
}
139145
}
140-
}
141-
[system.gc]::Collect()
142-
143-
# Scan all SSIS integration runtime resources in the subscription
144-
$rgs | Get-AzDataFactoryV2 | Get-AzDataFactoryV2IntegrationRuntime | Where-Object { $_.State -eq "Started" -and $_.NodeSize -ne $null } | ForEach-Object {
145-
if ($_.LicenseType -ne $LicenseType) {
146-
# Set the license type to $LicenseType
147-
Set-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $_.ResourceGroupName -DataFactoryName $_.DataFactoryName -Name $_.Name -LicenseType $LicenseType
148-
Write-Host ([Environment]::NewLine + "-- DataFactory $_.DataFactoryName is set to $LicenseType")
146+
[system.gc]::Collect()
147+
148+
# Scan all SSIS integration runtime resources in the subscription
149+
Get-AzDataFactoryV2IntegrationRuntime | Where-Object { $_.State -eq "Started" -and $_.NodeSize -ne $null } | ForEach-Object {
150+
if ($_.LicenseType -ne $LicenseType) {
151+
# Set the license type to $LicenseType
152+
Set-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $_.ResourceGroupName -DataFactoryName $_.DataFactoryName -Name $_.Name -LicenseType $LicenseType
153+
Write-Host ([Environment]::NewLine + "-- DataFactory $_.DataFactoryName is set to $LicenseType")
154+
}
149155
}
150-
}
151-
[system.gc]::Collect()
152-
153-
# Scan all SQL VMs in the subscription
154-
$rgs | Get-AzVM | Where-Object { $_.StorageProfile.ImageReference.Offer -like "*sql*" -and $_.ProvisioningState -eq "Succeeded" } | ForEach-Object {
155-
$vmName = $_.Name
156-
$resourceGroupName = $_.ResourceGroupName
157-
158-
# Get the SQL configuration for the VM
159-
$sqlConfig = Get-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent"
156+
[system.gc]::Collect()
157+
158+
# Scan all SQL VMs in the subscription
159+
Get-AzVM | Where-Object { $_.StorageProfile.ImageReference.Offer -like "*sql*" -and $_.ProvisioningState -eq "Succeeded" } | ForEach-Object {
160+
$vmName = $_.Name
161+
$resourceGroupName = $_.ResourceGroupName
162+
163+
# Get the SQL configuration for the VM
164+
$sqlConfig = Get-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent"
165+
166+
if ($sqlConfig -ne $null) {
167+
$licenseType = $sqlConfig.Settings.LicenseType
168+
169+
if ($licenseType -ne $LicenseType) {
170+
# Set the license type to $LicenseType
171+
Set-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent" -Publisher "Microsoft.SqlServer.Management" -ExtensionType "SqlIaaSAgent" -TypeHandlerVersion "1.5" -Settings @{ "LicenseType" = $LicenseType }
172+
Write-Host ([Environment]::NewLine + "-- SQL VM $vmName is set to $LicenseType")
173+
}
160174

161-
if ($sqlConfig -ne $null) {
162-
$licenseType = $sqlConfig.Settings.LicenseType
163-
164-
if ($licenseType -ne $LicenseType) {
165-
# Set the license type to $LicenseType
166-
Set-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent" -Publisher "Microsoft.SqlServer.Management" -ExtensionType "SqlIaaSAgent" -TypeHandlerVersion "1.5" -Settings @{ "LicenseType" = $LicenseType }
167-
Write-Host ([Environment]::NewLine + "-- SQL VM $vmName is set to $LicenseType")
168175
}
169-
170176
}
171-
}
172-
[system.gc]::Collect()
177+
[system.gc]::Collect()
173178

179+
}
174180
}
175181

176182
# Record the end time
177183
$endTime = Get-Date
178184
Write-Host ("Script execution ended at: $endTime")
179-

0 commit comments

Comments
 (0)