Azure SQL Hybrid Cloud Toolkit Notebooks Extension Command (#13286)

* added extension folder incomplete

* WIP extension progress

* notebook finally opens in side panel

* notebook now opens via notebook extension

* html file spaces restored

* package json fixed

* fixed vscode import issue

* more cleanup

* remove git stuff

* placeholder icon logos added

* fixed gulpfile

* cleanup changes

* vscode import fixed

* fixed main and yarn.lock

* added provided notebooks view

* formatting for package.json

* removed first command as its not necessary

* fixed notebook typo

* readded spaces
This commit is contained in:
Alex Ma
2020-11-11 13:50:36 -08:00
committed by GitHub
parent b32e5f8f25
commit fa608f9f80
87 changed files with 8661 additions and 0 deletions

View File

@@ -0,0 +1,239 @@
{
"metadata": {
"kernelspec": {
"name": "powershell",
"display_name": "PowerShell"
},
"language_info": {
"name": "powershell",
"codemirror_mode": "shell",
"mimetype": "text/x-sh",
"file_extension": ".ps1"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": [
"Create Azure SQL Database\r\n",
"==================================\r\n",
"\r\n",
"Description\r\n",
"-----------\r\n",
"This Azure CLI script example creates database in Azure SQL Database and configures a server-level firewall rule. After the script has been successfully run, the database can be accessed from all Azure services and the configured IP address\r\n",
""
],
"metadata": {
"azdata_cell_guid": "c9e3ccf4-199a-486d-9e19-7f448bd03f00"
}
},
{
"cell_type": "markdown",
"source": [
"Steps of this procedure include:\r\n",
"1. Connect to Azure subscription\r\n",
"1. Provision resource group for SQL Managed Instance\r\n",
"2. Create Sql Server\r\n",
"3. Provision firewall rules to allow access\r\n",
"4. Create Sql Database"
],
"metadata": {
"azdata_cell_guid": "ac87ba4f-d818-4fb3-8041-ee29ffd17294"
}
},
{
"cell_type": "markdown",
"source": [
"#### Sample values for below code block\r\n",
"| Variables | Sample Values |\r\n",
"| ------------ | --------- |\r\n",
"| $Env:BOOTSTRAP_Subscription | Subscription Name or ID |\r\n",
"| $Env:BOOTSTRAP_ResourceGroup | Intended Resource Group Name |\r\n",
"| $location | Valid location from Azure... See appendix at bottom |\r\n",
"| $randomIdentifier | Simple text... For example \"random123\"|\r\n",
"| $server | Name of intended Sql Server in simple text... For example \"server\" |\r\n",
"| $database | Name of intended database in simple text... For example \"database\" |\r\n",
"| $login | Sql Database Login Name... For example \"sampleLogin\" |\r\n",
"| \"password\" | This could be alphanumeric charecters of choice | \r\n",
"| $startIP | Intended Start Ip...For example 165.197.220.224|\r\n",
"| $endIP | Intended End Ip...For example 165.197.220.225 |\r\n",
""
],
"metadata": {
"azdata_cell_guid": "a199a24e-220c-4a8b-a090-37e38e725ab3"
}
},
{
"cell_type": "code",
"source": [
"$Env:BOOTSTRAP_Subscription = \"\" # Azure Subscription ID/Name for the ADP Resource Group # Both RG are assumed to be in the same subscription\r\n",
"$Env:BOOTSTRAP_ResourceGroup = \"\" # Azure Resource Group which contains the ADP Resources\r\n",
"\r\n",
"# SQL Server \r\n",
"$location =\"\"\r\n",
"$randomIdentifier =\"\"\r\n",
"\r\n",
"$server=\"server-$randomIdentifier\"\r\n",
"$database=\"database-$randomIdentifier\"\r\n",
"\r\n",
"$login=\"\"\r\n",
"$password=\"\"\r\n",
"\r\n",
"$startIP=\"\"\r\n",
"$endIP=\"\""
],
"metadata": {
"azdata_cell_guid": "0b2af740-99df-4f44-8cb7-fd00a78f8b9a",
"tags": []
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Connect to Azure and Select Subscription\r\n",
"Run the below cell to login to an Azure account. <b>Be sure to check the Windows Taskbar for a subscription selection dialog box.</b>\r\n",
"\r\n",
"_Note: the dialog box window may appear behind the active Azure Data Studio window._"
],
"metadata": {
"azdata_cell_guid": "acd95a93-f820-48cc-8699-975964225658"
}
},
{
"cell_type": "code",
"source": [
"az login\r\n",
"az account set --subscription $Env:BOOTSTRAP_Subscription"
],
"metadata": {
"azdata_cell_guid": "2a11cdb2-9549-4dab-a2fe-f0fd37474b6d"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create Resource Group\r\n",
"The Data Portability Notebooks provision several resources to enable import and export of Azure SQL DB databases and Azure SQL Managed Instance databases. These resources include Storage, Azure Batch, Azure Function resources and their dependent Azure resources. All resources created by this notebook are associated with a common Azure Resource Group to enable easy management of resources related to Data Portability.\r\n",
"\r\n",
"_Note: this notebook will reuse the Resource Group specified if it already exists._"
],
"metadata": {
"azdata_cell_guid": "a3a0c72c-04cb-4bb2-8dbc-4d96e7482ce9"
}
},
{
"cell_type": "code",
"source": [
"echo \"Creating $resource...\"\r\n",
"az group create --name $Env:BOOTSTRAP_ResourceGroup --location \"$location\""
],
"metadata": {
"azdata_cell_guid": "a9272913-cb61-4536-b89a-7826baa7465d"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create Sql Server\r\n",
"To create an Azure SQL Database using the Azure CLI 2.0,create an Azure SQL Server first, which can be done by running the following command"
],
"metadata": {
"azdata_cell_guid": "70e04ce4-c5ff-4cde-9a21-05fac7275dbf"
}
},
{
"cell_type": "code",
"source": [
"echo \"Creating $server in $location...\"\r\n",
"az sql server create --name $server --resource-group $Env:BOOTSTRAP_ResourceGroup --location \"$location\" --admin-user $login --admin-password $password"
],
"metadata": {
"azdata_cell_guid": "dbd81221-d61e-4441-ad6d-bb4dcef91f0c"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Configure Firewall Rule\r\n",
"\r\n",
""
],
"metadata": {
"azdata_cell_guid": "22bbc194-c946-493d-ab65-2750246596c3"
}
},
{
"cell_type": "code",
"source": [
"echo \"Configuring firewall...\"\r\n",
"az sql server firewall-rule create --resource-group $Env:BOOTSTRAP_ResourceGroup --server $server -n AllowYourIp --start-ip-address $startIP --end-ip-address $endIP"
],
"metadata": {
"azdata_cell_guid": "7dbbedfd-32cc-467c-b65a-aaf9ece946b7"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create Database on server\r\n",
""
],
"metadata": {
"azdata_cell_guid": "b002d01a-6b2d-4b45-bd77-0d139fbe5503"
}
},
{
"cell_type": "code",
"source": [
"echo \"Creating $database on $server...\"\r\n",
"az sql db create --resource-group $Env:BOOTSTRAP_ResourceGroup --server $server --name $database --sample-name AdventureWorksLT --edition GeneralPurpose --family Gen5 --capacity 2 --zone-redundant false # zone redundancy is only supported on premium and business critical service tiers"
],
"metadata": {
"azdata_cell_guid": "dd8d1b41-8ee7-4823-9655-a9d871b9fe97"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Appendices\r\n",
"These sections are not vital for the execution of this notebook but provide valuable background information.\r\n",
"\r\n",
"### Appendix: Locations\r\n",
"See the <a href=\"https://azure.microsoft.com/en-us/global-infrastructure/locations/\">Azure locations</a> page for a complete list of Azure regions along with their general physical location. The following is a list of common North American location settings for this guide:\r\n",
"\r\n",
"#### US Regions\r\n",
"| Setting | Location |\r\n",
"| ------------ | --------- |\r\n",
"| Central US | Iowa |\r\n",
"| East US | Virginia |\r\n",
"| East US 2 | Virginia |\r\n",
"| North Central US | Illinois |\r\n",
"| South Central US | Texas |\r\n",
"| West US 2 | Washington |\r\n",
"| West Central US | Wyoming |\r\n",
"| West US | California | \r\n",
"| Canada Central | Toronto |\r\n",
"| Canada East | Quebec City |\r\n",
"| Brazil South | Sao Paulo |\r\n",
"| Mexico Central | Queretaro |"
],
"metadata": {
"azdata_cell_guid": "52dd52ff-cabd-40cc-9b34-5a5ebd7ef0c3"
}
}
]
}

View File

@@ -0,0 +1,243 @@
{
"metadata": {
"kernelspec": {
"name": "powershell",
"display_name": "PowerShell"
},
"language_info": {
"name": "powershell",
"codemirror_mode": "shell",
"mimetype": "text/x-sh",
"file_extension": ".ps1"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": [
"Create Azure SQL Database\n",
"==================================\n",
"\n",
"Description\n",
"-----------\n",
"This notebook will help you get created single Azure SQL Database inside Azure SQL Server and configure a server-level firewall rule. For more information see <a href=https://docs.microsoft.com/en-us/azure/azure-sql/database/scripts/create-and-configure-database-powershell>Use PowerShell to create a single database and configure a server-level firewall rule</a>\n",
""
],
"metadata": {
"azdata_cell_guid": "6af59d69-ade7-480a-b33e-52a86fe5bfd3"
}
},
{
"cell_type": "markdown",
"source": [
"Steps of this procedure include:\r\n",
"1. Connect to Azure subscription\r\n",
"1. Provision resource group for SQL Managed Instance\r\n",
"2. Create Sql Server\r\n",
"3. Provision firewall rules to allow access\r\n",
"4. Create Sql Database"
],
"metadata": {
"azdata_cell_guid": "d5346c50-c03b-4e3a-983f-7b4b22c78319"
}
},
{
"cell_type": "code",
"source": [
"# Resource Group \r\n",
"$resourceGroupName = \"\" # Name of the resource group to create in the current subscription\r\n",
"$location = \"\" # Name of location (see Appendix for a list of location settings)\r\n",
"\r\n",
"# SQL Server\r\n",
"$serverName = \"mysqlserver-$(Get-Random)\"\r\n",
"$adminLogin = \"\" # Username of Sql server. Default would be 'azureuser'\r\n",
"$password = \"\" # Desired password for Sql Server\r\n",
"\r\n",
"# SQL Database\r\n",
"$databaseName = \"\" # Name of database to be created\r\n",
"\r\n",
"# The ip address range that you want to allow to access your server\r\n",
"$startIp = \"0.0.0.0\"\r\n",
"$endIp = \"0.0.0.0\""
],
"metadata": {
"azdata_cell_guid": "c5c06fd6-8e47-4abb-808a-edc8b1c2d690"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### <b>Connect to Azure </b>\r\n",
"Below command will open a _Dialouge Box_ asking your account credentials."
],
"metadata": {
"azdata_cell_guid": "e34334a7-0d55-4c18-8c0a-1c4a673629cd"
}
},
{
"cell_type": "code",
"source": [
"Connect-AzAccount"
],
"metadata": {
"azdata_cell_guid": "96800b54-48a8-463b-886c-3d0e96f29765"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### <b>Get Subscription</b>\r\n",
"Below command will open a _Dialouge Box_ with list of subscriptions. Selecting one of those will set that subscription for rest of the commands."
],
"metadata": {
"azdata_cell_guid": "ed6b781d-ce7e-4b51-a7ec-1eeeb2032c73"
}
},
{
"cell_type": "code",
"source": [
"$subscription = Get-AzSubscription | Out-GridView -PassThru\r\n",
"Set-AzContext -SubscriptionName $subscription"
],
"metadata": {
"azdata_cell_guid": "17b57956-98cf-44de-9ab5-348469ddabf4"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### <b>Create the New Resource Group</b>\r\n",
"_New-AzResourceGroup_ command will create new resource group in given subscription."
],
"metadata": {
"azdata_cell_guid": "3ecc2a29-fb77-4f7f-8901-e9c5c71ce1a2"
}
},
{
"cell_type": "code",
"source": [
"# Create Azure resource group, if necessary\r\n",
"$rg = Get-AzResourceGroup | Where ResourceGroupName -eq $resourceGroupName\r\n",
"\r\n",
"if (!$rg)\r\n",
"{\r\n",
" # Need to create a new resource group\r\n",
" Write-Output \"Resource Group $resourceGroupName does not exist. Creating...\"\r\n",
" $rg = New-AzResourceGroup -Name $resourceGroupName -Location $location\r\n",
"}"
],
"metadata": {
"azdata_cell_guid": "4837690a-2204-49ab-8a19-414a8ce782b6"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### <b>Create a SQL server with a system wide unique server name</b>\r\n",
"_New-AzSqlServer_ will create Sql Server with required configurations."
],
"metadata": {
"azdata_cell_guid": "2d951526-40dc-49cc-8668-c393eaf58000"
}
},
{
"cell_type": "code",
"source": [
"Write-Output \"Creating SqlServer with name $serverName ....\"\r\n",
"New-AzSqlServer -ResourceGroupName $resourceGroupName `\r\n",
" -ServerName $serverName `\r\n",
" -Location $location `\r\n",
" -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential `\r\n",
" -ArgumentList $adminLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))"
],
"metadata": {
"azdata_cell_guid": "c45757ac-6a58-468d-a04c-04504f8a2e0e"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### <b>Create a server firewall rule that allows access from the specified IP range</b>\r\n",
"_New-AzSqlServerFirewallRule_ creates a new firewall rule for sql server"
],
"metadata": {
"azdata_cell_guid": "ba895abf-3176-48b5-9e49-a060b3f74370"
}
},
{
"cell_type": "code",
"source": [
"Write-Output \"Configuring firewall for Sql Server...\"\r\n",
"New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `\r\n",
" -ServerName $ServerName `\r\n",
" -FirewallRuleName \"AllowedIPs\" -StartIpAddress $startIp -EndIpAddress $endIp"
],
"metadata": {
"azdata_cell_guid": "ceae5670-292f-4c45-9c10-4ac85baf2d07"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### <b>Create SQL Database</b>\r\n",
"_New-AzSqlDatabase_ command will create new database in the server."
],
"metadata": {
"azdata_cell_guid": "b460ca8f-65a7-4d6c-94b7-6d7dd9655fad"
}
},
{
"cell_type": "code",
"source": [
"Write-Output \"Creating a gen5 2 vCore database...\"\r\n",
"$database = New-AzSqlDatabase -ResourceGroupName $resourceGroupName `\r\n",
" -ServerName $ServerName `\r\n",
" -DatabaseName $databaseName `\r\n",
" -Edition GeneralPurpose `\r\n",
" -VCore 2 `\r\n",
" -ComputeGeneration Gen5 `\r\n",
" -MinimumCapacity 2"
],
"metadata": {
"azdata_cell_guid": "dc3b2f6f-83ac-4a4d-9d81-2f534e90913e"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### <b> Get Database details </b>\r\n",
"_Get-AzSqlDatabase_ command gets database details on the server specified."
],
"metadata": {
"azdata_cell_guid": "0b35ed4f-1786-4102-a09a-a6a360fd20f2"
}
},
{
"cell_type": "code",
"source": [
"Get-AzSqlDatabase -ResourceGroupName $resourceGroupName -ServerName $serverName -DatabaseName $databaseName"
],
"metadata": {
"azdata_cell_guid": "5001bf24-5f3f-434e-abf6-a5c21af4aa32"
},
"outputs": [],
"execution_count": null
}
]
}

View File

@@ -0,0 +1,339 @@
{
"metadata": {
"kernelspec": {
"name": "powershell",
"display_name": "PowerShell"
},
"language_info": {
"name": "powershell",
"codemirror_mode": "shell",
"mimetype": "text/x-sh",
"file_extension": ".ps1"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": [
"# Create Azure SQL Managed Instance\n",
"\n",
"## Description\n",
"\n",
"This PowerShell script example creates a managed instance in a dedicated subnet within a new virtual network. It also configures a route table and a network security group for the virtual network. Once the script has been successfully run, the managed instance can be accessed from within the virtual network or from an on-premises environment. See [Configure Azure VM to connect to Azure SQL Database Managed Instance](https://docs.microsoft.com/en-us/azure/azure-sql/managed-instance/connect-vm-instance-configure) and [Configure a point-to-site connection to Azure SQL Managed Instance from on-premises](https://docs.microsoft.com/en-us/azure/azure-sql/managed-instance/point-to-site-p2s-configure).\n",
"\n",
"<img width=\"50%\" src=https://docs.microsoft.com/en-us/azure/sql-database/media/sql-database-managed-instance/key-features.png />"
],
"metadata": {
"azdata_cell_guid": "2dcca704-2945-4763-8cbe-116b90696f9c"
}
},
{
"cell_type": "markdown",
"source": [
"Steps of this procedure include:\r\n",
"1. Connect to Azure subscription\r\n",
"1. Provision resource group for SQL Managed Instance\r\n",
"2. Configure Network Settings\r\n",
"3. Create credentials to be used for Managed Instance\r\n",
"4. Create Managed Instance"
],
"metadata": {
"azdata_cell_guid": "dd1cfc56-2919-416b-968d-cdf3345dcb1c"
}
},
{
"cell_type": "code",
"source": [
"$NSnetworkModels = \"Microsoft.Azure.Commands.Network.Models\"\r\n",
"$NScollections = \"System.Collections.Generic\"\r\n",
"\r\n",
"# Set the resource group name and Location for your managed instance\r\n",
"$ResourceGroupName = \"myResourceGroup-$(Get-Random)\"\r\n",
"$Location = \"eastus2\"\r\n",
"\r\n",
"# Set the networking values for your managed instance\r\n",
"$VNetName = \"myVnet-$(Get-Random)\"\r\n",
"$VNetAddressPrefix = \"10.0.0.0/16\"\r\n",
"$DefaultSubnetName = \"myDefaultSubnet-$(Get-Random)\"\r\n",
"$DefaultSubnetAddressPrefix = \"10.0.0.0/24\"\r\n",
"$MiSubnetName = \"myMISubnet-$(Get-Random)\"\r\n",
"$MiSubnetAddressPrefix = \"10.0.0.0/24\"\r\n",
"\r\n",
"#Set the managed instance name for the new managed instance\r\n",
"$InstanceName = \"myMIName-$(Get-Random)\"\r\n",
"# Set the admin login and password for your managed instance\r\n",
"$MiAdminSqlLogin = \"SqlAdmin\"\r\n",
"$MiAdminSqlPassword = \"ChangeYourAdminPassword1\"\r\n",
"\r\n",
"# Set the managed instance service tier, compute level, and License mode\r\n",
"$Edition = \"General Purpose\"\r\n",
"$VCores = 8\r\n",
"$MaxStorage = 256\r\n",
"$ComputeGeneration = \"Gen5\"\r\n",
"$License = \"LicenseIncluded\" #\"BasePrice\" or LicenseIncluded if you have don't have SQL Server licence that can be used for AHB discount\r\n",
""
],
"metadata": {
"azdata_cell_guid": "e574921c-3611-47d7-a872-564d07030815"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Connect to Azure Account"
],
"metadata": {
"azdata_cell_guid": "2ab3812a-cb4f-44a1-94d4-a0681a05606f"
}
},
{
"cell_type": "code",
"source": [
"Connect-AzAccount"
],
"metadata": {
"azdata_cell_guid": "b137afd5-512a-4261-90c0-b43dbf1724ff"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Get Subscription\r\n",
"Below command will open a Dialouge Box with list of subscriptions.\r\n",
"Selecting one of those will set that subscription for rest of the commands."
],
"metadata": {
"azdata_cell_guid": "d3af080f-c3d1-4a04-a168-78f8bfce247b"
}
},
{
"cell_type": "code",
"source": [
"$subscription = Get-AzSubscription | Out-GridView -PassThru\r\n",
"Set-AzContext -SubscriptionName $subscription"
],
"metadata": {
"azdata_cell_guid": "6356291d-96e4-4cbb-bae8-27e81f6e0bb8"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create the resource group\r\n",
"Create a resource group with the _New-AzResourceGroup_ command. An Azure resource group is a logical container into which Azure resources are deployed and managed. A resource group must be created first:"
],
"metadata": {
"azdata_cell_guid": "c8a4555b-8b80-41f9-9e8f-eaab3b419030"
}
},
{
"cell_type": "code",
"source": [
"# Create Azure resource group, if necessary\r\n",
"$resourceGroup = Get-AzResourceGroup -Name $ResourceGroupName\r\n",
"\r\n",
"if (!$resourceGroup)\r\n",
"{\r\n",
" # Need to create a new resource group\r\n",
" Write-Output \"Resource Group $RG1 does not exist. Creating...\"\r\n",
" $resourceGroup = New-AzResourceGroup -Name $RG1 -Location $Location -Tag @{Owner=\"SQLDB-Samples\"}\r\n",
"}"
],
"metadata": {
"azdata_cell_guid": "920c904f-6c6b-4e59-8305-f34b44297a64"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Configure virtual network, subnets, network security group, and routing table"
],
"metadata": {
"azdata_cell_guid": "2ed3ccec-ab94-4c20-aa74-6d67c3db014d"
}
},
{
"cell_type": "code",
"source": [
"$networkSecurityGroupMiManagementService = New-AzNetworkSecurityGroup `\r\n",
" -Name 'myNetworkSecurityGroupMiManagementService' `\r\n",
" -ResourceGroupName $ResourceGroupName `\r\n",
" -Location $Location\r\n",
"\r\n",
"$routeTableMiManagementService = New-AzRouteTable `\r\n",
" -Name 'myRouteTableMiManagementService' `\r\n",
" -ResourceGroupName $ResourceGroupName `\r\n",
" -Location $Location\r\n",
"\r\n",
"$virtualNetwork = New-AzVirtualNetwork `\r\n",
" -ResourceGroupName $ResourceGroupName `\r\n",
" -Location $Location `\r\n",
" -Name $VNetName `\r\n",
" -AddressPrefix $VNetAddressPrefix\r\n",
"\r\n",
" Add-AzVirtualNetworkSubnetConfig `\r\n",
" -Name $MiSubnetName `\r\n",
" -VirtualNetwork $virtualNetwork `\r\n",
" -AddressPrefix $MiSubnetAddressPrefix `\r\n",
" -NetworkSecurityGroup $networkSecurityGroupMiManagementService `\r\n",
" -RouteTable $routeTableMiManagementService |\r\n",
" Set-AzVirtualNetwork\r\n",
"\r\n",
"$virtualNetwork = Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName\r\n",
"\r\n",
"$subnet= $virtualNetwork.Subnets[0]"
],
"metadata": {
"azdata_cell_guid": "1f7cc6ec-ef04-4ffe-85d5-6ec40c5b2d1c"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create a delegation"
],
"metadata": {
"azdata_cell_guid": "a55da861-b130-451f-a017-4a033123e76d"
}
},
{
"cell_type": "code",
"source": [
"$subnet.Delegations = New-Object \"$NScollections.List``1[$NSnetworkModels.PSDelegation]\"\r\n",
"$delegationName = \"dgManagedInstance\" + (Get-Random -Maximum 1000)\r\n",
"$delegation = New-AzDelegation -Name $delegationName -ServiceName \"Microsoft.Sql/managedInstances\"\r\n",
"$subnet.Delegations.Add($delegation)\r\n",
"\r\n",
"Set-AzVirtualNetwork -VirtualNetwork $virtualNetwork\r\n",
"\r\n",
"$miSubnetConfigId = $subnet.Id\r\n",
"\r\n",
"\r\n",
"\r\n",
"$allowParameters = @{\r\n",
" Access = 'Allow'\r\n",
" Protocol = 'Tcp'\r\n",
" Direction= 'Inbound'\r\n",
" SourcePortRange = '*'\r\n",
" SourceAddressPrefix = 'VirtualNetwork'\r\n",
" DestinationAddressPrefix = '*'\r\n",
"}\r\n",
"$denyInParameters = @{\r\n",
" Access = 'Deny'\r\n",
" Protocol = '*'\r\n",
" Direction = 'Inbound'\r\n",
" SourcePortRange = '*'\r\n",
" SourceAddressPrefix = '*'\r\n",
" DestinationPortRange = '*'\r\n",
" DestinationAddressPrefix = '*'\r\n",
"}\r\n",
"$denyOutParameters = @{\r\n",
" Access = 'Deny'\r\n",
" Protocol = '*'\r\n",
" Direction = 'Outbound'\r\n",
" SourcePortRange = '*'\r\n",
" SourceAddressPrefix = '*'\r\n",
" DestinationPortRange = '*'\r\n",
" DestinationAddressPrefix = '*'\r\n",
"}\r\n",
"\r\n",
"Get-AzNetworkSecurityGroup `\r\n",
" -ResourceGroupName $ResourceGroupName `\r\n",
" -Name \"myNetworkSecurityGroupMiManagementService\" |\r\n",
" Add-AzNetworkSecurityRuleConfig `\r\n",
" @allowParameters `\r\n",
" -Priority 1000 `\r\n",
" -Name \"allow_tds_inbound\" `\r\n",
" -DestinationPortRange 1433 |\r\n",
" Add-AzNetworkSecurityRuleConfig `\r\n",
" @allowParameters `\r\n",
" -Priority 1100 `\r\n",
" -Name \"allow_redirect_inbound\" `\r\n",
" -DestinationPortRange 11000-11999 |\r\n",
" Add-AzNetworkSecurityRuleConfig `\r\n",
" @denyInParameters `\r\n",
" -Priority 4096 `\r\n",
" -Name \"deny_all_inbound\" |\r\n",
" Add-AzNetworkSecurityRuleConfig `\r\n",
" @denyOutParameters `\r\n",
" -Priority 4096 `\r\n",
" -Name \"deny_all_outbound\" |\r\n",
" Set-AzNetworkSecurityGroup"
],
"metadata": {
"azdata_cell_guid": "cb4292f6-1a78-43ec-81cb-505ebd791527"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create credentials"
],
"metadata": {
"azdata_cell_guid": "e83be748-e6ec-46e1-b056-18be20185195"
}
},
{
"cell_type": "code",
"source": [
"$secpassword = ConvertTo-SecureString $MiAdminSqlPassword -AsPlainText -Force\r\n",
"$credential = New-Object System.Management.Automation.PSCredential ($MiAdminSqlLogin, $secpassword)\r\n",
""
],
"metadata": {
"azdata_cell_guid": "63bca2ea-fbf8-4e2d-8f58-a2b7bd913f6e"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create managed instance"
],
"metadata": {
"azdata_cell_guid": "d17b701e-4c36-423c-b297-602879022621"
}
},
{
"cell_type": "markdown",
"source": [
"### This script will take a minimum of 3 hours to create a new managed instance in a new virtual network. \r\n",
"### A second managed instance is created much faster."
],
"metadata": {
"azdata_cell_guid": "3fdb03c6-3404-4ab6-9795-c2293cbea693"
}
},
{
"cell_type": "code",
"source": [
"New-AzSqlInstance -Name $InstanceName `\r\n",
" -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $miSubnetConfigId `\r\n",
" -AdministratorCredential $credential `\r\n",
" -StorageSizeInGB $MaxStorage -VCore $VCores -Edition $Edition `\r\n",
" -ComputeGeneration $ComputeGeneration -LicenseType $License\r\n",
""
],
"metadata": {
"azdata_cell_guid": "e3b55f89-d795-4d98-a24d-ade53cf53648"
},
"outputs": [],
"execution_count": null
}
]
}

View File

@@ -0,0 +1,557 @@
{
"metadata": {
"kernelspec": {
"name": "powershell",
"display_name": "PowerShell"
},
"language_info": {
"name": "powershell",
"codemirror_mode": "shell",
"mimetype": "text/x-sh",
"file_extension": ".ps1"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": [
"# Create Azure Sql Virtual Machine using Az-Cli"
],
"metadata": {
"azdata_cell_guid": "420658c9-5177-4220-9b7e-34429d2e526d"
}
},
{
"cell_type": "markdown",
"source": [
"## PS Version and Modules\r\n",
"This notebook uses a <a href=\"https://youtu.be/BhbiAINQBYE\">PowerShell Notebook kernel</a> (YouTube video) and requires PS v5.1 or greater. It also uses the latest version of Azure CLI. To install, see https://aka.ms/installazurecliwindows. Update instructions can be found at https://aka.ms/doc/UpdateAzureCliMsi"
],
"metadata": {
"azdata_cell_guid": "e0d6402d-4f1c-4e37-923e-1e9483431587"
}
},
{
"cell_type": "code",
"source": [
"# Check that PowerShell >= v5.1 and Azure Command Line Interface (Az CLI) are installed\r\n",
"if ($PSVersionTable.PSVersion.Major -lt 5)\r\n",
"{\r\n",
" Write-Warning \"Please install latest version of PowerShell. Do not continue executing the rest of the notebook.\"\r\n",
"}\r\n",
"else {\r\n",
" $psVersion = $PSVersionTable.PSVersion.ToString()\r\n",
" Write-Output \"Found PS version... $psVersion\"\r\n",
" # Check for Azure CLI\r\n",
" $azCli = (&az --version) \r\n",
" if ($azCli) { \r\n",
" $azVersion = $azCli[0].Split()[-1]\r\n",
" Write-Output \"Found Az CLI version... $azVersion\"\r\n",
" }\r\n",
"}"
],
"metadata": {
"azdata_cell_guid": "5a6893cf-e29d-4855-8771-0101bb6e55cd",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Set Variables for the Notebook"
],
"metadata": {
"azdata_cell_guid": "8998da5f-e4aa-4b32-aadf-51077748c56d"
}
},
{
"cell_type": "code",
"source": [
"# ADP Resource \r\n",
"$Env:BOOTSTRAP_Subscription = \"\" # Azure Subscription ID/Name # The bacpac files and ADP Resources are assumed to be in the same subscription\r\n",
"$Env:BOOTSTRAP_ResourceGroup = \"TestResourceGroup\" # Azure Resource Group which contains the ADP Resources\r\n",
"\r\n",
"# SQL Server \r\n",
"$VMName = \"TestVM\" # Name of the Virtual Machine to be created\r\n",
"$ImageSku = \"Enterprise\" # Choose your Image sku (see appendix)\r\n",
"$Location = \"East US\" # see Appendix for a list of location settings\r\n",
"$VmUsername = \"\" # Username of Virtual Machine to be created\r\n",
"$VmPassword = \"\" # Password of Virtual Machine to be created\r\n",
"\r\n",
"# Set Variables for ADP Resources\r\n",
"$Env:BOOTSTRAP_FUNC = $Env:BOOTSTRAP_ResourceGroup + \"Control\" \r\n",
"$Env:BOOTSTRAP_BATCH = $Env:BOOTSTRAP_ResourceGroup.ToLower() + \"batch\"\r\n",
"$Env:BOOTSTRAP_VNET = $Env:BOOTSTRAP_ResourceGroup + \"Vnet\""
],
"metadata": {
"azdata_cell_guid": "a6f7e1bb-6517-4c43-a63b-d70a5e8627ba",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Notebook Functions\r\n",
"Defines logical functions for the rest of the notebook. Function blocks are combined in a single cell that can be collapsed for readability or expanded for further examination. Nothing is executed until called later in the notebook. As a result, this cell is a requirement for any of the other cells below it. "
],
"metadata": {
"azdata_cell_guid": "c0f151bc-d1a3-4d41-8c93-b67f7a068be0"
}
},
{
"cell_type": "code",
"source": [
"# Expand cell to view framework\r\n",
"\r\n",
"function Login-Azure\r\n",
"{ \r\n",
" # query azure locations to test for existing az login session exists with valid access tocken\r\n",
" $azureLocations = az account list-locations -o JSON 2>$null | ConvertFrom-Json\r\n",
" \r\n",
" if (!$azureLocations){ #If there are no az locations, there is no existing az login session\r\n",
" $subscriptions = az login -o JSON | ConvertFrom-Json # Login \r\n",
" }\r\n",
" else {\r\n",
" $subscriptions = az account list -o JSON | ConvertFrom-Json # getting subscriptions for the user to use in gridview\r\n",
" }\r\n",
"\r\n",
" if(![string]::IsNullOrWhiteSpace($Env:BOOTSTRAP_Subscription)) #If there is a subscription specified by user in the variables section\r\n",
" {\r\n",
" $specified_Subscription= az account show --subscription $Env:BOOTSTRAP_Subscription -o json |ConvertFrom-Json \r\n",
" if (!$specified_Subscription) #if specified subscription is not valid\r\n",
" { \r\n",
" $currentUser= az ad signed-in-user show --query \"{displayName:displayName,UPN:userPrincipalName}\" -o json|ConvertFrom-Json # get current logged in user infomration\r\n",
" Write-Host \"Refer below for the list of subscriptions for logged in account '$($currentUser.UPN)'`n\"\r\n",
" az account list --query \"[].{Name:name,SubscriptionID:id}\" -o table # list subscriptions under current logged in account\r\n",
" }\r\n",
" else { # if specified subscription is valid\r\n",
" Write-Output \"Using subscription... '$($specified_Subscription.name)' ... '$($specified_Subscription.id)'\" \r\n",
" }\r\n",
" }\r\n",
" else { # if no subscription is specified, users are given a gridview to select subscription from\r\n",
"\r\n",
" $selectedSubscription = $subscriptions | Select-Object -Property Name, Id | Out-GridView -PassThru\r\n",
" $SubscriptionId = $selectedSubscription.Id\r\n",
" $Subscription = $selectedSubscription.Name \r\n",
" $Env:BOOTSTRAP_Subscription = $subscription \r\n",
" Write-Output \"Using subscription... '$Env:BOOTSTRAP_Subscription' ... '$SubscriptionId'\" \r\n",
" } \r\n",
"}\r\n",
"\r\n",
"function Verify-ADPResources\r\n",
"{ \r\n",
" [CmdletBinding()]\r\n",
" param(\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Subscription,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$ADPResourceGroupName,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$FunctionName, \r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$BatchAccountName,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$VNetName,\r\n",
" [Parameter (Mandatory=$false)] [ValidateNotNullOrEmpty()] [string]$ApplicationName=\"SqlPackageWrapper\", \r\n",
" [Parameter (Mandatory=$false)] [ValidateNotNullOrEmpty()] [string]$ApplicationPackageVersionName=\"1\",\r\n",
" [Parameter (Mandatory=$false)] [ValidateNotNullOrEmpty()] [string]$SubNetName=\"default\" \r\n",
" ) \r\n",
"\r\n",
"# validate Subscription\r\n",
"$specified_Subscription= az account show --subscription $Subscription -o json | ConvertFrom-Json\r\n",
"if(!$specified_Subscription){\r\n",
" $currentUser= az ad signed-in-user show --query \"{displayName:displayName,UPN:userPrincipalName}\" -o json|ConvertFrom-Json # get current logged in user information\r\n",
" Write-Host \"Refer below for the list of subscriptions for logged in account '$($currentUser.UPN)'`n\"\r\n",
" az account list --query \"[].{Name:name,SubscriptionID:id}\" -o table # list subscriptions under current logged in account\r\n",
" return } \r\n",
"# validate ResourceGroup \r\n",
"$specified_ResourceGroup= az group show -n $ADPResourceGroupName --subscription $Subscription -o json | ConvertFrom-Json\r\n",
"if(!$specified_ResourceGroup) { \r\n",
" return\r\n",
" } \r\n",
"\r\n",
"$Installed = [ordered]@{} # ordered hash to store status of installation\r\n",
"$countError=0\r\n",
"\r\n",
"#Verify if VNet exists \r\n",
"$specified_VNet= az network vnet show -n $VNetName -g $ADPResourceGroupName --subscription $Subscription -o JSON 2>$null |ConvertFrom-Json \r\n",
"if(!$specified_VNet) {\r\n",
" $Installed['VNET']=\"Not Found\"\r\n",
" $countError++\r\n",
"} \r\n",
"else { \r\n",
" $existingVnetSubnet = az network vnet subnet show -n $SubNetName --vnet-name $VNetName -g $ADPResourceGroupName --subscription $Subscription -o JSON 2>$null |ConvertFrom-Json\r\n",
" if(!$existingVnetSubnet){\r\n",
" $Installed['VNET']=\"Default Subnet under\"+ $VNetName + \"Not Found\"\r\n",
" $countError++\r\n",
" }\r\n",
" else {\r\n",
" $Installed['VNET']=\"Installed\"\r\n",
" }\r\n",
" }\r\n",
"\r\n",
"#Verify if FunctionApp Exists\r\n",
"$specified_FunctionApp = az functionapp show -n $FunctionName -g $ADPResourceGroupName --subscription $Subscription -o JSON 2>$null | ConvertFrom-Json\r\n",
"if(!$specified_FunctionApp)\r\n",
"{\r\n",
" $Installed['FunctionApp']=\"Not Installed\"\r\n",
" $countError++\r\n",
"}\r\n",
"else\r\n",
"{\r\n",
" $Installed['FunctionApp']=\"Installed\"\r\n",
"} \r\n",
"\r\n",
"#check if Batch account exists\r\n",
"$specified_BatchAccount = az batch account show -n $BatchAccountName -g $ADPResourceGroupName --subscription $Subscription -o JSON 2>$null | ConvertFrom-Json\r\n",
"if(!$specified_BatchAccount)\r\n",
"{\r\n",
" $Installed['Batch']=\"Not Installed\"\r\n",
" $countError++\r\n",
"}\r\n",
"else\r\n",
"{\r\n",
" $appPackageInstalled = az batch application package show --application-name $ApplicationName --version-name $ApplicationPackageVersionName -n $BatchAccountName -g $ADPResourceGroupName --subscription $Subscription -o JSON 2>$null | ConvertFrom-Json\r\n",
" $connectedToStorage= $specified_BatchAccount.autoStorage \r\n",
" if($connectedToStorage -and $appPackageInstalled){ # BatchAccount connected to storageaccount and applicationpackage is installed\r\n",
" $Installed['Batch']=\"Installed\"\r\n",
" $Installed['Batch_ApplicationPackage']=\"Installed\"\r\n",
" $Installed['Batch_StorageAccount']=\"Connected to storage- \"+$connectedToStorage.storageAccountId.Split(\"/\")[-1]\r\n",
" }\r\n",
" if(!$connectedToStorage)\r\n",
" {\r\n",
" $Installed['Batch_StorageAccount']='Not Found'\r\n",
" $countError++\r\n",
" } \r\n",
" if(!$appPackageInstalled)\r\n",
" {\r\n",
" $Installed['Batch_ApplicationPackage']=\"Not Found\"\r\n",
" $countError++\r\n",
" } \r\n",
"}\r\n",
"if ($countError -gt 0){\r\n",
" Write-Output \"ADP Resources are not installed correctly. Please refer the list below and use the Bootstrap NB to install ADP Resources\"\r\n",
"}\r\n",
"$Installed\r\n",
"if ($countError -eq 0){\r\n",
" Write-Output \"`nFound all ADP Resources.\"\r\n",
"}\r\n",
"}\r\n",
"\r\n",
"function Prepare-InputForImportFunction\r\n",
"{ \r\n",
" [CmdletBinding()]\r\n",
" param(\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Subscription,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$ADPResourceGroupName,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$FunctionName, \r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$BatchAccountName,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$BackupFiles_StorageAccount,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$BackupFiles_ContainerName,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$VNetName, \r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$TargetRGName,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$SqlServerName,\r\n",
" [Parameter (Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$SqlServerPassword\r\n",
" )\r\n",
" \r\n",
" $Result = @{}\r\n",
" # Build Header \r\n",
" ## get Function key\r\n",
" $FunctionAppID =az functionapp show -n $FunctionName -g $ADPResourceGroupName --subscription $Subscription --query \"[id]\" -o JSON 2>$null | ConvertFrom-Json\r\n",
" $DefaultHostKey = az rest --method post --uri \"$FunctionAppID/host/default/listKeys?api-version=2018-11-01\" --query \"[functionKeys.default]\" -o JSON 2>$null | ConvertFrom-Json\r\n",
" ## Build Json Object for Headers\r\n",
" $headers = @{\r\n",
" 'x-functions-key' = $DefaultHostKey\r\n",
" }\r\n",
" $Result['Header']=$headers\r\n",
"\r\n",
" # Build string for Function URL \r\n",
" $specified_Subscription= az account show --subscription $Subscription -o json |ConvertFrom-Json #Get SpecifiedSubscriptionID\r\n",
" $SubscriptionID= $specified_Subscription.id\r\n",
" $FunctionUrl = 'https://'+ $FunctionName +'.azurewebsites.net/api/subscriptions/'+ $SubscriptionID +'/resourceGroups/' + $ADPResourceGroupName + '/Import'\r\n",
" $Result['FunctionURL']=$FunctionUrl\r\n",
"\r\n",
" # Set parameter variables for Body\r\n",
" ## Get BatchAccountURL \r\n",
" $specified_Batch = az batch account show -n $BatchAccountName -g $ADPResourceGroupName --subscription $Subscription -o JSON 2>$null | ConvertFrom-Json\r\n",
" $BatchAccountURL = 'https://' + $specified_Batch.accountEndpoint\r\n",
"\r\n",
" ## Get default SubNet ID for specified VNet\r\n",
" $specified_VNet_SubNet = az network vnet subnet show -g $ADPResourceGroupName --vnet-name $VNetName -n 'default' --subscription $Subscription -o JSON |ConvertFrom-Json\r\n",
" $VNetSubNetID = $specified_VNet_SubNet.id\r\n",
"\r\n",
" ## Create access token to source sql server\r\n",
" $targetAccessToken = az account get-access-token --resource=https://database.windows.net --query accessToken\r\n",
" $targetAccessToken\r\n",
"\r\n",
" ## Build JSon object for Body\r\n",
" $Body = @{\r\n",
" batchAccountUrl = $BatchAccountURL\r\n",
" VNetSubnetId= $VNetSubNetID\r\n",
" storageAccountName = $BackupFiles_StorageAccount\r\n",
" containerName = $BackupFiles_ContainerName\r\n",
" targetSqlServerResourceGroupName = $TargetRGName\r\n",
" targetSqlServerName = $SQLServerName \r\n",
" userName = $SqlServerLogin \r\n",
" targetAccessToken = $targetAccessToken\r\n",
" sqlAdminPassword = $SqlServerPassword\r\n",
" }\r\n",
" $json = $Body | ConvertTo-Json\r\n",
" $Result['Body']=$json\r\n",
"\r\n",
" $Result\r\n",
" \r\n",
"}\r\n",
"\r\n",
"function Provision-FuncRBAC {\r\n",
" [CmdletBinding()]\r\n",
" param (\r\n",
" [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$Subscription,\r\n",
" [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$ResourceGroupName,\r\n",
" [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$FunctionName,\r\n",
" [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$ScopeRGName,\r\n",
" [Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()][string]$Role=\"Contributor\"\r\n",
" )\r\n",
"\r\n",
" # Get the scope resource group's ID\r\n",
" $scopeID = az group show --resource-group $ScopeRGName --subscription $Subscription --query \"[id]\" -o JSON | ConvertFrom-Json \r\n",
" if(!$scopeID) {\r\n",
" Write-Output \"Provision-FuncRBAC failed.\" \r\n",
" return }\r\n",
" else { Write-Output \"Found scope '$ScopeRGName' with ID... '$scopeID'\"\r\n",
" }\r\n",
"\r\n",
" # Get the az function principal id\r\n",
" $app_PrincipalID = az functionapp show -n $FunctionName --resource-group $ResourceGroupName --subscription $Subscription --query \"[identity.principalId]\" -o JSON | ConvertFrom-Json \r\n",
" if(!$app_PrincipalID) {\r\n",
" Write-Output \"Provision-FuncRBAC failed.\" \r\n",
" return }\r\n",
" else { Write-Output \"Found principal id of Azure function '$FunctionName'... '$app_PrincipalID'\"\r\n",
" }\r\n",
"\r\n",
" # Verify if a role assignment has been created for function\r\n",
" $app_RoleAssignmentDefinition= az role assignment list --subscription $Subscription --assignee $app_PrincipalID --scope $scopeID --query \"[].roleDefinitionName\" -o JSON 2>$null | ConvertFrom-Json\r\n",
"\r\n",
" if($app_RoleAssignmentDefinition -eq $Role)\r\n",
" {\r\n",
" Write-Output \"Found Role Assignment for Principal ID.. '$app_PrincipalID' with Role.. '$app_RoleAssignmentDefinition' . No work needed\"\r\n",
" }\r\n",
" else\r\n",
" {\r\n",
" # Continue to setup RBAC, once we verify an assignment is not setup and all the resources exist\r\n",
" Write-Output \"Creating new role assignment by running: 'az functionapp identity assign -n $FunctionName --role $Role -g $ResourceGroupName --scope $scopeID --subscription $Subscription'\"\r\n",
" Write-Warning \"If your account does not have the access to assign new roles as Owner or User Access Administrator for the resource group, than you will need to contact your Azure AD Administrator to assign a service principle using the commands above\"\r\n",
" az functionapp identity assign -n $FunctionName --role $Role -g $ResourceGroupName --scope $scopeID --subscription $Subscription \r\n",
" }\r\n",
"}\r\n",
"Write-Host \"Helper Functions Created successfully\" "
],
"metadata": {
"azdata_cell_guid": "ea9ddf9c-4c09-4db3-94ea-e01bc5f105c9",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Connect to Azure Account\r\n",
"Run the below cell to login to an Azure account. <b>Be sure to check the Windows Taskbar for a login dialog box underneath the notebook or other windows or by pressing Alt+TAB.</b>"
],
"metadata": {
"azdata_cell_guid": "2a5a155f-556d-451d-a0d4-b802017946df"
}
},
{
"cell_type": "code",
"source": [
"Login-Azure"
],
"metadata": {
"azdata_cell_guid": "77067078-7ec5-4d07-803b-5c52dfcf06f0",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Verify ADP Resources \r\n",
"Verify if ADP resources exists in specified Resource Group"
],
"metadata": {
"azdata_cell_guid": "717960de-4baf-4b78-bcec-3a8e386b42b7"
}
},
{
"cell_type": "code",
"source": [
"Verify-ADPResources -Subscription $Env:BOOTSTRAP_Subscription -ADPResourceGroupName $Env:BOOTSTRAP_ResourceGroup `\r\n",
" -BatchAccountName $Env:BOOTSTRAP_BATCH -FunctionName $Env:BOOTSTRAP_FUNC -VNetName $Env:BOOTSTRAP_VNET "
],
"metadata": {
"azdata_cell_guid": "22e8a9ed-d2e0-46eb-92b0-8c1108b60b67",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create a resource group\r\n",
"\r\n",
"In Azure, all resources are allocated in a resource management group. Resource groups provide logical groupings of resources that make them easier to work with as a collection"
],
"metadata": {
"azdata_cell_guid": "73e7c7f9-f518-4b0a-87d1-d43c6a10cbad"
}
},
{
"cell_type": "code",
"source": [
"$rsgExists = az group exists -n $Env:BOOTSTRAP_ResourceGroup\r\n",
"if ($rsgExists -eq 'false') {\r\n",
" az group create -l $Location -n $Env:BOOTSTRAP_ResourceGroup\r\n",
"}"
],
"metadata": {
"azdata_cell_guid": "7e95158c-9ddc-4b5e-b289-2086a2d03c15",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create a virtual machine\r\n",
"\r\n",
""
],
"metadata": {
"azdata_cell_guid": "222d6b8d-9789-4416-b512-63b1ce6f11e0"
}
},
{
"cell_type": "code",
"source": [
"az vm create -n sql -g $Env:BOOTSTRAP_ResourceGroup --image MicrosoftSQLServer:SQL2017-WS2016:Standard:14.0.1000200 --location $Location --admin-username $VmUsername --admin-password $VmPassword --verbose"
],
"metadata": {
"azdata_cell_guid": "37ab908d-839a-45e9-89cf-78ed2452debe",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Get VM information with queries\r\n",
"Now that a VM has been created, detailed information about it can be retrieved. The common command for getting information from a resource is _show_"
],
"metadata": {
"azdata_cell_guid": "9de4dd69-c9e3-4e10-89f1-cc785ea712b4"
}
},
{
"cell_type": "code",
"source": [
"az vm show --name TutorialVM1 --resource-group TutorialResources"
],
"metadata": {
"azdata_cell_guid": "51a4a092-1697-476c-934f-75a1a30dab7f",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"Lot of information can be seen, which can be difficult to parse visually. The returned JSON contains information on authentication, network interfaces, storage, and more. Most importantly, it contains the Azure object IDs for resources that the VM is connected to. Object IDs allow accessing these resources directly to get more information about the VM's configuration and capabilities.\r\n",
"\r\n",
"In order to extract the object ID , the --query argument is used. Queries are written in the JMESPath query language. Start with getting the network interface controller (NIC) object ID"
],
"metadata": {
"azdata_cell_guid": "902e834e-bbbf-40f4-80db-48d3b4b90f99"
}
},
{
"cell_type": "code",
"source": [
"az vm show --name TutorialVM1 \\\r\n",
" --resource-group TutorialResources \\\r\n",
" --query 'networkProfile.networkInterfaces[].id' \\\r\n",
" --output tsv"
],
"metadata": {
"azdata_cell_guid": "2bc8e0c7-d936-42ce-9e6a-e66207cd78f0",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### Appendix: Locations\r\n",
"See the <a href=\"https://azure.microsoft.com/en-us/global-infrastructure/locations/\">Azure locations</a> page for a complete list of Azure regions along with their general physical location. The following is a list of common North American location settings for this guide:\r\n",
"\r\n",
"#### US Regions\r\n",
"| Setting | Location |\r\n",
"| ------------ | --------- |\r\n",
"| Central US | Iowa |\r\n",
"| East US | Virginia |\r\n",
"| East US 2 | Virginia |\r\n",
"| North Central US | Illinois |\r\n",
"| South Central US | Texas |\r\n",
"| West US 2 | Washington |\r\n",
"| West Central US | Wyoming |\r\n",
"| West US | California | \r\n",
"| Canada Central | Toronto |\r\n",
"| Canada East | Quebec City |\r\n",
"| Brazil South | Sao Paulo |\r\n",
"| Mexico Central | Queretaro |\r\n",
"\r\n",
"### Appendix: Storage Skus\r\n",
"Use these as values for provisioning storage skus. \r\n",
"\r\n",
"Data for table taken from <a href=\"https://docs.microsoft.com/en-us/rest/api/storagerp/srp_sku_types\">SKU Types</a> page but is subject to change. Not all skus are listed here. SKU type names are case-sensitive.\r\n",
"\r\n",
"| Name |\r\n",
"| -----|\r\n",
"| Developer |\r\n",
"| Enterprise |\r\n",
"| Express |\r\n",
"| Standard |\r\n",
"| Web |"
],
"metadata": {
"azdata_cell_guid": "a40b46e4-4b92-4ada-b022-4342ce1b35f2"
}
}
]
}

View File

@@ -0,0 +1,383 @@
{
"metadata": {
"kernelspec": {
"name": "powershell",
"display_name": "PowerShell"
},
"language_info": {
"name": "powershell",
"codemirror_mode": "shell",
"mimetype": "text/x-sh",
"file_extension": ".ps1"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": [
"Create Azure SQL Virtual Machine\n",
"============================================\n",
"\n",
"Description\n",
"-----------\n",
"For more information about other Azure PowerShell options for creating SQL VMs, see the <a href=\"https://docs.microsoft.com/en-us/azure/virtual-machines/windows/sql/virtual-machines-windows-ps-sql-create\">Provisioning guide for SQL Server VMs with Azure PowerShell</a>. See also <a href=\"https://docs.microsoft.com/en-us/azure/virtual-machines/windows/sql/quickstart-sql-vm-create-powershell\">Quickstart guide</a> to creating a SQL Server VM with Azure PowerShell.\n",
"\n",
"\n",
"Steps of this procedure include:\n",
"1. Connect to Azure subscription\n",
"1. Provision resource group for SQL VM migration\n",
"2. Create a storage account\n",
"3. Configure Network Settings\n",
"3. Provision SQL VM\n",
"4. Configure SQL VM IaaS agent"
],
"metadata": {
"azdata_cell_guid": "e479b550-d6bd-49c5-965a-34a7d1d16412"
}
},
{
"cell_type": "markdown",
"source": [
"#### <b> *Enter all the values in quotes *</b>"
],
"metadata": {
"azdata_cell_guid": "37db2e50-dcde-4dd5-820c-7dc11212f1eb"
}
},
{
"cell_type": "code",
"source": [
"# Resource Group \r\n",
"$ResourceGroupName = \"\" # Name of the resource group to create in the current subscription\r\n",
"$Location = \"\" # see Appendix for a list of location settings\r\n",
"\r\n",
"# Compute\r\n",
"$VMName = \"\" # VM to create\r\n",
"$PublisherName = \"\" # Name of Publisher, Default would be 'MicrosoftSQLServer'\r\n",
"$Version = \"\" # Version of VM, Default would be 'latest'\r\n",
"\r\n",
"# Storage\r\n",
"$StorageAccountName = $ResourceGroupName + \"_storage\"\r\n",
"$StorageSku = \"\" # Choose your storage sku (see appendix)\r\n",
"$StorageName = \"sqlstorage\" + (Get-Random -Minimum 1 -Maximum 100)\r\n",
"\r\n",
"# VM Password\r\n",
"$secureVMPassword = \"\" # Create the password for VM."
],
"metadata": {
"azdata_cell_guid": "b9aff9cc-a3af-41cb-a2a5-35f36b2bcc55",
"tags": []
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Connect to Azure Account"
],
"metadata": {
"azdata_cell_guid": "b06fee5e-355d-47fc-8c1f-41294756cc87"
}
},
{
"cell_type": "code",
"source": [
"Connect-AzAccount"
],
"metadata": {
"azdata_cell_guid": "7dbdc638-d6cd-4ebe-9268-3efc18d3c415"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Get Subscription\r\n",
"Below command will open a _Dialouge Box_ with list of subscriptions. Selecting one of those will set that subscription for rest of the commands."
],
"metadata": {
"azdata_cell_guid": "b58f1048-3e9d-4888-bda0-4d0443a11c97"
}
},
{
"cell_type": "code",
"source": [
"$subscription = Get-AzSubscription | Out-GridView -PassThru\r\n",
"Set-AzContext -SubscriptionName $subscription"
],
"metadata": {
"azdata_cell_guid": "0cc44e68-3810-46f4-b29c-e6ad4321e384"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create Azure Resource Group"
],
"metadata": {
"azdata_cell_guid": "c17dd324-5c55-484f-8a25-2a5a7e43633e"
}
},
{
"cell_type": "code",
"source": [
"# Create Azure resource group, if necessary\r\n",
"$rg = Get-AzResourceGroup | Where ResourceGroupName -eq $ResourceGroupName\r\n",
"\r\n",
"if (!$rg)\r\n",
"{\r\n",
" # Need to create a new resource group\r\n",
" Write-Output \"Resource Group $ResourceGroupName does not exist. Creating...\"\r\n",
" $rg = New-AzResourceGroup -Name $ResourceGroupName -Location $Location\r\n",
"}"
],
"metadata": {
"azdata_cell_guid": "2c37ef31-5f47-4918-a2b3-05e11aab28da"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create a storage account\r\n",
"VMs require storage resources for OS, SQL data and logs. Create a new storage account as a place for it."
],
"metadata": {
"azdata_cell_guid": "11b78695-40d3-45d7-8e3c-32d086ddf94a"
}
},
{
"cell_type": "code",
"source": [
"$StorageAccount = Get-AzStorageAccount | Where StorageAccountName -eq $StorageAccountName\r\n",
"\r\n",
"if (!$StorageAccount)\r\n",
"{\r\n",
" Write-Output \"Storage Account $StorageName does not exist. Creating...\"\r\n",
" $StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName -SkuName $StorageSku -Kind \"Storage\" -Location $Location\r\n",
"}"
],
"metadata": {
"azdata_cell_guid": "f992bf91-a84a-40c2-813b-cb778907370d",
"tags": []
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Configure network settings\r\n",
"Create a virtual network, subnet, and a public IP address. These resources are used to provide network connectivity to the virtual machine and connect it to the internet."
],
"metadata": {
"azdata_cell_guid": "202634eb-7edf-4ff4-8486-fffbda45dbc8"
}
},
{
"cell_type": "code",
"source": [
"$SubnetName = $ResourceGroupName + \"subnet\"\r\n",
"$VnetName = $ResourceGroupName + \"vnet\"\r\n",
"$PipName = $ResourceGroupName + $(Get-Random)\r\n",
"\r\n",
"# Create a subnet configuration\r\n",
"$SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 192.168.1.0/24\r\n",
"\r\n",
"# Create a virtual network\r\n",
"$Vnet = New-AzVirtualNetwork -ResourceGroupName $ResourceGroupName -Location $Location `\r\n",
" -Name $VnetName -AddressPrefix 192.168.0.0/16 -Subnet $SubnetConfig\r\n",
"\r\n",
"# Create a public IP address and specify a DNS name\r\n",
"$Pip = New-AzPublicIpAddress -ResourceGroupName $ResourceGroupName -Location $Location `\r\n",
" -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name $PipName"
],
"metadata": {
"azdata_cell_guid": "af88cdae-1a62-4990-9231-094481c9337d"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"2. Create a network security group. Configure rules to allow remote desktop (RDP) and SQL Server connections."
],
"metadata": {
"azdata_cell_guid": "3b25e16e-b150-4a2e-80dc-66f2d18b43fb"
}
},
{
"cell_type": "code",
"source": [
"# Rule to allow remote desktop (RDP)\r\n",
"$NsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name \"RDPRule\" -Protocol Tcp `\r\n",
" -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * `\r\n",
" -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow\r\n",
"\r\n",
"#Rule to allow SQL Server connections on port 1433\r\n",
"$NsgRuleSQL = New-AzNetworkSecurityRuleConfig -Name \"MSSQLRule\" -Protocol Tcp `\r\n",
" -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * `\r\n",
" -DestinationAddressPrefix * -DestinationPortRange 1433 -Access Allow\r\n",
"\r\n",
"# Create the network security group\r\n",
"$NsgName = $ResourceGroupName + \"nsg\"\r\n",
"$Nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName `\r\n",
" -Location $Location -Name $NsgName `\r\n",
" -SecurityRules $NsgRuleRDP,$NsgRuleSQL"
],
"metadata": {
"azdata_cell_guid": "debe940d-0d0f-4540-be5b-4d6495d338e1"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"3. Create the network interface."
],
"metadata": {
"azdata_cell_guid": "d44de03c-d4f2-48ef-8a60-507069d6c08e"
}
},
{
"cell_type": "code",
"source": [
"$InterfaceName = $ResourceGroupName + \"int\"\r\n",
"$Interface = New-AzNetworkInterface -Name $InterfaceName `\r\n",
" -ResourceGroupName $ResourceGroupName -Location $Location `\r\n",
" -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $Pip.Id `\r\n",
" -NetworkSecurityGroupId $Nsg.Id"
],
"metadata": {
"azdata_cell_guid": "6dbb3ea0-b52f-4ed2-bd24-59096d134e88"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create the SQL VM\r\n",
"1. Define your credentials to sign in to the VM. The username is \"azureadmin\". Make sure you change <password> before running the command."
],
"metadata": {
"azdata_cell_guid": "b25dca9e-269b-45db-8cdf-efa53e2213d2"
}
},
{
"cell_type": "code",
"source": [
"# Define a credential object\r\n",
"$SecurePassword = ConvertTo-SecureString $secureVMPassword `\r\n",
" -AsPlainText -Force\r\n",
"$Cred = New-Object System.Management.Automation.PSCredential (\"azureadmin\", $securePassword)"
],
"metadata": {
"azdata_cell_guid": "40d0c992-860a-4b83-8104-16ec7e6e7983"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### Appendix: Azure SQL VM Offers\r\n",
"Run the following command to get updated list of offers for Microsoft SQL Server in your location. These settings can be used for the OfferName associated with this migration. Configure it for your purposes.\r\n",
"\r\n",
"Note that the SQL Version is first then appended with an operating system version. E.g.: \"WS2019\" means Windows Server 2019. Along with various versions of Windows Servers, there are also enterprise Linux versions such as RedHat Enterprise, Suse Enterprise, and Ubuntu. Some versions are BYOL (Bring Your Own License) aka <a href=\"https://azure.microsoft.com/en-us/pricing/hybrid-benefit/\">Hybrid Benefit</a>."
],
"metadata": {
"azdata_cell_guid": "ce2daf93-7c20-4073-babc-53c9187e5691"
}
},
{
"cell_type": "code",
"source": [
"Get-AzVMImageOffer -Location $Location -Publisher $PublisherName | Select Offer"
],
"metadata": {
"azdata_cell_guid": "e5808cbf-e6c6-4abe-8e1b-3c282bc7667c"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"2. Create a virtual machine configuration object and then create the VM. The following command creates a SQL Server 2017 Developer Edition VM on Windows Server 2016."
],
"metadata": {
"azdata_cell_guid": "2038172f-3f29-499c-ad68-88a1d96ead1f"
}
},
{
"cell_type": "code",
"source": [
"# Create a virtual machine configuration\r\n",
"\r\n",
"$VMConfig = New-AzVMConfig -VMName $VMName -VMSize Standard_DS13_V2 |\r\n",
" Set-AzVMOperatingSystem -Windows -ComputerName $VMName -Credential $Cred -ProvisionVMAgent -EnableAutoUpdate |\r\n",
" Set-AzVMSourceImage -PublisherName \"MicrosoftSQLServer\" -Offer \"SQL2017-WS2016\" -Skus \"SQLDEV\" -Version \"latest\" |\r\n",
" Add-AzVMNetworkInterface -Id $Interface.Id\r\n",
"\r\n",
""
],
"metadata": {
"azdata_cell_guid": "6625d7b2-6c97-432e-b5f5-be4ca93017ae"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Create Virtual Machine"
],
"metadata": {
"azdata_cell_guid": "c42ec570-331a-46ea-b358-b05e47320967"
}
},
{
"cell_type": "code",
"source": [
"# Create the VM\r\n",
"New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VMConfig"
],
"metadata": {
"azdata_cell_guid": "05fa1f3d-94e1-480f-ad20-d3006bafc6ac"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Install the SQL IaaS Agent\r\n",
"To get portal integration and SQL VM features, you must install the SQL Server IaaS Agent Extension. To install the agent on the new VM, run the following command after the VM is created."
],
"metadata": {
"azdata_cell_guid": "ef07b3d1-3e2d-45f0-b9d3-fb00be2a7da9"
}
},
{
"cell_type": "code",
"source": [
"Set-AzVMSqlServerExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -name \"SQLIaasExtension\" -version \"2.0\" -Location $Location"
],
"metadata": {
"azdata_cell_guid": "bb3b5436-c34b-44b3-b631-ea60c9dcf16a"
},
"outputs": [],
"execution_count": null
}
]
}

View File

@@ -0,0 +1,8 @@
# Azure SQL Provisioning
[Home](../readme.md)
## Notebooks in this Chapter
- [Create Azure SQL Virtual Machine](create-sqlvm.ipynb) - SQL Server on Azure Virtual Machines enables to use full versions of SQL Server in the cloud without having to manage any on-premises hardware. The virtual machine image gallery allows to create a SQL Server VM with the right version, edition, and operating system
- [Create Azure SQL Managed Instance](create-sqlmi.ipynb) - Azure SQL Managed Instance is the intelligent, scalable, cloud database service that combines the broadest SQL Server engine compatibility with all the benefits of a fully managed and evergreen platform as a service. An instance is a copy of the sqlservr.exe executable that runs as an operating system service
- [Create Azure SQL Database](create-sqldb.ipynb) - Azure SQL Database is Microsoft's fully managed cloud relational database service in Microsoft Azure. It shares the same code base as traditional SQL Servers but with Microsoft's Cloud first strategy the newest features of SQL Server are actually released to Azure SQL Database first. Use this notebook when a need is systematic collection of data that stores data in tables
This chapter contains Notebooks that help provision new Azure SQL resources that can be used as migration targets for existing on-premises SQL instances and databases. Use alongside the planning notebooks to use existing resources as the basis for the best type of resource to create and how it should be configured. You can use the notebooks and configure the settings manually or provide a provisioning plan created by the [Create Provisioning Plan](../provisioning/provisioning-plan.ipynb) notebook.