在 Azure Stack Hub 中使用 Powershell 部署模板Deploy a template using Powershell in Azure Stack Hub

可以使用 PowerShell 将 Azure 资源管理器模板部署到 Azure Stack Hub。You can use PowerShell to deploy Azure Resource Manager templates to Azure Stack Hub. 本文介绍如何使用 PowerShell 部署模板。This article describes how to use PowerShell to deploy a template.

运行 AzureRM PowerShell cmdletRun AzureRM PowerShell cmdlets

此示例使用 AzureRM PowerShell cmdlet 和存储在 GitHub 上的模板。This example uses AzureRM PowerShell cmdlets and a template stored on GitHub. 此模板创建 Windows Server 2012 R2 Datacenter 虚拟机。The template creates a Windows Server 2012 R2 Datacenter virtual machine.

备注

在尝试此示例之前,请确保已为 Azure Stack Hub 用户配置了 PowerShellBefore you try this example, make sure that you've configured PowerShell for an Azure Stack Hub user.

  1. 浏览 AzureStack-QuickStart-Templates 存储库,找到 101-simple-windows-vm 模板。Browse the AzureStack-QuickStart-Templates repo and find the 101-simple-windows-vm template. 将模板保存到此位置:C:\templates\azuredeploy-101-simple-windows-vm.jsonSave the template to this location: C:\templates\azuredeploy-101-simple-windows-vm.json.

  2. 打开权限提升的 PowerShell 命令提示符。Open an elevated PowerShell command prompt.

  3. 将以下脚本中的 usernamepassword 替换为你的用户名和密码,然后运行脚本:Replace username and password in the following script with your user name and password, then run the script:

    # Set deployment variables
    $myNum = "001" # Modify this per deployment
    $RGName = "myRG$myNum"
    $myLocation = "yourregion" # local for the ASDK
    
    # Create resource group for template deployment
    New-AzureRmResourceGroup -Name $RGName -Location $myLocation
    
    # Deploy simple IaaS template
    New-AzureRmResourceGroupDeployment `
        -Name myDeployment$myNum `
        -ResourceGroupName $RGName `
        -TemplateUri <path>\AzureStack-QuickStart-Templates\101-vm-windows-create\azuredeploy.json `
        -AdminUsername <username> `
        -AdminPassword ("<password>" | ConvertTo-SecureString -AsPlainText -Force)
    

    重要

    每次运行此脚本时,都应递增 $myNum 参数的值,以避免覆盖你的部署。Every time you run this script, increment the value of the $myNum parameter to prevent overwriting your deployment.

  4. 打开 Azure Stack Hub 门户,选择“浏览” ,然后选择“虚拟机” 以查找新虚拟机 (myDeployment001)。Open the Azure Stack Hub portal, select Browse, and then select Virtual machines to find your new virtual machine (myDeployment001).

取消正在运行的模板部署Cancel a running template deployment

若要取消正在运行的模板部署,请使用 Stop-AzureRmResourceGroupDeployment PowerShell cmdlet。To cancel a running template deployment, use the Stop-AzureRmResourceGroupDeployment PowerShell cmdlet.

后续步骤Next steps