Редагувати

Поділитися через


Quickstart: Create an Azure portal dashboard with PowerShell

A dashboard in the Azure portal is a focused and organized view of your cloud resources. This article focuses on the process of using the Az.Portal PowerShell module to create a dashboard. The example dashboard shows the performance of a virtual machine (VM) that you create, along with some static information and links.

Prerequisites

  • An Azure account with an active subscription. Create an account for free.

  • 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. For more information about installing the Az PowerShell module, see Install Azure PowerShell.

Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code or command block. Selecting Try It doesn't automatically copy the code or command to Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Button to launch Azure Cloud Shell.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Screenshot that shows the Cloud Shell button in the Azure portal

To use Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block (or command block) to copy the code or command.

  3. Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code or command.

Choose a specific Azure subscription

If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources should be billed. Select a specific subscription using the Set-AzContext cmdlet.

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

Define variables

The example dashboard uses several pieces of information repeatedly. Create variables to store this information.

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

# Azure region
$location = 'centralus'

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

Create a resource group

Create an Azure resource group using the New-AzResourceGroup cmdlet. A resource group is a logical container in which Azure resources are deployed and managed as a group.

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

The example dashboard requires an existing VM. Create a VM by following these steps.

Store login credentials for the VM in a variable. The password must be complex. This is a new username and password (not the account you use to sign in to Azure). For more information, see username requirements and password requirements.

$Cred = Get-Credential

Create the VM.

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

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

Since Azure dashboards are resources, they can be represented as JSON. The following code downloads a JSON representation of a sample dashboard. For more information, see The structure of Azure Dashboards.

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

$myPortalDashboardTemplatePath = "$HOME\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

For more information about the dashboard template structure, see Microsoft portal dashboards template reference.

Deploy the dashboard template

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

Verify that you can see data about your virtual machine in the Azure portal dashboard.

  1. In the Azure portal menu, select Dashboard.

    Screenshot of the Dashboard item on the Azure portal menu.

  2. On the dashboard page, select Simple VM Dashboard.

    Screenshot of the dashboard selection option in the Azure portal.

  3. Review the dashboard, which should look similar to the one shown here. While some of the content is static, there are also charts that show the performance of the VM you created at the beginning.

    Screenshot of an example dashboard in the Azure portal.

Clean up resources

To remove the VM and associated dashboard, delete the resource group that contains them.

Caution

Deleting the resource group will delete all of the resources contained within it. If the resource group contains additional resources aside from your virtual machine and dashboard, those resources will also be deleted.

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

Next steps

For more information about the cmdlets contained in the Az.Portal PowerShell module, see: