快速入门:使用 PowerShell 创建 Azure 门户仪表板

Azure 门户中的仪表板可以集中且有组织地呈现你的云资源。 本文重点介绍使用 Az.Portal PowerShell 模块创建仪表板的过程。 仪表板会显示你创建的虚拟机 (VM) 的性能以及一些静态信息和链接。

Azure 门户中的仪表板可以集中且有组织地呈现你的云资源。 本快速入门介绍如何使用 Az.Portal PowerShell 创建仪表板。 示例仪表板会显示虚拟机 (VM) 的性能以及一些静态信息和链接。

先决条件

注意

在可以在由世纪互联运营的 Microsoft Azure 中使用 Azure CLI 之前,请先运行 az cloud set -n AzureChinaCloud 来更改云环境。 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloud

选择特定 Azure 订阅

如果有多个 Azure 订阅,请选择应当计费的资源所在的相应订阅。 使用 Set-AzContext cmdlet 选择特定订阅。

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

定义变量

你将重复使用几条信息。 创建变量以存储信息。

# 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 = 'myVM1'

创建资源组

使用 New-AzResourceGroup cmdlet 创建 Azure 资源组。 资源组是在其中以组的形式部署和管理 Azure 资源的逻辑容器。

下面的示例创建一个资源组,该资源组基于在 $location 变量中指定的区域中 $resourceGroupName 变量中的名称。

New-AzResourceGroup -Name $resourceGroupName -Location $location

创建虚拟机

在本快速入门的下一部分中创建的仪表板需要一台现有 VM。 按照以下步骤创建 VM。

将 VM 的登录凭据存储在一个变量中。 密码必须是复杂密码。 这是一个新的用户名和密码;它不是用于登录 Azure 的帐户。 有关详细信息,请参阅用户名要求密码要求

$Cred = Get-Credential

创建 VM。

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

VM 部署现在将开始进行,通常需要几分钟才能完成。 部署完成后,请转到下一部分。

下载仪表板模板

由于 Azure 仪表板是资源,所以它们可以表示为 JSON。 下面的代码会下载示例仪表板的 JSON 表示形式。 有关详细信息,请参阅 Azure 仪表板结构

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

$myPortalDashboardTemplatePath = "$HOME\portal-dashboard-template-testvm.json"

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

自定义模板

通过运行以下代码来自定义下载的模板。

$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 门户仪表板模板参考

部署仪表板模板

可以使用 Az.Portal 模块中的 New-AzPortalDashboard cmdlet 直接从 PowerShell 部署模板。

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

查看已部署的资源

检查仪表板是否已成功创建。

Get-AzPortalDashboard -Name $dashboardName -ResourceGroupName $resourceGroupName

验证是否可以在 Azure 门户仪表板中查看虚拟机的相关数据。

  1. 在 Azure 门户菜单中,选择“仪表板”。

    Screenshot of the Dashboard item on the Azure portal menu.

  2. 在仪表板页面上,选择“简单的 VM 仪表板”。

    Screenshot of the dashboard selection option in the Azure portal.

  3. 查看仪表板,该仪表板应类似于此处所示的仪表板。 虽然某些内容是静态的,但也有一些图表显示了你在一开始创建的 VM 的性能。

    Screenshot of an example dashboard in the Azure portal.

清理资源

若要删除 VM 和关联仪表板,请删除包含它们的资源组。

注意

删除资源组会删除其包含的所有资源。 如果资源组包含虚拟机和仪表板之外的其他资源,则这些资源也将删除。

Remove-AzResourceGroup -Name $resourceGroupName
Remove-Item -Path "$HOME\portal-dashboard-template-testvm.json"

后续步骤

有关 Az.Portal PowerShell 模块中包含的 cmdlet 的详细信息,请参阅: