mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Merge pull request #1430 from anosov1960/PAYG-transition
Updates to payg transition script
This commit is contained in:
@@ -3,7 +3,7 @@ services: Azure Arc-enabled SQL Server
|
||||
platforms: Azure
|
||||
author: anosov1960
|
||||
ms.author: sashan
|
||||
ms.date: 05/01/2025
|
||||
ms.date: 10/13/2025
|
||||
---
|
||||
|
||||
# About this sample
|
||||
@@ -14,11 +14,13 @@ ms.date: 05/01/2025
|
||||
- **Authors:** Alexander (Sasha) Nosov
|
||||
- **Update history:**
|
||||
|
||||
05/01/2025 - added the consent parameter for teh CSP-managed subscriptions
|
||||
05/01/2025 - added the consent parameter for the CSP-managed subscriptions
|
||||
|
||||
05/09/2025 - added *-ReportOnly* and *-TenandId* parameters
|
||||
|
||||
05/13/2025 - added support for a .CSV file with the machine names
|
||||
05/13/2025 - added support for a .CSV file with the machine names
|
||||
|
||||
10/13/2025 - turn off Azure login V2
|
||||
|
||||
# Overview
|
||||
|
||||
|
||||
+2
-1
@@ -119,7 +119,8 @@ function Connect-Azure {
|
||||
}
|
||||
Write-Output "Environment detected: $envType"
|
||||
|
||||
# 2) Ensure Az.PowerShell context
|
||||
# 2) Ensure Az.PowerShell context. Use login V1
|
||||
Update-AzConfig -LoginExperienceV2 Off
|
||||
$currentCtx = Get-AzContext -ErrorAction SilentlyContinue
|
||||
if ($currentCtx -and $currentCtx.Account) {
|
||||
if ($TenantId) {
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
"TenantID","SubID","ResourceName","ResourceType","Status","OriginalLicenseType","ResourceGroup","Location"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2012EEArc","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2022EE","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2022rtm","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2025devstd","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","ajayjsqlmi","Microsoft.Sql/managedInstances","Ready","BasePrice","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","AdventureworksLT","Microsoft.Sql/servers/databases","Online","BasePrice","ajayj","westus2"
|
||||
|
+7
@@ -0,0 +1,7 @@
|
||||
"TenantID","SubID","ResourceName","ResourceType","Status","OriginalLicenseType","ResourceGroup","Location"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2012EEArc","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2022EE","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2022rtm","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2025devstd","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","ajayjsqlmi","Microsoft.Sql/managedInstances","Ready","BasePrice","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","AdventureworksLT","Microsoft.Sql/servers/databases","Online","BasePrice","ajayj","westus2"
|
||||
|
+7
@@ -0,0 +1,7 @@
|
||||
"TenantID","SubID","ResourceName","ResourceType","Status","OriginalLicenseType","ResourceGroup","Location"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2012EEArc","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2022EE","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2022rtm","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2025devstd","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","ajayjsqlmi","Microsoft.Sql/managedInstances","Ready","BasePrice","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","AdventureworksLT","Microsoft.Sql/servers/databases","Online","BasePrice","ajayj","westus2"
|
||||
|
+7
@@ -0,0 +1,7 @@
|
||||
"TenantID","SubID","ResourceName","ResourceType","Status","OriginalLicenseType","ResourceGroup","Location"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2012EEArc","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2022EE","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2022rtm","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2025devstd","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","ajayjsqlmi","Microsoft.Sql/managedInstances","Ready","BasePrice","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","AdventureworksLT","Microsoft.Sql/servers/databases","Online","BasePrice","ajayj","westus2"
|
||||
|
+7
@@ -0,0 +1,7 @@
|
||||
"TenantID","SubID","ResourceName","ResourceType","Status","OriginalLicenseType","ResourceGroup","Location"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2012EEArc","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2022EE","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2022rtm","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2025devstd","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","ajayjsqlmi","Microsoft.Sql/managedInstances","Ready","BasePrice","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","AdventureworksLT","Microsoft.Sql/servers/databases","Online","BasePrice","ajayj","westus2"
|
||||
|
+7
@@ -0,0 +1,7 @@
|
||||
"TenantID","SubID","ResourceName","ResourceType","Status","OriginalLicenseType","ResourceGroup","Location"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2012EEArc","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","SQL2022EE","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2022rtm","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","sql2025devstd","Microsoft.SqlVirtualMachine/sqlVirtualMachines","VM running","AHUB","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","ajayjsqlmi","Microsoft.Sql/managedInstances","Ready","BasePrice","ajayj","westus2"
|
||||
"72f988bf-86f1-41af-91ab-2d7cd011db47","fa58cf66-caaf-4ba9-875d-f310d3694845","AdventureworksLT","Microsoft.Sql/servers/databases","Online","BasePrice","ajayj","westus2"
|
||||
|
@@ -3,7 +3,7 @@ services: Azure SQL
|
||||
platforms: Azure
|
||||
author: anosov1960,rodrigomonteiro-gbb
|
||||
ms.author: sashan.romontei
|
||||
ms.date: 06/21/2025
|
||||
ms.date: 10/13/2025
|
||||
---
|
||||
|
||||
# About this sample
|
||||
@@ -22,12 +22,14 @@ ms.date: 06/21/2025
|
||||
|
||||
06/10/2025 - Fixed a RG filter for SQL DB
|
||||
|
||||
10/13/2025 - blocked modification of the DR replica
|
||||
|
||||
|
||||
# Overview
|
||||
|
||||
This script provides a scaleable solution to change the license type of various Azure SQL resources within the selected scope. It automates the process of modifying license settings for SQL Databases, Elastic Pools, SQL Managed Instances, SQL Instance Pools, SQL Virtual Machines, and DataFactory SSIS Integration Runtimes. The script supports targeting a single subscription, a list of subscriptions defined in a CSV file, or all accessible subscriptions. Optionally, it can also start resources that are stopped (if the -ForceStartOnResources parameter is enabled).
|
||||
|
||||
This script is designed to help administrators standardize SQL licensing across their Azure environment by automating license updates. It accepts a subscription ID or CSV file (for a list of subscriptions). If no subscription is specified, it defaults to updating resources in all accessible subscriptions.
|
||||
This script is designed to help administrators standardize SQL licensing across their Azure environment by automating license updates. It accepts a subscription ID or CSV file (for a list of subscriptions). If no subscription is specified, it defaults to updating resources in all accessible subscriptions. The update will preserve the existing configuration of the passive replicas with failover rights (with License type value set to "DR").
|
||||
|
||||
# Target Resource Types
|
||||
|
||||
@@ -70,13 +72,14 @@ The scripts is seamlessly integrated with Azure Authentication. It uses managed
|
||||
|`-SubId`|`subscription_id` *or* a file_name|Optional: Subscription id or a .csv file with the list of subscriptions<sup>1</sup>. If not specified all subscriptions will be scanned|
|
||||
|`-ResourceGroup` |`resource_group_name`|Optional: Limits the scope to a specific resource group|
|
||||
|`-ResourceName` |`resource_name`|Optional: Limits the scope to resources associated with this name. For SQL Server - updates all databases under the specified server. For SQL Managed Instance - updates the specified instance. For SQL VM - updates the specified VM |
|
||||
|`-LicenseType` | `LicenseIncluded` (default) or `BasePrice` | Optional: Sets the license type to the specified value |
|
||||
|`-LicenseType` | `LicenseIncluded` (default) or `BasePrice` | Optional: Sets the license type to the specified value<sup>2</sup> |
|
||||
|`-ExclusionTags`| `'{"tag1":"value1","tag2":"value2"}'` |*Optional*. If specified, excludes the resources that have these tags assigned.|
|
||||
|`-TenantId`| `tenant_id` |*Optional*. If specified, uses this tenant id to log in. Otherwise, the current context is used.|
|
||||
|`-ReportOnly`| |*Optional*. If true, generates a csv file with the list of resources that are to be modified, but doesn't make the actual change.|
|
||||
|`-UseManagedIdentity`| |*Optional*. If true, logs in both PowerShell and CLI using managed identity. Required to run the script as a runbook.|
|
||||
|
||||
<sup>1</sup>You can generate a .csv file that lists only specific subscriptions. E.g., the following command will include only production subscriptions (exclude dev/test).
|
||||
<sup>2</sup>The script will skip the resources with the LicenseType property set to "DR"
|
||||
```PowerShell
|
||||
$tenantId = "<your-tenant-id>"
|
||||
Get-AzSubscription -TenantId $tenantId | Where-Object {
|
||||
|
||||
+10
-9
@@ -154,7 +154,9 @@ if (-not $TenantId) {
|
||||
} else {
|
||||
Write-Output "Using provided TenantId: $TenantId"
|
||||
}
|
||||
# Ensure connection with both PowerShell and CLI.
|
||||
|
||||
# Ensure connection with both PowerShell and CLI. Use V1 login.
|
||||
Update-AzConfig -LoginExperienceV2 Off
|
||||
if ($UseManagedIdentity) {
|
||||
Connect-Azure ($TenantId, $UseManagedIdentity)
|
||||
}else{
|
||||
@@ -264,7 +266,7 @@ foreach ($sub in $subscriptions) {
|
||||
Write-Output "Seeking SQL Virtual Machines that require a license update to $SqlVmLicenseType..."
|
||||
|
||||
# Build SQL VM query
|
||||
$sqlVmQuery = "[?sqlServerLicenseType!='${SqlVmLicenseType}' && sqlServerLicenseType!= 'DR'"
|
||||
$sqlVmQuery = "[?sqlServerLicenseType!='${SqlVmLicenseType}' && sqlServerLicenseType!='DR'"
|
||||
|
||||
# Add resource group filter if specified
|
||||
if ($rgFilter) {
|
||||
@@ -296,7 +298,7 @@ foreach ($sub in $subscriptions) {
|
||||
if($null -ne (az vm list --query "[?name=='$($sqlvm.name)' && resourceGroup=='$($sqlvm.resourceGroup)' $tagsFilter]"))
|
||||
{
|
||||
$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
|
||||
if ($vmStatus.PowerState -eq "VM running") {
|
||||
if (($vmStatus.PowerState -eq "VM running") -and ($sqlvm.sqlServerLicenseType -ne "DR")) {
|
||||
|
||||
# Collect data before modification
|
||||
$modifiedResources += [PSCustomObject]@{
|
||||
@@ -333,14 +335,13 @@ foreach ($sub in $subscriptions) {
|
||||
Write-Error "An error occurred while updating SQL VMs: $_"
|
||||
}
|
||||
|
||||
# --- Section: Update SQL Managed Instances (Stopped then Ready) "hybridSecondaryUsage": "Passive"---
|
||||
# --- Section: Update SQL Managed Instances (Stopped then Ready) "
|
||||
$sqlMIsToUpdate = [System.Collections.ArrayList]::new()
|
||||
try {
|
||||
|
||||
Write-Output "Processing SQL Managed Instances that are running to $LicenseType..."
|
||||
|
||||
|
||||
# Build Managed Instance query
|
||||
$miRunningQuery = "[?licenseType!='${LicenseType}' && hybridSecondaryUsage!='Passive' && state=='Ready'"
|
||||
$miRunningQuery = "[?licenseType!='${LicenseType}' && state=='Ready'"
|
||||
|
||||
# Add resource group filter if specified
|
||||
if ($rgFilter) {
|
||||
@@ -619,8 +620,8 @@ foreach ($sub in $subscriptions) {
|
||||
try {
|
||||
Write-Output "Searching for SQL Instance Pools that require a license update..."
|
||||
|
||||
# Build instance pool query
|
||||
$instancePoolsQuery = "[?licenseType!='${LicenseType}'"
|
||||
# Build instance pool query (skip the passive replicas)
|
||||
$instancePoolsQuery = "[?licenseType!='${LicenseType}' && state=='Ready'"
|
||||
|
||||
# Add resource group filter if specified
|
||||
if ($rgFilter) {
|
||||
|
||||
Reference in New Issue
Block a user