快速入门:使用 PowerShell 创建 Azure 门户仪表板Quickstart: Create an Azure portal dashboard with PowerShell

Azure 门户中的仪表板可以集中且有组织地呈现你的云资源。A dashboard in the Azure portal is a focused and organized view of your cloud resources. 本文重点介绍使用 Az.Portal PowerShell 模块创建仪表板的过程。This article focuses on the process of using the Az.Portal PowerShell module to create a dashboard. 仪表板会显示虚拟机 (VM) 的性能以及一些静态信息和链接。The dashboard shows the performance of a virtual machine (VM), as well as some static information and links.

要求Requirements

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

如果选择在本地使用 PowerShell,则本文要求安装 Az PowerShell 模块,并使用 Connect-AzAccount cmdlet 连接到 Azure 帐户。If you choose to use PowerShell locally, this article requires that you install the Az PowerShell module and connect to your Azure account using the Connect-AzAccount cmdlet. 有关安装 Az PowerShell 模块的详细信息,请参阅安装 Azure PowerShellFor more information about installing the Az PowerShell module, see Install Azure PowerShell.

重要

尽管 Az.Portal PowerShell 模块为预览版,但必须使用 Install-Module cmdlet 从 Az PowerShell 模块单独安装它。While the Az.Portal PowerShell module is in preview, you must install it separately from from the Az PowerShell module using the Install-Module cmdlet. 此 PowerShell 模块正式发布后,它会包含在将来的 Az PowerShell 模块发行版中,并在 Azure Cloud Shell 中原生提供。Once this PowerShell module becomes generally available, it becomes part of future Az PowerShell module releases and available natively from within Azure Cloud Shell.

Install-Module -Name Az.Portal

选择特定 Azure 订阅Choose a specific Azure subscription

如果有多个 Azure 订阅,请选择应当计费的资源所在的相应订阅。If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources should be billed. 使用 Set-AzContext cmdlet 选择特定订阅。Select a specific subscription using the Set-AzContext cmdlet.

Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000

定义变量Define variables

你将重复使用几条信息。You'll be using several pieces of information repeatedly. 创建变量以存储信息。Create variables to store the information.

# Name of resource group used throughout this article
$resourceGroupName = 'myResourceGroup'

# Azure region
$location = 'chinaeast'

# Dashboard Title
$dashboardTitle = 'Simple VM Dashboard'

# Dashboard Name
$dashboardName = $dashboardTitle -replace '\s'

# Your Azure Subscription ID
$subscriptionID = (Get-AzContext).Subscription.Id

# Name of test VM
$vmName = 'SimpleWinVM'

创建资源组Create a resource group

使用 New-AzResourceGroup cmdlet 创建 Azure 资源组Create an Azure resource group using the New-AzResourceGroup cmdlet. 资源组是在其中以组的形式部署和管理 Azure 资源的逻辑容器。A resource group is a logical container in which Azure resources are deployed and managed as a group.

下面的示例创建一个资源组,该资源组基于在 $location 变量中指定的区域中 $resourceGroupName 变量中的名称。The following example creates a resource group based on the name in the $resourceGroupName variable in the region specified in the $location variable.

New-AzResourceGroup -Name $resourceGroupName -Location $location

创建虚拟机Create a virtual machine

在本快速入门的下一部分中创建的仪表板需要一台现有 VM。The dashboard you create in the next part of this quickstart requires an existing VM. 按照以下步骤创建 VM。Create a VM by following these steps.

将 VM 的登录凭据存储在一个变量中。Store login credentials for the VM in a variable. 密码必须是复杂密码。The password must be complex. 这是一个新的用户名和密码;它不是用于登录 Azure 的帐户。This is a new user name and password; it's not, for example, the account you use to sign in to Azure. 有关详细信息,请参阅用户名要求密码要求For more information, see username requirements and password requirements.

$Cred = Get-Credential

创建 VM。Create the VM.

$AzVmParams = @{
  ResourceGroupName = $resourceGroupName
  Name = $vmName
  Location = $location
  Credential = $Cred
}
New-AzVm @AzVmParams

VM 部署现在将开始进行,通常需要几分钟才能完成。The VM deployment now starts and typically takes a few minutes to complete. 部署完成后,请转到下一部分。After deployment completes, move on to the next section.

下载仪表板模板Download the dashboard template

由于 Azure 仪表板是资源,所以它们可以表示为 JSON。Since Azure dashboards are resources, they can be represented as JSON. 下面的代码会下载示例仪表板的 JSON 表示形式。The following code downloads a JSON representation of a sample dashboard. 有关详细信息,请参阅 Azure 仪表板结构For more information, see The structure of Azure Dashboards.

$myPortalDashboardTemplateUrl = 'https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/azure-portal/portal-dashboard-template-testvm.json'

$myPortalDashboardTemplatePath = "$env:TEMP\portal-dashboard-template-testvm.json"

Invoke-WebRequest -Uri $myPortalDashboardTemplateUrl -OutFile $myPortalDashboardTemplatePath -UseBasicParsing

自定义模板Customize the template

通过运行以下代码来自定义下载的模板。Customize the downloaded template by running the following code.

$Content = Get-Content -Path $myPortalDashboardTemplatePath -Raw
$Content = $Content -replace '<subscriptionID>', $subscriptionID
$Content = $Content -replace '<rgName>', $resourceGroupName
$Content = $Content -replace '<vmName>', $vmName
$Content = $Content -replace '<dashboardTitle>', $dashboardTitle
$Content = $Content -replace '<location>', $location
$Content | Out-File -FilePath $myPortalDashboardTemplatePath -Force

有关详细信息,请参阅 Microsoft 门户仪表板模板参考For more information, see Microsoft portal dashboards template reference.

部署仪表板模板Deploy the dashboard template

可以使用 Az.Portal 模块中的 New-AzPortalDashboard cmdlet 直接从 PowerShell 部署模板。You can use the New-AzPortalDashboard cmdlet that's part of the Az.Portal module to deploy the template directly from PowerShell.

$DashboardParams = @{
  DashboardPath = $myPortalDashboardTemplatePath
  ResourceGroupName = $resourceGroupName
  DashboardName = $dashboardName
}
New-AzPortalDashboard @DashboardParams

查看已部署的资源Review the deployed resources

检查仪表板是否已成功创建。Check that the dashboard was created successfully.

Get-AzPortalDashboard -Name $dashboardName -ResourceGroupName $resourceGroupName

验证是否可以在 Azure 门户中查看有关 VM 的数据。Verify that you can see data about the VM from within the Azure portal.

  1. 在 Azure 门户中,选择“仪表板”。In the Azure portal, select Dashboard.

    Azure 门户导航到仪表板

  2. 在仪表板页面上,选择“简单的 VM 仪表板”。On the dashboard page, select Simple VM Dashboard.

    导航到“简单的 VM 仪表板”

  3. 查看仪表板。Review the dashboard. 你可以看到某些内容是静态的,但也有一些图表显示 VM 的性能。You can see that some of the content is static, but there are also charts that show the performance of the VM.

    查看简单的 VM 仪表板

清理资源Clean up resources

若要删除 VM 和关联仪表板,请删除包含它们的资源组。To remove the VM and associated dashboard, delete the resource group that contains them.

注意

以下示例删除指定的资源组及其包含的所有资源。The following example deletes the specified resource group and all resources contained within it. 如果指定的资源组中存在本文范围外的资源,这些资源也会被删除。If resources outside the scope of this article exist in the specified resource group, they will also be deleted.

Remove-AzResourceGroup -Name $resourceGroupName

后续步骤Next steps

有关 Az.Portal PowerShell 模块中包含的 cmdlet 的详细信息,请参阅:For more information about the cmdlets contained in the Az.Portal PowerShell module, see: