Skip to content

Latest commit

 

History

History
135 lines (111 loc) · 5.58 KB

create-powershell-azure-resource-manager-template.md

File metadata and controls

135 lines (111 loc) · 5.58 KB
title titleSuffix description services ms.service ms.subservice ms.custom ms.devlang ms.topic author ms.author ms.reviewer ms.date
Create an instance (ARM Template & Powershell)
Azure SQL Managed Instance
Use this Azure PowerShell example script to create an Azure SQL Managed Instance.
sql-database
sql-database
managed-instance
seo-dt-2019, sqldbrb=1
PowerShell
sample
jovanpop-msft
jovanpop
sstein
03/12/2019

Use PowerShell with Azure Resource Manager template to create an Azure SQL Managed Instance

Azure SQL Managed Instance can be created using Azure PowerShell library and Azure Resource Manager templates.

[!INCLUDE quickstarts-free-trial-note] [!INCLUDE updated-for-az] [!INCLUDE cloud-shell-try-it.md]

If you choose to install and use PowerShell locally, this tutorial requires AZ PowerShell 1.4.0 or later. If you need to upgrade, see Install Azure PowerShell module. If you are running PowerShell locally, run Connect-AzAccount to create a connection to Azure.

Azure PowerShell commands can start deployment using a predefined Azure Resource Manager template. The following properties can be specified in the template:

  • SQL Managed Instance name
  • SQL administrator username and password.
  • Size of the instance (number of cores and max storage size).
  • VNet and subnet where the instance will be placed.
  • Server-level collation of the instance (Preview).

Instance name, SQL Administrator user name, VNet/subnet, and collation cannot be changed later. Other instance properties can be changed.

Prerequisites

This sample assumes that you have created a valid network environment or modified an existing VNet for your SQL Managed Instance. You can prepare the network environment using a separate Azure Resource Managed template, if necessary.

The sample uses the cmdlets New-AzResourceGroupDeployment and Get-AzVirtualNetwork so make sure that you have installed the following PowerShell modules:

Install-Module Az.Network
Install-Module Az.Resources

Azure Resource Manager template

Save the following script into a .json file, and note the file location:

{
    "$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.1",
    "parameters": {
        "instance": {
            "type": "string"
        },
        "user": {
            "type": "string"
        },
        "pwd": {
            "type": "securestring"
        },
        "subnetId": {
            "type": "string"
        }
    },
    "resources": [
        {
            "name": "[parameters('instance')]",
            "location": "West Central US",
            "tags": {
                "Description":"GP Instance with custom instance collation - Serbian_Cyrillic_100_CS_AS"
            },
            "sku": {
                "name": "GP_Gen5",
                "tier": "GeneralPurpose"
            },
            "properties": {
                "administratorLogin": "[parameters('user')]",
                "administratorLoginPassword": "[parameters('pwd')]",
                "subnetId": "[parameters('subnetId')]",
                "storageSizeInGB": 256,
                "vCores": 8,
                "licenseType": "LicenseIncluded",
                "hardwareFamily": "Gen5",
                "collation": "Serbian_Cyrillic_100_CS_AS"
            },
            "type": "Microsoft.Sql/managedInstances",
            "identity": {
                "type": "SystemAssigned"
            },
            "apiVersion": "2015-05-01-preview"
        }
    ]
}

Update the following PowerShell script with the correct file path for the .json file you saved previously, and change the names of the objects in the script:

$subscriptionId = "ed827499-xxxx-xxxx-xxxx-xxxxxxxxxx"
Select-AzSubscription -SubscriptionId $subscriptionId

# Managed Instance properties
$resourceGroup = "rg_mi"
$location = "West Central US"
$name = "managed-instance-name"
$user = "miSqlAdmin"
$secpasswd = ConvertTo-SecureString "<Put some strong password here>" -AsPlainText -Force

# Network configuration
$vNetName = "my_vnet"
$vNetResourceGroup = "rg_mi_vnet"
$subnetName = "ManagedInstances"
$vNet = Get-AzVirtualNetwork -Name $vNetName -ResourceGroupName $vNetResourceGroup
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNet
$subnetId = $subnet.Id

# Deploy Instance using Azure Resource Manager template:
New-AzResourceGroupDeployment  -Name MyDeployment -ResourceGroupName $resourceGroup  `
                                    -TemplateFile 'C:\...\create-managed-instance.json' `
                                    -instance $name -user $user -pwd $secpasswd -subnetId $subnetId

Once the script completes, the SQL Managed Instance can be accessed from all Azure services and the configured IP address.

Next steps

For more information on the Azure PowerShell, see Azure PowerShell documentation.

Additional SQL Managed Instance PowerShell script samples can be found in the Azure SQL Managed Instance PowerShell scripts.