以编程方式创建 Azure 仪表板Programmatically create Azure Dashboards

本文介绍如何以编程方式创建和发布 Azure 仪表板。This article walks you through the process of programmatically creating and publishing Azure dashboards. 文档中将通篇引用以下仪表板。The dashboard shown below is referenced throughout the document.

示例仪表板

概述Overview

Azure 门户中的共享仪表板与虚拟机和存储帐户一样,是一种资源Shared dashboards in the Azure portal are resources just like virtual machines and storage accounts. 可以使用 Azure 资源管理器 REST APIAzure CLIAzure PowerShell 命令以编程方式管理这些资源。You can manage resources programmatically by using the Azure Resource Manager REST APIs, the Azure CLI, and Azure PowerShell commands.

许多功能都建立这些 API 的基础之上,可以简化资源的管理。Many features build on these APIs to make resource management easier. 所有这些 API 和工具都提供了创建、罗列、检索、修改和删除资源的方法。Each of these APIs and tools offers ways to create, list, retrieve, modify, and delete resources. 由于仪表板是资源,因此可以选择使用你偏好的 API 或工具。Since dashboards are resources, you can pick your favorite API or tool to use.

无论使用哪种工具,若要以编程方式创建仪表板,都请构造仪表板对象的 JSON 表示形式。Whichever tools you use, to create a dashboard programmatically, you construct a JSON representation of your dashboard object. 此对象包含有关仪表板上的磁贴的信息。This object contains information about the tiles on the dashboard. 这包括大小、位置、绑定到的资源和任何用户自定义项。It includes sizes, positions, resources they're bound to, and any user customizations.

构建此 JSON 文档的最实用方法是使用 Azure 门户。The most practical way to build up this JSON document is to use the Azure portal. 可以交互方式添加并放置磁贴。You can interactively add and position your tiles. 然后导出 JSON,并从结果创建模板,以供后面在脚本、程序和部署工具中使用。Then export the JSON and create a template from the result for later use in scripts, programs, and deployment tools.

创建仪表板Create a dashboard

要创建仪表板,请从 Azure 门户菜单中选择“仪表板”,然后选择“新建仪表板”。 To create a dashboard, select Dashboard from the Azure portal menu, then select New dashboard.

“新建仪表板”命令

使用磁贴库来查找和添加磁贴。Use the tile gallery to find and add tiles. 通过拖放磁贴进行添加。Tiles are added by dragging and dropping them. 某些磁贴支持使用拖拽控点来调整大小。Some tiles support resizing by using a drag handle.

用于更改大小的拖拽控点

其他磁贴具有固定的大小,可从其上下文菜单中选择大小。Others have fixed sizes to choose from in their context menu.

用于更改大小的大小上下文菜单

共享仪表板Share the dashboard

配置仪表板后,下一步是使用“共享”命令发布仪表板。 After you configure the dashboard, the next step is to publish the dashboard using the Share command.

共享仪表板

选择“共享”时,系统会提示选择要发布到的订阅和资源组。 Selecting Share prompts you to choose which subscription and resource group to publish to. 必须对所选订阅和资源组拥有写入访问权限。You must have write access to the subscription and resource group that you choose. 有关详细信息,请参阅使用 Azure 门户分配 Azure 角色For more information, see Assign Azure roles using the Azure portal.

对共享和访问权限进行更改

提取仪表板的 JSON 表示形式Fetch the JSON representation of the dashboard

仅需要几秒钟即可完成发布。Publishing only takes a few seconds. 完成后,下一步是使用“下载” 命令获取 JSON。When it's done, the next step is to fetch the JSON using the Download command.

下载 JSON 表示形式

从 JSON 创建模板Create a template from the JSON

下一步是从此 JSON 创建模板。The next step is to create a template from this JSON. 通过相应的资源管理 API、命令行工具或者在门户中以编程方式使用该模板。Use that template programmatically with the appropriate resource management APIs, command-line tools, or within the portal.

不必要全面了解仪表板 JSON 结构即可创建模板。You don't have to fully understand the dashboard JSON structure to create a template. 大多数情况下,只需保留每个磁贴的结构和配置。In most cases, you want to preserve the structure and configuration of each tile. 然后参数化磁贴指向的 Azure 资源集。Then parameterize the set of Azure resources that the tiles point to. 查看导出的 JSON 仪表板,并找到 Azure 资源 ID 的所有匹配项。Look at your exported JSON dashboard and find all occurrences of Azure resource IDs. 示例仪表板具有多个磁贴,它们都指向单个 Azure 虚拟机。Our example dashboard has multiple tiles that all point at a single Azure virtual machine. 这是因为该仪表板仅在此单个资源中查找。That's because our dashboard only looks at this single resource. 如果在(文档末尾包含的)示例 JSON 中搜索“/subscriptions”,将找到此 ID 的多个匹配项。If you search the sample JSON, included at the end of the document, for "/subscriptions", you find several occurrences of this ID.

/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1

将来若要针对任何虚拟机发布此仪表板,请参数化 JSON 中此字符串的每个匹配项。To publish this dashboard for any virtual machine in the future, parameterize every occurrence of this string within the JSON.

在 Azure 中创建资源的 API 采用两种方法:There are two approaches for APIs that create resources in Azure:

基于模板的部署支持参数化和模板化。Template-based deployment supports parameterization and templating. 本文将使用此方法。We use this approach in this article.

使用模板部署以编程方式从模板创建仪表板Programmatically create a dashboard from your template using a template deployment

Azure 提供协调多资源部署的功能。Azure offers the ability to orchestrate the deployment of multiple resources. 创建用于表达要部署的资源集的部署模板及资源之间的关系。You create a deployment template that expresses the set of resources to deploy and the relationships between them. 每个资源的 JSON 格式与逐个创建资源时的格式相同。The JSON format of each resource is the same as if you were creating them one by one. 差别在于模板语言会添加一些概念,例如变量、参数、基本功能等。The difference is that the template language adds a few concepts like variables, parameters, basic functions, and more. 此扩展语法仅在模板部署上下文中受支持。This extended syntax is only supported in the context of a template deployment. 如果与前述命令式 API 一起使用,则不起作用。It doesn't work if used with the imperative APIs discussed earlier. 有关详细信息,请参阅了解 Azure 资源管理器模板的结构和语法For more information, see Understand the structure and syntax of Azure Resource Manager templates.

应使用模板的参数语法来实现参数化。Parameterization should be done using the template's parameter syntax. 替换之前找到的所有资源 ID 的实例,如下所示。You replace all instances of the resource ID we found earlier as shown here.

具有硬编码的资源 ID 的示例 JSON 属性Example JSON property with hard-coded resource ID:

id: "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"

基于模板参数转换为参数化版本的示例 JSON 属性Example JSON property converted to a parameterized version based on template parameters

id: "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"

在 JSON 模板顶部声明所需的模板元数据和参数,如下所示:Declare required template metadata and the parameters at the top of the JSON template like this:


{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "virtualMachineName": {
            "type": "string"
        },
        "virtualMachineResourceGroup": {
            "type": "string"
        },
        "dashboardName": {
            "type": "string"
        }
    },
    "variables": {},

    ... rest of template omitted ...

配置模板后,使用以下任意方法部署模板:Once you've configured your template, deploy it using any of the following methods:

接下来会看到示例仪表板 JSON 的两个版本。Next you'll see two versions of our example dashboard JSON. 第一个版本是从门户导出的、已绑定到资源的模板。The first is the version that we exported from the portal that was already bound to a resource. 第二个是可以编程方式绑定到任何虚拟机并使用 Azure 资源管理器进行部署的模板版本。The second is the template version that can be programmatically bound to any virtual machine and deployed using Azure Resource Manager.

模板化之前示例仪表板的 JSON 表示形式JSON representation of our example dashboard before templating

此示例显示了在遵循本文操作的情况下预期可以看到的内容。This example shows what you can expect to see if you followed along with this article. 指令导出了已部署的仪表板的 JSON 表示形式。The instructions exported the JSON representation of a dashboard that is already deployed. 硬编码资源标识符显示此仪表板指向特定的 Azure 虚拟机。The hard-coded resource identifiers show that this dashboard is pointing at a specific Azure virtual machine.


{
    "properties": {
        "lenses": {
            "0": {
                "order": 0,
                "parts": {
                    "0": {
                        "position": {
                            "x": 0,
                            "y": 0,
                            "rowSpan": 2,
                            "colSpan": 3
                        },
                        "metadata": {
                            "inputs": [],
                            "type": "Extension[azure]/HubsExtension/PartType/MarkdownPart",
                            "settings": {
                                "content": {
                                    "settings": {
                                        "content": "## Azure Virtual Machines Overview\r\nNew team members should watch this video to get familiar with Azure Virtual Machines.",
                                        "title": "",
                                        "subtitle": ""
                                    }
                                }
                            }
                        }
                    },
                    "1": {
                        "position": {
                            "x": 3,
                            "y": 0,
                            "rowSpan": 4,
                            "colSpan": 8
                        },
                        "metadata": {
                            "inputs": [],
                            "type": "Extension[azure]/HubsExtension/PartType/MarkdownPart",
                            "settings": {
                                "content": {
                                    "settings": {
                                        "content": "This is the team dashboard for the test VM we use on our team. Here are some useful links:\r\n\r\n1. [Getting started](https://www.contoso.com/tsgs)\r\n1. [Troubleshooting guide](https://www.contoso.com/tsgs)\r\n1. [Architecture docs](https://www.contoso.com/tsgs)",
                                        "title": "Test VM Dashboard",
                                        "subtitle": "Contoso"
                                    }
                                }
                            }
                        }
                    },
                    "2": {
                        "position": {
                            "x": 0,
                            "y": 2,
                            "rowSpan": 2,
                            "colSpan": 3
                        },
                        "metadata": {
                            "inputs": [],
                            "type": "Extension[azure]/HubsExtension/PartType/VideoPart",
                            "settings": {
                                "content": {
                                    "settings": {
                                        "title": "",
                                        "subtitle": "",
                                        "src": "<https://www.contoso.com/yourvideo>",
                                        "autoplay": false
                                    }
                                }
                            }
                        }
                    },
                    "3": {
                        "position": {
                            "x": 0,
                            "y": 4,
                            "rowSpan": 3,
                            "colSpan": 11
                        },
                        "metadata": {
                            "inputs": [
                                {
                                    "name": "queryInputs",
                                    "value": {
                                        "timespan": {
                                            "duration": "PT1H",
                                            "start": null,
                                            "end": null
                                        },
                                        "id": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1",
                                        "chartType": 0,
                                        "metrics": [
                                            {
                                                "name": "Percentage CPU",
                                                "resourceId": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                            }
                                        ]
                                    }
                                }
                            ],
                            "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                            "settings": {}
                        }
                    },
                    "4": {
                        "position": {
                            "x": 0,
                            "y": 7,
                            "rowSpan": 2,
                            "colSpan": 3
                        },
                        "metadata": {
                            "inputs": [
                                {
                                    "name": "queryInputs",
                                    "value": {
                                        "timespan": {
                                            "duration": "PT1H",
                                            "start": null,
                                            "end": null
                                        },
                                        "id": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1",
                                        "chartType": 0,
                                        "metrics": [
                                            {
                                                "name": "Disk Read Operations/Sec",
                                                "resourceId": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                            },
                                            {
                                                "name": "Disk Write Operations/Sec",
                                                "resourceId": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                            }
                                        ]
                                    }
                                }
                            ],
                            "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                            "settings": {}
                        }
                    },
                    "5": {
                        "position": {
                            "x": 3,
                            "y": 7,
                            "rowSpan": 2,
                            "colSpan": 3
                        },
                        "metadata": {
                            "inputs": [
                                {
                                    "name": "queryInputs",
                                    "value": {
                                        "timespan": {
                                            "duration": "PT1H",
                                            "start": null,
                                            "end": null
                                        },
                                        "id": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1",
                                        "chartType": 0,
                                        "metrics": [
                                            {
                                                "name": "Disk Read Bytes",
                                                "resourceId": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                            },
                                            {
                                                "name": "Disk Write Bytes",
                                                "resourceId": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                            }
                                        ]
                                    }
                                }
                            ],
                            "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                            "settings": {}
                        }
                    },
                    "6": {
                        "position": {
                            "x": 6,
                            "y": 7,
                            "rowSpan": 2,
                            "colSpan": 3
                        },
                        "metadata": {
                            "inputs": [
                                {
                                    "name": "queryInputs",
                                    "value": {
                                        "timespan": {
                                            "duration": "PT1H",
                                            "start": null,
                                            "end": null
                                        },
                                        "id": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1",
                                        "chartType": 0,
                                        "metrics": [
                                            {
                                                "name": "Network In",
                                                "resourceId": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                            },
                                            {
                                                "name": "Network Out",
                                                "resourceId": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                            }
                                        ]
                                    }
                                }
                            ],
                            "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                            "settings": {}
                        }
                    },
                    "7": {
                        "position": {
                            "x": 9,
                            "y": 7,
                            "rowSpan": 2,
                            "colSpan": 2
                        },
                        "metadata": {
                            "inputs": [
                                {
                                    "name": "id",
                                    "value": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
                                }
                            ],
                            "type": "Extension/Microsoft_Azure_Compute/PartType/VirtualMachinePart",
                            "asset": {
                                "idInputName": "id",
                                "type": "VirtualMachine"
                            },
                            "defaultMenuItemId": "overview"
                        }
                    }
                }
            }
        },
        "metadata": { }
    },
    "id": "/subscriptions/6531c8c8-df32-4254-d717-b6e983273e5d/resourceGroups/dashboards/providers/Microsoft.Portal/dashboards/aa9786ae-e159-483f-b05f-1f7f767741a9",
    "name": "aa9786ae-e159-483f-b05f-1f7f767741a9",
    "type": "Microsoft.Portal/dashboards",
    "location": "chinaeast2",
    "tags": {
        "hidden-title": "Created via API"
    }
}

示例仪表板的模板表示形式Template representation of our example dashboard

仪表板的模板版本定义了名为 virtualMachineNamevirtualMachineResourceGroupdashboardName 的三个参数。The template version of the dashboard has defined three parameters called virtualMachineName, virtualMachineResourceGroup, and dashboardName. 使用这些参数可在每次部署时将此仪表板指向不同的 Azure 虚拟机。The parameters let you point this dashboard at a different Azure virtual machine every time you deploy. 此仪表板可以编程方式进行配置并部署为指向任何 Azure 虚拟机。This dashboard can be programmatically configured and deployed to point to any Azure virtual machine. 若要测试此功能,请复制以下模板并将其粘贴到 Azure 门户的模板部署页To test this feature, copy the following template and paste it into the Azure portal template deployment page.

此示例自行部署了一个仪表板,但使用模板语言可部署多个资源并将其与一个或多个仪表板绑定。This example deploys a dashboard by itself, but the template language lets you deploy multiple resources, and bundle one or more dashboards along side them.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "virtualMachineName": {
            "type": "string"
        },
        "virtualMachineResourceGroup": {
            "type": "string"
        },
        "dashboardName": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [
        {
            "properties": {
                "lenses": {
                    "0": {
                        "order": 0,
                        "parts": {
                            "0": {
                                "position": {
                                    "x": 0,
                                    "y": 0,
                                    "rowSpan": 2,
                                    "colSpan": 3
                                },
                                "metadata": {
                                    "inputs": [],
                                    "type": "Extension[azure]/HubsExtension/PartType/MarkdownPart",
                                    "settings": {
                                        "content": {
                                            "settings": {
                                                "content": "## Azure Virtual Machines Overview\r\nNew team members should watch this video to get familiar with Azure Virtual Machines.",
                                                "title": "",
                                                "subtitle": ""
                                            }
                                        }
                                    }
                                }
                            },
                            "1": {
                                "position": {
                                    "x": 3,
                                    "y": 0,
                                    "rowSpan": 4,
                                    "colSpan": 8
                                },
                                "metadata": {
                                    "inputs": [],
                                    "type": "Extension[azure]/HubsExtension/PartType/MarkdownPart",
                                    "settings": {
                                        "content": {
                                            "settings": {
                                                "content": "This is the team dashboard for the test VM we use on our team. Here are some useful links:\r\n\r\n1. [Getting started](https://www.contoso.com/tsgs)\r\n1. [Troubleshooting guide](https://www.contoso.com/tsgs)\r\n1. [Architecture docs](https://www.contoso.com/tsgs)",
                                                "title": "Test VM Dashboard",
                                                "subtitle": "Contoso"
                                            }
                                        }
                                    }
                                }
                            },
                            "2": {
                                "position": {
                                    "x": 0,
                                    "y": 2,
                                    "rowSpan": 2,
                                    "colSpan": 3
                                },
                                "metadata": {
                                    "inputs": [],
                                    "type": "Extension[azure]/HubsExtension/PartType/VideoPart",
                                    "settings": {
                                        "content": {
                                            "settings": {
                                                "title": "",
                                                "subtitle": "",
                                                "src": "<https://www.contoso.com/yourvideo>",
                                                "autoplay": false
                                            }
                                        }
                                    }
                                }
                            },
                            "3": {
                                "position": {
                                    "x": 0,
                                    "y": 4,
                                    "rowSpan": 3,
                                    "colSpan": 11
                                },
                                "metadata": {
                                    "inputs": [
                                        {
                                            "name": "queryInputs",
                                            "value": {
                                                "timespan": {
                                                    "duration": "PT1H",
                                                    "start": null,
                                                    "end": null
                                                },
                                                "id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
                                                "chartType": 0,
                                                "metrics": [
                                                    {
                                                        "name": "Percentage CPU",
                                                        "resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                                    }
                                                ]
                                            }
                                        }
                                    ],
                                    "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                                    "settings": {}
                                }
                            },
                            "4": {
                                "position": {
                                    "x": 0,
                                    "y": 7,
                                    "rowSpan": 2,
                                    "colSpan": 3
                                },
                                "metadata": {
                                    "inputs": [
                                        {
                                            "name": "queryInputs",
                                            "value": {
                                                "timespan": {
                                                    "duration": "PT1H",
                                                    "start": null,
                                                    "end": null
                                                },
                                                "id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
                                                "chartType": 0,
                                                "metrics": [
                                                    {
                                                        "name": "Disk Read Operations/Sec",
                                                        "resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                                    },
                                                    {
                                                        "name": "Disk Write Operations/Sec",
                                                        "resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                                    }
                                                ]
                                            }
                                        }
                                    ],
                                    "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                                    "settings": {}
                                }
                            },
                            "5": {
                                "position": {
                                    "x": 3,
                                    "y": 7,
                                    "rowSpan": 2,
                                    "colSpan": 3
                                },
                                "metadata": {
                                    "inputs": [
                                        {
                                            "name": "queryInputs",
                                            "value": {
                                                "timespan": {
                                                    "duration": "PT1H",
                                                    "start": null,
                                                    "end": null
                                                },
                                                "id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
                                                "chartType": 0,
                                                "metrics": [
                                                    {
                                                        "name": "Disk Read Bytes",
                                                        "resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                                    },
                                                    {
                                                        "name": "Disk Write Bytes",
                                                        "resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                                    }
                                                ]
                                            }
                                        }
                                    ],
                                    "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                                    "settings": {}
                                }
                            },
                            "6": {
                                "position": {
                                    "x": 6,
                                    "y": 7,
                                    "rowSpan": 2,
                                    "colSpan": 3
                                },
                                "metadata": {
                                    "inputs": [
                                        {
                                            "name": "queryInputs",
                                            "value": {
                                                "timespan": {
                                                    "duration": "PT1H",
                                                    "start": null,
                                                    "end": null
                                                },
                                                "id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
                                                "chartType": 0,
                                                "metrics": [
                                                    {
                                                        "name": "Network In",
                                                        "resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                                    },
                                                    {
                                                        "name": "Network Out",
                                                        "resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                                    }
                                                ]
                                            }
                                        }
                                    ],
                                    "type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart",
                                    "settings": {}
                                }
                            },
                            "7": {
                                "position": {
                                    "x": 9,
                                    "y": 7,
                                    "rowSpan": 2,
                                    "colSpan": 2
                                },
                                "metadata": {
                                    "inputs": [
                                        {
                                            "name": "id",
                                            "value": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
                                        }
                                    ],
                                    "type": "Extension/Microsoft_Azure_Compute/PartType/VirtualMachinePart",
                                    "asset": {
                                        "idInputName": "id",
                                        "type": "VirtualMachine"
                                    },
                                    "defaultMenuItemId": "overview"
                                }
                            }
                        }
                    }
                }
            },
            "metadata": { },
            "apiVersion": "2015-08-01-preview",
            "type": "Microsoft.Portal/dashboards",
            "name": "[parameters('dashboardName')]",
            "location": "chinaeast",
            "tags": {
                "hidden-title": "[parameters('dashboardName')]"
            }
        }
    ]
}

通过示例了解如何使用参数化模板来部署仪表板后,就可以尝试使用 Azure 资源管理器 REST APIAzure CLIAzure PowerShell 命令来部署模板。Now that you've seen an example of using a parameterized template to deploy a dashboard, you can try deploying the template by using the Azure Resource Manager REST APIs, the Azure CLI, or Azure PowerShell commands.

使用 Azure CLI 以编程方式创建仪表板Programmatically create a dashboard by using Azure CLI

为 Azure CLI 准备环境。Prepare your environment for the Azure CLI.

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

运行 az portal dashboard create 命令来创建仪表板:Run the az portal dashboard create command to create a dashboard:

az portal dashboard create --resource-group myResourceGroup --name 'Simple VM Dashboard' \
   --input-path portal-dashboard-template-testvm.json --location chinaeast2

可以使用 az portal dashboard update 命令更新仪表板:You can update a dashboard by using the az portal dashboard update command:

az portal dashboard update --resource-group myResourceGroup --name 'Simple VM Dashboard' \
--input-path portal-dashboard-template-testvm.json --location chinaeast2

通过运行 az portal dashboard show 命令查看仪表板的详细信息:See the details of a dashboard by running the az portal dashboard show command:

az portal dashboard show --resource-group myResourceGroup --name 'Simple VM Dashboard'

若要查看当前订阅的所有仪表板,请使用 az portal dashboard listTo see all the dashboards for the current subscription, use az portal dashboard list:

az portal dashboard list

还可以查看资源组的所有仪表板:You can also see all the dashboards for a resource group:

az portal dashboard list --resource-group myResourceGroup

后续步骤Next steps

有关桌面的详细信息,请参阅管理 Azure 门户设置和首选项For more information about desktops, see Manage Azure portal settings and preferences.

有关对仪表板的 Azure CLI 支持的详细信息,请参阅 az portal dashboardFor more information about Azure CLI support for dashboards, see az portal dashboard.