How to Register an Azure SQL Server VM with the SQL IaaS Agent Extension Using PowerShell and CLI When we install SQL Server in Azure VM.
The SQL IaaS Agent Extension enables advanced management capabilities for SQL Server Virtual Machines (VMs) in Azure, such as automated backups, patching, and simplified license management. In this article, we’ll guide you through registering an existing SQL Server VM with the SQL IaaS Agent Extension using both PowerShell and Azure CLI.
What is the SQL IaaS Agent Extension?
The SQL IaaS Agent Extension is a lightweight agent that brings Azure SQL management features to SQL Server instances running on Azure VMs. Key benefits include:
- Automated Backup: Configure backups with defined retention periods.
- Automated Patching: Apply SQL Server updates with minimal intervention.
- License Optimization: Choose between Pay-as-you-Go (PAYG) or Azure Hybrid Use Benefit (AHUB).
- Monitoring and Reporting: Seamless integration with Azure Monitor for SQL.
Prerequisites
- Azure CLI or PowerShell:
- Install Azure CLI: Installation Guide
- Install Azure PowerShell Module:powershell
Install-Module -Name Az -AllowClobber -Force
- Permissions: Ensure you have Contributor access or higher to the resource group or subscription.
- Microsoft.SqlVirtualMachine Resource Provider:
- Register the provider if not already enabled:powershell
Register-AzResourceProvider -ProviderNamespace "Microsoft.SqlVirtualMachine"
- SQL Server VM: The VM must exist, be running, and have SQL Server installed.
Option 1: Register Using PowerShell
PowerShell Script
powershell# Variables
$ResourceGroupName = "MASDAR-UAEN-FP-RG" # Replace with your Resource Group name
$VMName = "AEADFPSQLDB01" # Replace with your VM name
$LicenseType = "AHUB" # Use "PAYG" for Pay-as-you-Go or "AHUB" for Azure Hybrid Use Benefit
# Register SQL VM with SQL IaaS Agent Extension
Register-AzSqlVM -ResourceGroupName $ResourceGroupName -VMName $VMName -LicenseType $LicenseType
Verifying Registration
Use the following PowerShell command to check the registration status:
powershellGet-AzSqlVM -ResourceGroupName $ResourceGroupName -VMName $VMName
Option 2: Register Using Azure CLI
If you prefer using Azure CLI, the command to register the SQL Server VM is straightforward:
CLI Command
bashaz sql vm create --name <VM Name> --resource-group <VM RG> --location "VM Deployed location" --license-type AHUB
Parameters Explained
--name: Name of the Azure VM.--resource-group: Resource Group where the VM resides.--location: Azure region where the VM is deployed. Use quotes if the region contains spaces.--license-type: Select betweenPAYGorAHUBfor license management.
Verifying Registration
Check the status of the registered VM using:
bashaz sql vm show --name <Vm Name> --resource-group <VM RG>
Benefits of Using PowerShell and CLI
- Consistency: Both methods allow repeatable and reliable registration processes.
- Flexibility: CLI is ideal for cross-platform environments, while PowerShell integrates seamlessly with Azure automation workflows.
- Scalability: Automate the registration of multiple VMs by iterating through a list in a script.
Troubleshooting Tips
- Resource Provider Issues:
- If you encounter errors, ensure the
Microsoft.SqlVirtualMachineresource provider is registered: - bash
az provider register --namespace "Microsoft.SqlVirtualMachine"
- If you encounter errors, ensure the
- Invalid License Type:
- Ensure the license type is either
PAYGorAHUB. Other values will cause the command to fail.
- Ensure the license type is either
- Provisioning State:
- Use the
Get-AzSqlVMoraz sql vm showcommand to confirm the Provisioning State isSucceeded.
- Use the
Conclusion
Registering SQL Server VMs with the SQL IaaS Agent Extension unlocks a range of advanced features that enhance manageability, cost optimization, and monitoring. Whether you choose PowerShell or Azure CLI, these tools provide robust automation options to streamline your Azure SQL environment.