快速入門:使用 PowerShell 建立 Azure 入口網站 儀錶板
Azure 入口網站 中的儀錶板是雲端資源的專注且有組織的檢視。 本文著重於使用 Az.Portal PowerShell 模組 建立儀錶板的程式。 儀錶板會顯示您建立的虛擬機 (VM) 效能,以及一些靜態資訊和連結。
Azure 入口網站 中的儀錶板是雲端資源的專注且有組織的檢視。 本快速入門說明如何使用 Az.Portal PowerShell 來建立儀錶板。 範例儀錶板會顯示虛擬機 (VM) 的效能,以及一些靜態資訊和連結。
必要條件
具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶。
如果您選擇在本機使用PowerShell,本文會要求您安裝 Az PowerShell 模組,並使用 連線-AzAccount Cmdlet 連線到 Azure 帳戶。 如需安裝 Az PowerShell 模組的詳細資訊,請參閱 安裝 Azure PowerShell。
Azure Cloud Shell
Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。
要啟動 Azure Cloud Shell:
選項 | 範例/連結 |
---|---|
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 | |
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 | |
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 |
若要使用 Azure Cloud Shell:
啟動 Cloud Shell。
選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。
透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。
選取 Enter 鍵執行程式碼或命令。
選擇特定的 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 = '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'
建立資源群組
使用 New-AzResourceGroup Cmdlet 建立 Azure 資源群組。 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 入口網站儀錶板範本參考。
部署儀錶板範本
您可以使用 New-AzPortalDashboard
Az.Portal 模組的 Cmdlet,直接從 PowerShell 部署範本。
$DashboardParams = @{
DashboardPath = $myPortalDashboardTemplatePath
ResourceGroupName = $resourceGroupName
DashboardName = $dashboardName
}
New-AzPortalDashboard @DashboardParams
檢閱已部署的資源
檢查儀錶板是否已成功建立。
Get-AzPortalDashboard -Name $dashboardName -ResourceGroupName $resourceGroupName
確認您可以在 Azure 入口網站 儀錶板中看到虛擬機的相關數據。
在 Azure 入口網站功能表中,選取 [儀表板]。
在儀錶板頁面上,選取 [ 簡單 VM 儀錶板]。
檢閱儀錶板,其看起來應該類似此處所示的儀錶板。 雖然某些內容是靜態的,但也有圖表顯示您一開始建立的 VM 效能。
清除資源
若要移除 VM 和相關聯的儀錶板,請刪除包含它們的資源群組。
警告
刪除資源群組將會刪除其內含的所有資源。 如果資源群組包含虛擬機和儀錶板以外的其他資源,也會刪除這些資源。
Remove-AzResourceGroup -Name $resourceGroupName
Remove-Item -Path "$HOME\portal-dashboard-template-testvm.json"
下一步
如需 Az.Portal PowerShell 模組中包含的 Cmdlet 詳細資訊,請參閱: