Skip to content

Commit a00fde4

Browse files
Updates the license type for Azure SQL resources (SQL DBs, Elastic Pools, Managed Instances, Instance Pools, SQL VMs)
to a specified model ("LicenseIncluded" or "BasePrice"). Optionally starts resources if needed.
1 parent 864df36 commit a00fde4

2 files changed

Lines changed: 455 additions & 234 deletions

File tree

Lines changed: 71 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,98 @@
11
---
22
services: Azure SQL
33
platforms: Azure
4-
author: anosov1960
5-
ms.author: sashan
6-
ms.date: 03/25/2025
4+
author: anosov1960,rodrigomonteiro-gbb
5+
ms.author: sashan.romontei
6+
ms.date: 04/08/2025
77
---
88

9-
109
# Overview
1110

12-
This script provides a scaleable solution to set or change the license type on all Azure SQL Servers resources in a specified scope.
11+
This Azure Runbook updates the license type for various Azure SQL resources using Azure CLI and PowerShell commands. 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 -Force_Start_On_Resources parameter is enabled).
12+
13+
This script is designed to help administrators standardize SQL licensing across their Azure environment by automating license updates. In summary, the script:
1314

14-
By default, the script scans all subscriptions the user account has access or a .CSV file with a list of subscriptions. Alternatively, you can specify a single subscription or and a specific resource group. The sript enumerates the specific resources that have been affected by the change.
15+
Targets Multiple Resource Types:
16+
SQL Virtual Machines: Updates license types after checking if the VM is running. If not, it can optionally start the VM to perform the update.
17+
SQL Managed Instances: Detects instances that are stopped or not in the "Ready" state and can force them to start before updating.
18+
SQL Databases & Elastic Pools: Scans individual SQL servers to locate databases and elastic pools with a different license type and updates them accordingly.
19+
SQL Instance Pools: Locates instance pools that require an update.
20+
DataFactory SSIS Integration Runtimes: Checks for integration runtimes with an out-of-date license setting and updates them.
21+
Flexible Subscription Targeting:
22+
The script 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.
23+
Interactive Reporting:
24+
The script logs steps along the process and compiles a final report summarizing which resources were updated in each category.
25+
Seamless Integration with Azure Authentication:
26+
It uses managed identity authentication (via Connect-AzAccount -Identity and az login --identity) to connect to your Azure environment securely.
1527

16-
# Prerequisites
28+
# Required Permissions
29+
The automation account needs to have the bellow permissions in order to be able to successfully run the Runbook and update all the SQL Server resources license type:
1730

18-
- The following minimum RBAC premissions are required to set the licesne type on the individual Azure SQL resources:
1931
1. **Azure SQL Databases**: *SQL DB Contributor role*.
2032
1. **Azure SQL Elastic Pools**: *SQL DB Contributor*
2133
1. **Azure SQL Managed Instances**: *SQL Managed Instance Contributor*
2234
1. **Azure SQL Instance Pools**: *SQL Managed Instance Contributor*
2335
1. **Azure Data Factory SSIS Integration Runtimes**: *Data Factory Contributor role*.
2436
1. **SQL Servers in Azure Virtual Machines**: *Virtual Machine Contributor role*.
2537

26-
A *Subscriptin Contributor* role has sufficient permissions to mdify any of the above resources.
27-
28-
# Launching the script
29-
30-
The script accepts the following command line parameters:
31-
32-
| **Parameter**                                         | **Value**                                                                       | **Description** |
33-
|:--|:--|:--|
34-
|-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|
35-
|-ResourceGroup |resource_group_name|Optional: Limits the scope to a specific resource group|
36-
|-LicenseType | "LicenseIncluded" or "BasePrice"| Optional: Sets the license type to the specified value. If not specified, "LicenseIncluded" is set |
37-
38-
<sup>1</sup>You can create a .csv file using the following command and then edit to remove the subscriptions you don't want to scan.
39-
```PowerShell
40-
Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
41-
```
38+
A *Subscription Contributor* role has sufficient permissions to mdify any of the above resources.
4239

43-
## Example 1
40+
# Additional Information
4441

45-
The following command will scan all the subscriptions to which the user has access to, and set the license type to "LicenseIncluded" on all servers where license type is undefined.
42+
Script Parameters:
43+
- SubId: A single subscription ID or the filename of a CSV file containing multiple subscriptions.
44+
- ResourceGroup: (Optional) Limits the script’s operations to a specific resource group.
45+
- LicenseType: (Optional) Defines the target license type. Valid values are "LicenseIncluded" (default) or "BasePrice".
46+
- Force_Start_On_Resources: (Optional) When enabled, the script will attempt to start SQL VMs and SQL Managed Instances if they are not running before applying the update.
47+
Logging & Error Handling:
4648

47-
```PowerShell
48-
.\modify-license-type.ps1 -LicenseType LicenseIncluded
49-
```
49+
The script logs key actions to the console and captures error messages using Write-Error. Check the console output for a summary report detailing which resources were updated.
5050

51-
## Example 2
51+
# Customizations:
52+
You might want to customize the script’s logging or incorporate additional logging (e.g., writing to a file or Azure Log Analytics) to integrate seamlessly with your monitoring and reporting workflow.
5253

53-
The following command will scan the subscription `<sub_id>` and set the license type value to "LicenseIncluded" on all servers.
54+
# Creating an Azure Runbook
5455

55-
```PowerShell
56-
.\modify-license-type.ps1 -SubId <sub_id> -LicenseType LicenseIncluded```
56+
You can schedule the command to run as a runbook. Follow these steps using the Azure Portal:
5757

58-
## Example 3
58+
### 1. Download the Script
5959

60-
The following command will scan resource group `<resource_group_name>` in the subscription `<sub_id>`, set the license type value to "BasePrice".
60+
Open a command shell on your device and run the command below. This will copy the script to your local folder.
6161

62-
```PowerShell
63-
.\modify-license-type.ps1 -SubId <sub_id> -ResourceGroup <resource_group_name> -LicenseType BasePrice
62+
```console
63+
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/refs/heads/master/samples/manage/azure-hybrid-benefit/modify-license-type/modify-license-type.ps1
6464
```
65-
66-
# Running the script using Cloud Shell
67-
68-
This option is recommended because Cloud shell has the Azure PowerShell modules pre-installed and you are automatically authenticated. Use the following steps to run the script in Cloud Shell.
69-
70-
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
71-
72-
1. Connect to Azure AD. You must specify `<tenant_id>` if you have access to more than one AAD tenants.
73-
74-
```console
75-
Connect-AzureAD -TenantID <tenant_id>
76-
```
77-
78-
1. Upload the script to your cloud shell using the following command:
79-
80-
```console
81-
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-hybrid-benefit/modify-license-type/modify-license-type.ps1 -o modify-license-type.ps1
82-
```
83-
84-
1. Run the script.
85-
86-
> [!NOTE]
87-
> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS.
88-
> - The script will be uploaded directly to the home folder associated with your Cloud Shell session.
89-
90-
# Running the script from a PC
91-
92-
Use the following steps to run the script in a PowerShell session on your PC.
93-
94-
1. Copy the script to your current folder:
95-
96-
```console
97-
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-hybrid-benefit/modify-license-type/modify-license-type.ps1 -o modify-license-type.ps1
98-
```
99-
100-
1. Make sure the NuGet package provider is installed:
101-
102-
```console
103-
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
104-
Install-packageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Scope CurrentUser -Force
105-
```
106-
107-
1. Make sure the the Az module is installed. For more information, see [Install the Azure Az PowerShell module](https://learn.microsoft.com/powershell/azure/install-az-ps):
108-
109-
```console
110-
Install-Module Az -Scope CurrentUser -Repository PSGallery -Force
111-
```
112-
113-
1. Connect to Azure AD and log in to your Azure account. You must specify `<tenant_id>` if you have access to more than one AAD tenants.
114-
115-
```console
116-
Connect-AzureAD -TenantID <tenant_id>
117-
Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId
118-
```
119-
120-
1. Run the script.
65+
### 2. Create or Use an Existing Automation Account
66+
[Create a new automation account](https://ms.portal.azure.com/#create/Microsoft.AutomationAccount) or open an existing one.
67+
In the Advanced section, ensure that System assigned identity is selected.
68+
### 3. Import the Runbook
69+
Navigate to the Process Automation group and select Runbooks.
70+
Click on the Import a runbook tab and configure it:
71+
File: Select the file you downloaded in Step 1.
72+
Name: Enter a name for the runbook.
73+
Type: Set to PowerShell.
74+
Runtime Version: Choose 7.2.
75+
Click Import.
76+
### 4. Publish the Runbook
77+
After the runbook is imported, click the Publish button to make it available for scheduling.
78+
79+
### 5. Link the Runbook to a Schedule
80+
Once the runbook status is Published, click on the Link to schedule button.
81+
Select Link a schedule to your runbook and click + Add a schedule.
82+
Configure the schedule:
83+
Name: Provide a name for the schedule.
84+
Start Time: Set the desired start time.
85+
Recurrence: Choose the recurrence need it.
86+
Click Create.
87+
### 6. Configure Runbook Parameters
88+
Return to the Schedule runbook page.
89+
Click on Parameters and run settings.
90+
Paste the license ID value into the appropriate field.
91+
Click OK to link the schedule, then OK again to create the job.
92+
### 7. Verify the Runbook Execution
93+
On the runbook Overview page, open a recent job that was completed after the scheduled start time.
94+
Click on the Output tab and verify that you see:
95+
Properties.activationState=Activated
96+
Your license is now active.
97+
98+
For more information about the runbooks, see the [Runbook tutorial](https://docs.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-powershell)

0 commit comments

Comments
 (0)