mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
added resourc group option to Azure SQL modify-license-type
This commit is contained in:
@@ -35,19 +35,17 @@ function CheckModule ($m) {
|
||||
# If module is already imported - do nothing
|
||||
|
||||
if (!(Get-Module | Where-Object {$_.Name -eq $m})) {
|
||||
# If module is not imported, but available on disk then import
|
||||
# If module is not imported, but available on disk then import
|
||||
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
|
||||
Import-Module $m
|
||||
}
|
||||
else {
|
||||
|
||||
# If module is not imported, not available on disk, but is in online gallery then install and import
|
||||
if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
|
||||
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
|
||||
Import-Module $m
|
||||
}
|
||||
else {
|
||||
|
||||
# If module is not imported, not available and not in online gallery then abort
|
||||
write-host "Module $m not imported, not available and not in online gallery, exiting."
|
||||
EXIT 1
|
||||
@@ -73,12 +71,14 @@ $requiredModules | Foreach-Object {CheckModule $_}
|
||||
|
||||
# Subscriptions to scan
|
||||
|
||||
$tenantID = (Get-AzureADTenantDetail).ObjectId
|
||||
|
||||
if ($SubId -like "*.csv") {
|
||||
$subscriptions = Import-Csv $SubId
|
||||
}elseif($SubId -ne $null){
|
||||
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
|
||||
}else{
|
||||
$subscriptions = Get-AzSubscription
|
||||
}elseif($SubId -ne "") {
|
||||
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription -TenantID $tenantID
|
||||
} else {
|
||||
$subscriptions = Get-AzSubscription -TenantID $tenantID
|
||||
}
|
||||
|
||||
# Record the start time
|
||||
@@ -87,93 +87,98 @@ Write-Host ("Script execution started at: $startTime")
|
||||
|
||||
# Calculate usage for each subscription
|
||||
|
||||
foreach ($sub in $subscriptions){
|
||||
foreach ($sub in $subscriptions) {
|
||||
|
||||
if ($sub.State -ne "Enabled") {continue}
|
||||
if ($sub.State -ne "Enabled") { continue }
|
||||
|
||||
try {
|
||||
Set-AzContext -SubscriptionId $sub.Id
|
||||
}catch {
|
||||
} catch {
|
||||
write-host "Invalid subscription: " $sub.Id
|
||||
continue
|
||||
}
|
||||
|
||||
# Get all resource groups in the subscription
|
||||
$rgs = Get-AzResourceGroup
|
||||
|
||||
# Get all logical servers
|
||||
$servers = Get-AzSqlServer
|
||||
|
||||
# Scan all vCore-based SQL database resources in the subscription
|
||||
$servers | Get-AzSqlDatabase | Where-Object { $_.SkuName -ne "ElasticPool" -and $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlDatabase -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -DatabaseName $_.DatabaseName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- Database $_.DatabaseName is set to $LicenseType")
|
||||
}
|
||||
# Get the resource groups
|
||||
if ($ResourceGroup) {
|
||||
$rgs = Get-AzResourceGroup -Name $ResourceGroup
|
||||
} else {
|
||||
$rgs = Get-AzResourceGroup
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all vCore-based SQL elastic pool resources in the subscription
|
||||
$servers | Get-AzSqlElasticPool | Where-Object { $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlElasticPool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -ElasticPoolName $_.ElasticPoolName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- ElasticPool $_.ElasticPoolName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
foreach ($rg in $rgs) {
|
||||
# Get all logical servers
|
||||
$servers = Get-AzSqlServer -ResourceGroupName $rg.ResourceGroupName
|
||||
|
||||
# Scan all SQL managed instance resources in the subscription
|
||||
Get-AzSqlInstance | Where-Object { $_.InstancePoolName -eq $null } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlInstance -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- Instance $_.InstanceName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all instance pool resources in the subscription
|
||||
Get-AzSqlInstancePool | Foreach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlInstancePool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- InstancePool $_.InstanceName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all SSIS integration runtime resources in the subscription
|
||||
$rgs | Get-AzDataFactoryV2 | Get-AzDataFactoryV2IntegrationRuntime | Where-Object { $_.State -eq "Started" -and $_.NodeSize -ne $null } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
# Set the license type to $LicenseType
|
||||
Set-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $_.ResourceGroupName -DataFactoryName $_.DataFactoryName -Name $_.Name -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- DataFactory $_.DataFactoryName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all SQL VMs in the subscription
|
||||
$rgs | Get-AzVM | Where-Object { $_.StorageProfile.ImageReference.Offer -like "*sql*" -and $_.ProvisioningState -eq "Succeeded" } | ForEach-Object {
|
||||
$vmName = $_.Name
|
||||
$resourceGroupName = $_.ResourceGroupName
|
||||
|
||||
# Get the SQL configuration for the VM
|
||||
$sqlConfig = Get-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent"
|
||||
|
||||
if ($sqlConfig -ne $null) {
|
||||
$licenseType = $sqlConfig.Settings.LicenseType
|
||||
|
||||
if ($licenseType -ne $LicenseType) {
|
||||
# Set the license type to $LicenseType
|
||||
Set-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent" -Publisher "Microsoft.SqlServer.Management" -ExtensionType "SqlIaaSAgent" -TypeHandlerVersion "1.5" -Settings @{ "LicenseType" = $LicenseType }
|
||||
Write-Host ([Environment]::NewLine + "-- SQL VM $vmName is set to $LicenseType")
|
||||
# Scan all vCore-based SQL database resources in the subscription
|
||||
$servers | Get-AzSqlDatabase | Where-Object { $_.SkuName -ne "ElasticPool" -and $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlDatabase -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -DatabaseName $_.DatabaseName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- Database $_.DatabaseName is set to $LicenseType")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all vCore-based SQL elastic pool resources in the subscription
|
||||
$servers | Get-AzSqlElasticPool | Where-Object { $_.Edition -in @("GeneralPurpose", "BusinessCritical", "Hyperscale") } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlElasticPool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -ElasticPoolName $_.ElasticPoolName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- ElasticPool $_.ElasticPoolName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all SQL managed instance resources in the subscription
|
||||
Get-AzSqlInstance | Where-Object { $_.InstancePoolName -eq $null } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlInstance -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- Instance $_.InstanceName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all instance pool resources in the subscription
|
||||
Get-AzSqlInstancePool | Foreach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
Set-AzSqlInstancePool -ResourceGroupName $_.ResourceGroupName -ServerName $_.ServerName -InstanceName $_.InstanceName -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- InstancePool $_.InstanceName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all SSIS integration runtime resources in the subscription
|
||||
Get-AzDataFactoryV2IntegrationRuntime | Where-Object { $_.State -eq "Started" -and $_.NodeSize -ne $null } | ForEach-Object {
|
||||
if ($_.LicenseType -ne $LicenseType) {
|
||||
# Set the license type to $LicenseType
|
||||
Set-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $_.ResourceGroupName -DataFactoryName $_.DataFactoryName -Name $_.Name -LicenseType $LicenseType
|
||||
Write-Host ([Environment]::NewLine + "-- DataFactory $_.DataFactoryName is set to $LicenseType")
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
# Scan all SQL VMs in the subscription
|
||||
Get-AzVM | Where-Object { $_.StorageProfile.ImageReference.Offer -like "*sql*" -and $_.ProvisioningState -eq "Succeeded" } | ForEach-Object {
|
||||
$vmName = $_.Name
|
||||
$resourceGroupName = $_.ResourceGroupName
|
||||
|
||||
# Get the SQL configuration for the VM
|
||||
$sqlConfig = Get-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent"
|
||||
|
||||
if ($sqlConfig -ne $null) {
|
||||
$licenseType = $sqlConfig.Settings.LicenseType
|
||||
|
||||
if ($licenseType -ne $LicenseType) {
|
||||
# Set the license type to $LicenseType
|
||||
Set-AzVMExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name "SqlIaaSAgent" -Publisher "Microsoft.SqlServer.Management" -ExtensionType "SqlIaaSAgent" -TypeHandlerVersion "1.5" -Settings @{ "LicenseType" = $LicenseType }
|
||||
Write-Host ([Environment]::NewLine + "-- SQL VM $vmName is set to $LicenseType")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
[system.gc]::Collect()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
# Record the end time
|
||||
$endTime = Get-Date
|
||||
Write-Host ("Script execution ended at: $endTime")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user