| title | Create SQL Server VM with PowerShell script |
|---|---|
| description | This article provides an end-to-end Azure PowerShell sample script to create SQL Server on Azure VMs. |
| author | bluefooted |
| ms.author | pamela |
| ms.reviewer | mathoma |
| ms.date | 05/29/2024 |
| ms.service | azure-vm-sql-server |
| ms.subservice | deployment |
| ms.topic | sample |
| ms.custom | devx-track-azurepowershell |
| ms.devlang | powershell |
[!INCLUDEappliesto-sqldb]
This PowerShell script example creates a Windows SQL Server virtual machine (VM) in Azure.
[!INCLUDE cloud-shell-try-it.md]
If you choose to install and use PowerShell locally, this tutorial requires Azure PowerShell 1.4.0 or later. If you need to upgrade, see Install Azure PowerShell module. If you're running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.
:::code language="powershell" source="~/../azure_powershell_scripts/azure-sql/virtual-machine/create-sql-server-vm.ps1" id="SetVariables":::
:::code language="powershell" source="~/../azure_powershell_scripts/azure-sql/virtual-machine/create-sql-server-vm.ps1" id="FullScript":::
Use the following command to remove the resource group and all resources associated with it.
:::code language="powershell" source="~/../azure_powershell_scripts/azure-sql/virtual-machine/create-sql-server-vm.ps1" id="Cleanup":::
The script in this article uses the following commands:
| Command | Notes |
|---|---|
| Get-AzVMImageOffer | Lists all Azure VM images. |
| Get-AzVMImageSku | Lists the SKUs for a particular offer. |
| New-AzResourceGroup | Creates a resource group in which all resources are stored. |
| New-AzStorageAccount | Creates a new Azure storage account. |
| New-AzVirtualNetworkSubnetConfig | Creates and configures a new subnet. |
| New-AzVirtualNetwork | Creates a virtual network. |
| New-AzPublicIpAddress | Creates a public IP address. |
| New-AzNetworkSecurityGroup | Creates a new security group. |
| New-AzNetworkInterface | Creates a network interface. |
| New-AzVMConfig | Creates a configurable virtual machine object. |
| Add-AzVMNetworkInterface | Adds a network interface to a virtual machine. |
| Register-AzResourceProvider | Registers a resource provider. |
| New-AzSqlVM | Creates or updates a SQL virtual machine resource. |
| Stop-AzVM | Stops an Azure virtual machine. |
| Remove-AzResourceGroup | Removes a resource group in Azure. |
For more information on Azure PowerShell, see Azure PowerShell documentation.