如何使用 Azure PowerShell 预配 SQL Server 虚拟机How to provision SQL Server virtual machines with Azure PowerShell

本指南解释了使用 Azure PowerShell 创建 Windows SQL Server VM 的选项。This guide explains your options to create Windows SQL Server VMs with Azure PowerShell. 有关具有多个默认值的简明 Azure PowerShell 示例,请参阅 SQL VM Azure PowerShell 快速入门For a streamlined Azure PowerShell example with more default values, see the SQL VM Azure PowerShell quickstart.

如果没有 Azure 订阅,可在开始前创建一个 试用帐户If you don't have an Azure subscription, create a trial account before you begin.

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

配置订阅Configure your subscription

  1. 打开 PowerShell,通过运行 Connect-AzAccount 命令建立对 Azure 帐户的访问。Open PowerShell and establish access to your Azure account by running the Connect-AzAccount command.

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 此时应会显示用于输入凭据的屏幕。You should see a screen to enter your credentials. 使用登录 Azure 门户时所用的相同电子邮件和密码。Use the same email and password that you use to sign in to the Azure portal.

定义映像变量Define image variables

若要重复使用值并简化脚本创建,请先定义一些变量。To reuse values and simplify script creation, start by defining a number of variables. 根据需要更改参数值,但在修改提供的值时,请注意与名称长度和特殊字符相关的命名限制。Change the parameter values as you want, but be aware of naming restrictions related to name lengths and special characters when modifying the values provided.

位置和资源组Location and resource group

定义数据区域,以及要在其中创建其他 VM 资源的资源组。Define the data region and the resource group into which you create the other VM resources.

根据需要进行修改,然后运行这些 cmdlet 以初始化这些变量。Modify as you want and then run these cmdlets to initialize these variables.

$Location = "chinaeast"
$ResourceGroupName = "sqlvm2"

存储属性Storage properties

定义存储帐户,以及虚拟机使用的存储类型。Define the storage account and the type of storage to be used by the virtual machine.

根据需要进行修改,然后运行以下 cmdlet 以初始化这些变量。Modify as you want and then run the following cmdlet to initialize these variables. 我们建议将高级 SSD 用于生产工作负载。We recommend using premium SSDs for production workloads.

$StorageName = $ResourceGroupName + "storage"
$StorageSku = "Premium_LRS"

网络属性Network properties

在虚拟机中定义网络使用的属性。Define the properties to be used by the network in the virtual machine.

  • 网络接口Network interface
  • TCP/IP 分配方法TCP/IP allocation method
  • 虚拟网络名称Virtual network name
  • 虚拟子网名称Virtual subnet name
  • 虚拟网络的 IP 地址范围Range of IP addresses for the virtual network
  • 子网的 IP 地址范围Range of IP addresses for the subnet
  • 公共域名标签Public domain name label

根据需要进行修改,然后运行此 cmdlet 以初始化这些变量。Modify as you want and then run this cmdlet to initialize these variables.

$InterfaceName = $ResourceGroupName + "ServerInterface"
$NsgName = $ResourceGroupName + "nsg"
$TCPIPAllocationMethod = "Dynamic"
$VNetName = $ResourceGroupName + "VNet"
$SubnetName = "Default"
$VNetAddressPrefix = "10.0.0.0/16"
$VNetSubnetAddressPrefix = "10.0.0.0/24"
$DomainName = $ResourceGroupName

虚拟机属性Virtual machine properties

定义虚拟机名称、计算机名称、虚拟机大小和虚拟机的操作系统磁盘名称。Define the virtual machine name, the computer name, the virtual machine size, and the operating system disk name for the virtual machine.

根据需要进行修改,然后运行此 cmdlet 以初始化这些变量。Modify as you want and then run this cmdlet to initialize these variables.

$VMName = $ResourceGroupName + "VM"
$ComputerName = $ResourceGroupName + "Server"
$VMSize = "Standard_DS13"
$OSDiskName = $VMName + "OSDisk"

选择 SQL Server 映像Choose a SQL Server image

使用以下变量来定义要用于虚拟机的 SQL Server 映像。Use the following variables to define the SQL Server image to use for the virtual machine.

  1. 首先,使用 Get-AzVMImageOffer 命令列出所有 SQL Server 映像套餐。First, list out all of the SQL Server image offerings with the Get-AzVMImageOffer command. 此命令将列出 Azure 门户中当前提供的映像,以及只能通过 PowerShell 安装的早期映像:This command lists current images that are available in the Azure Portal and also older images that can only be installed with PowerShell:

    Get-AzVMImageOffer -Location $Location -Publisher 'MicrosoftSQLServer'
    
  2. 对于本教程,请使用以下变量指定 Windows Server 2016 上的 SQL Server 2017。For this tutorial, use the following variables to specify SQL Server 2017 on Windows Server 2016.

    $OfferName = "SQL2017-WS2016"
    $PublisherName = "MicrosoftSQLServer"
    $Version = "latest"
    
  3. 接下来,列出套餐的可用版本。Next, list the available editions for your offer.

    Get-AzVMImageSku -Location $Location -Publisher 'MicrosoftSQLServer' -Offer $OfferName | Select Skus
    
  4. 对于本教程,请使用 SQL Server 2017 Developer Edition (SQLDEV)。For this tutorial, use the SQL Server 2017 Developer edition (SQLDEV). Developer Edition 针对测试和开发自由授权,用户只需支付运行 VM 的成本。The Developer edition is freely licensed for testing and development, and you only pay for the cost of running the VM.

    $Sku = "SQLDEV"
    

创建资源组Create a resource group

在 Resource Manager 部署模型中,创建的第一个对象是资源组。With the Resource Manager deployment model, the first object that you create is the resource group. 使用 New-AzResourceGroup cmdlet 创建 Azure 资源组及其资源。Use the New-AzResourceGroup cmdlet to create an Azure resource group and its resources. 指定前面初始化的资源组名称和位置变量。Specify the variables that you previously initialized for the resource group name and location.

运行此 cmdlet 来创建新的资源组。Run this cmdlet to create your new resource group.

New-AzResourceGroup -Name $ResourceGroupName -Location $Location

创建存储帐户Create a storage account

虚拟机需要使用存储资源来存储操作系统磁盘及 SQL Server 数据和日志文件。The virtual machine requires storage resources for the operating system disk and for the SQL Server data and log files. 为简单起见,我们将为两者创建单个磁盘。For simplicity, you'll create a single disk for both. 稍后可以使用 Add-AzureDisk cmdlet 来附加其他磁盘,以便将 SQL Server 数据和日志文件放在专用磁盘上。You can attach additional disks later using the Add-AzureDisk cmdlet to place your SQL Server data and log files on dedicated disks. 使用 New-AzStorageAccount cmdlet 在新资源组中创建标准存储帐户。Use the New-AzStorageAccount cmdlet to create a standard storage account in your new resource group. 指定前面初始化的存储帐户名称、存储 SKU 名称和位置变量。Specify the variables that you previously initialized for the storage account name, storage Sku name, and location.

运行此 cmdlet 来创建新的存储帐户。Run this cmdlet to create your new storage account.

$StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
   -Name $StorageName -SkuName $StorageSku `
   -Kind "Storage" -Location $Location

Tip

创建存储帐户可能需要花费几分钟时间。Creating the storage account can take a few minutes.

创建网络资源Create network resources

虚拟机需要使用多个网络资源来建立网络连接。The virtual machine requires a number of network resources for network connectivity.

  • 每个虚拟机需要一个虚拟网络。Each virtual machine requires a virtual network.
  • 必须为每个虚拟网络至少定义一个子网。A virtual network must have at least one subnet defined.
  • 必须使用公共或专用 IP 地址定义网络接口。A network interface must be defined with either a public or a private IP address.

创建虚拟网络子网配置Create a virtual network subnet configuration

首先创建虚拟网络的子网配置。Start by creating a subnet configuration for your virtual network. 本教程使用 New-AzVirtualNetworkSubnetConfig cmdlet 创建默认子网。For this tutorial, create a default subnet using the New-AzVirtualNetworkSubnetConfig cmdlet. 指定前面初始化的子网名称和地址前缀变量。Specify the variables that you previously initialized for the subnet name and address prefix.

Note

可以使用此 cmdlet 来定义虚拟网络子网配置的其他属性,但这已超出本教程的范围。You can define additional properties of the virtual network subnet configuration using this cmdlet, but that is beyond the scope of this tutorial.

运行此 cmdlet 来创建虚拟子网配置。Run this cmdlet to create your virtual subnet configuration.

$SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VNetSubnetAddressPrefix

创建虚拟网络Create a virtual network

接下来,使用 New-AzVirtualNetwork cmdlet 在新资源组中创建虚拟网络。Next, create your virtual network in your new resource group using the New-AzVirtualNetwork cmdlet. 指定前面初始化的名称、位置和地址前缀变量。Specify the variables that you previously initialized for the name, location, and address prefix. 使用在上一步骤中定义的子网配置。Use the subnet configuration that you defined in the previous step.

运行此 cmdlet 来创建虚拟网络。Run this cmdlet to create your virtual network.

$VNet = New-AzVirtualNetwork -Name $VNetName `
   -ResourceGroupName $ResourceGroupName -Location $Location `
   -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig

创建公共 IP 地址Create the public IP address

定义虚拟网络后,必须配置 IP 地址才能连接到虚拟机。Now that your virtual network is defined, you must configure an IP address for connectivity to the virtual machine. 本教程使用动态 IP 地址创建公共 IP 地址,以支持 Internet 连接。For this tutorial, create a public IP address using dynamic IP addressing to support Internet connectivity. 使用 New-AzPublicIpAddress cmdlet 在新资源组中创建公共 IP 地址。Use the New-AzPublicIpAddress cmdlet to create the public IP address in your new resource group. 指定前面初始化的名称、位置、分配方法和 DNS 域名标签变量。Specify the variables that you previously initialized for the name, location, allocation method, and DNS domain name label.

Note

可以使用此 cmdlet 来定义公共 IP 地址的其他属性,但这已超出本初步教程的范围。You can define additional properties of the public IP address using this cmdlet, but that is beyond the scope of this initial tutorial. 也可以创建专用地址或具有静态地址的地址,但这也超出了本教程的范围。You could also create a private address or an address with a static address, but that is also beyond the scope of this tutorial.

运行此 cmdlet 来创建公共 IP 地址。Run this cmdlet to create your public IP address.

$PublicIp = New-AzPublicIpAddress -Name $InterfaceName `
   -ResourceGroupName $ResourceGroupName -Location $Location `
   -AllocationMethod $TCPIPAllocationMethod -DomainNameLabel $DomainName

创建网络安全组Create the network security group

为确保 VM 和 SQL Server 流量安全,请创建网络安全组。To secure the VM and SQL Server traffic, create a network security group.

  1. 首先,为 RDP 创建网络安全组规则以允许远程桌面连接。First, create a network security group rule for RDP to allow remote desktop connections.

    $NsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name "RDPRule" -Protocol Tcp `
      -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * `
      -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow
    
  2. 配置一个网络安全组规则以允许 TCP 端口 1433 上的流量。Configure a network security group rule that allows traffic on TCP port 1433. 这样就可以通过 Internet 连接到 SQL Server。Doing so enables connections to SQL Server over the internet.

    $NsgRuleSQL = New-AzNetworkSecurityRuleConfig -Name "MSSQLRule"  -Protocol Tcp `
      -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * `
      -DestinationAddressPrefix * -DestinationPortRange 1433 -Access Allow
    
  3. 创建网络安全组。Create the network security group.

    $Nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName `
      -Location $Location -Name $NsgName `
      -SecurityRules $NsgRuleRDP,$NsgRuleSQL
    

创建网络接口Create the network interface

现在,可为虚拟机创建网络接口。You're now ready to create the network interface for your virtual machine. 使用 New-AzNetworkInterface cmdlet 在新资源组中创建网络接口。Use the New-AzNetworkInterface cmdlet to create your network interface in your new resource group. 指定前面定义的名称、位置、子网和公共 IP 地址。Specify the name, location, subnet, and public IP address previously defined.

运行此 cmdlet 来创建网络接口。Run this cmdlet to create your network interface.

$Interface = New-AzNetworkInterface -Name $InterfaceName `
   -ResourceGroupName $ResourceGroupName -Location $Location `
   -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PublicIp.Id `
   -NetworkSecurityGroupId $Nsg.Id

配置 VM 对象Configure a VM object

现已定义存储和网络资源,接下来可以定义虚拟机的计算资源。Now that storage and network resources are defined, you're ready to define compute resources for the virtual machine.

  • 指定虚拟机大小和各项操作系统属性。Specify the virtual machine size and various operating system properties.
  • 指定前面创建的网络接口。Specify the network interface that you previously created.
  • 定义 Blob 存储。Define blob storage.
  • 指定操作系统磁盘。Specify the operating system disk.

创建 VM 对象Create the VM object

首先指定虚拟机大小。Start by specifying the virtual machine size. 本教程指定了 DS13。For this tutorial, specify a DS13. 使用 New-AzVMConfig cmdlet 创建可配置的虚拟机对象。Use the New-AzVMConfig cmdlet to create a configurable virtual machine object. 指定前面初始化的名称和大小变量。Specify the variables that you previously initialized for the name and size.

运行此 cmdlet 来创建虚拟机对象。Run this cmdlet to create the virtual machine object.

$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize

创建一个凭据对象,以保留本地管理员凭据的名称和密码Create a credential object to hold the name and password for the local administrator credentials

必须先提供本地管理员帐户的凭据作为安全字符串,才能设置虚拟机的操作系统属性。Before you can set the operating system properties for the virtual machine, you must supply the credentials for the local administrator account as a secure string. 为实现此目的,请使用 Get-Credential cmdlet。To accomplish this, use the Get-Credential cmdlet.

运行以下 cmdlet,然后在 PowerShell 凭据请求窗口中,键入虚拟机中本地管理员帐户使用的名称和密码。Run the following cmdlet and, in the PowerShell credential request window, type the name and password to use for the local administrator account in the virtual machine.

$Credential = Get-Credential -Message "Type the name and password of the local administrator account."

设置虚拟机的操作系统属性Set the operating system properties for the virtual machine

现在,可以使用 Set-AzVMOperatingSystem cmdlet 设置虚拟机的操作系统属性。Now you're ready to set the virtual machine's operating system properties with the Set-AzVMOperatingSystem cmdlet.

  • 将操作系统类型设置为 Windows。Set the type of operating system as Windows.
  • 要求安装虚拟机代理Require the virtual machine agent to be installed.
  • 指定 cmdlet 要启用自动更新。Specify that the cmdlet enables auto update.
  • 指定前面初始化的虚拟机名称、计算机名和凭据变量。Specify the variables that you previously initialized for the virtual machine name, the computer name, and the credential.

运行此 cmdlet 来设置虚拟机的操作系统属性。Run this cmdlet to set the operating system properties for your virtual machine.

$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine `
   -Windows -ComputerName $ComputerName -Credential $Credential `
   -ProvisionVMAgent -EnableAutoUpdate

将网络接口添加到虚拟机Add the network interface to the virtual machine

接下来,运行 Add-AzVMNetworkInterface cmdlet,以使用前面定义的变量来添加网络接口。Next, use the Add-AzVMNetworkInterface cmdlet to add the network interface using the variable that you defined earlier.

运行此 cmdlet 来设置虚拟机的网络接口。Run this cmdlet to set the network interface for your virtual machine.

$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id

设置虚拟机所要使用的磁盘的 Blob 存储位置Set the blob storage location for the disk to be used by the virtual machine

接下来,使用前面定义的变量设置 VM 磁盘的 Blob 存储位置。Next, set the blob storage location for the VM's disk using the variables that you defined earlier.

运行此 cmdlet 来设置 Blob 存储位置。Run this cmdlet to set the blob storage location.

$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"

设置虚拟机的操作系统磁盘属性Set the operating system disk properties for the virtual machine

接下来,使用 Set-AzVMOSDisk cmdlet 设置虚拟机的操作系统磁盘属性。Next, set the operating system disk properties for the virtual machine using the Set-AzVMOSDisk cmdlet.

  • 指定来自映像的虚拟机的操作系统。Specify that the operating system for the virtual machine will come from an image.
  • 将缓存设置为只读(因为 SQL Server 将安装在同一磁盘上)。Set caching to read only (because SQL Server is being installed on the same disk).
  • 指定前面初始化的 VM 名称和操作系统磁盘变量。Specify the variables that you previously initialized for the VM name and the operating system disk.

运行此 cmdlet 来设置虚拟机的操作系统磁盘属性。Run this cmdlet to set the operating system disk properties for your virtual machine.

$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -Name `
   $OSDiskName -VhdUri $OSDiskUri -Caching ReadOnly -CreateOption FromImage

指定虚拟机的平台映像Specify the platform image for the virtual machine

最后一个配置步骤是指定虚拟机的平台映像。The last configuration step is to specify the platform image for your virtual machine. 本教程使用最新的 SQL Server 2016 CTP 映像。For this tutorial, use the latest SQL Server 2016 CTP image. 运行 Set-AzVMSourceImage cmdlet,以通过前面定义的变量来使用此映像。Use the Set-AzVMSourceImage cmdlet to use this image with the variables that you defined earlier.

运行此 cmdlet 来指定虚拟机的平台映像。Run this cmdlet to specify the platform image for your virtual machine.

$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine `
   -PublisherName $PublisherName -Offer $OfferName `
   -Skus $Sku -Version $Version

创建 SQL VMCreate the SQL VM

完成配置步骤后,接下来可以创建虚拟机。Now that you've finished the configuration steps, you're ready to create the virtual machine. 运行 New-AzVM cmdlet,以使用你定义的变量来创建虚拟机。Use the New-AzVM cmdlet to create the virtual machine using the variables that you defined.

Tip

创建 VM 可能需要几分钟时间。Creating the VM can take a few minutes.

运行此 cmdlet 来创建虚拟机。Run this cmdlet to create your virtual machine.

New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine

虚拟机已创建。The virtual machine is created.

Note

如果收到有关启动诊断的错误,可将其忽略。If you get an error about boot diagnostics, you can ignore it. 由于为虚拟机磁盘指定的存储帐户是高级存储帐户,因此会创建标准存储帐户用于启动诊断。A standard storage account is created for boot diagnostics because the specified storage account for the virtual machine's disk is a premium storage account.

安装 SQL IaaS 代理Install the SQL Iaas Agent

SQL Server 虚拟机通过 SQL Server IaaS 代理扩展支持自动化管理功能。SQL Server virtual machines support automated management features with the SQL Server IaaS Agent Extension. 若要在新 VM 上安装该代理,请在创建 VM 后运行以下命令。To install the agent on the new VM, run the following command after it's created.

Set-AzVMSqlServerExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -name "SQLIaasExtension" -version "1.2" -Location $Location

停止或删除 VMStop or remove a VM

如果不需要让 VM 持续运行,可以在不使用它时将它停止,以免产生不必要的费用。If you don't need the VM to run continuously, you can avoid unnecessary charges by stopping it when not in use. 以下命令可停止 VM,但保留它供将来使用。The following command stops the VM but leaves it available for future use.

Stop-AzVM -Name $VMName -ResourceGroupName $ResourceGroupName

还可以使用 Remove-AzResourceGroup 命令永久删除与虚拟机关联的所有资源。You can also permanently delete all resources associated with the virtual machine with the Remove-AzResourceGroup command. 这样做也会永久删除虚拟机,因此请小心使用此命令。Doing so permanently deletes the virtual machine as well, so use this command with care.

示例脚本Example script

以下脚本包含本教程的完整 PowerShell 脚本。The following script contains the complete PowerShell script for this tutorial. 假设你已经设置了 Azure 订阅以与 Connect-AzAccount-Environment AzureChinaCloudSelect-AzSubscription 命令一起使用。It assumes that you have already set up the Azure subscription to use with the Connect-AzAccount -Environment AzureChinaCloud and Select-AzSubscription commands.

# Variables

## Global
$Location = "chinaeast"
$ResourceGroupName = "sqlvm2"

## Storage
$StorageName = $ResourceGroupName + "storage"
$StorageSku = "Premium_LRS"

## Network
$InterfaceName = $ResourceGroupName + "ServerInterface"
$NsgName = $ResourceGroupName + "nsg"
$VNetName = $ResourceGroupName + "VNet"
$SubnetName = "Default"
$VNetAddressPrefix = "10.0.0.0/16"
$VNetSubnetAddressPrefix = "10.0.0.0/24"
$TCPIPAllocationMethod = "Dynamic"
$DomainName = $ResourceGroupName

##Compute
$VMName = $ResourceGroupName + "VM"
$ComputerName = $ResourceGroupName + "Server"
$VMSize = "Standard_DS13"
$OSDiskName = $VMName + "OSDisk"

##Image
$PublisherName = "MicrosoftSQLServer"
$OfferName = "SQL2017-WS2016"
$Sku = "SQLDEV"
$Version = "latest"

# Sign in 
Connect-AzAccount -Environment AzureChinaCloud

# Resource Group
New-AzResourceGroup -Name $ResourceGroupName -Location $Location

# Storage
$StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName -SkuName $StorageSku -Kind "Storage" -Location $Location

# Network
$SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VNetSubnetAddressPrefix
$VNet = New-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig
$PublicIp = New-AzPublicIpAddress -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod $TCPIPAllocationMethod -DomainNameLabel $DomainName
$NsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name "RDPRule" -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow
$NsgRuleSQL = New-AzNetworkSecurityRuleConfig -Name "MSSQLRule"  -Protocol Tcp -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 1433 -Access Allow
$Nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $Location -Name $NsgName -SecurityRules $NsgRuleRDP,$NsgRuleSQL
$Interface = New-AzNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PublicIp.Id -NetworkSecurityGroupId $Nsg.Id

# Compute
$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize
$Credential = Get-Credential -Message "Type the name and password of the local administrator account."
$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate #-TimeZone = $TimeZone
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id
$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -Caching ReadOnly -CreateOption FromImage

# Image
$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName $PublisherName -Offer $OfferName -Skus $Sku -Version $Version

# Create the VM in Azure
New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine

# Add the SQL IaaS Extension
Set-AzVMSqlServerExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -name "SQLIaasExtension" -version "1.2" -Location $Location

后续步骤Next steps

在创建虚拟机后,可以:After the virtual machine is created, you can: