使用 PowerShell 啟用 VM 深入解析

本文說明如何使用 PowerShell 在 Azure 虛擬機器上啟用 VM 深入解析。 此程序可用於:

  • Azure 虛擬機器
  • Azure 虛擬機器擴展集

此指令碼會安裝 Log Analytics/Azure 監視代理程式 (AMA) 的 VM 擴充功能,並視需要安裝 Dependency Agent 以啟用 VM Insights。 如果 AMA 上線,資料收集規則 (DCR) 和使用者指派的受控識別 (UAMI) 也會與虛擬機器和虛擬機器擴展集相關聯。

重要

舊版 Log Analytics 代理程式將於 2024 年 8 月被取代。 在此日期之後,Microsoft 將不再提供 Log Analytics 代理程式的任何支援。 在 2024 年 8 月之前移轉至 Azure 監視器代理程式,才能繼續擷取資料。

注意

1.10.1 版開始支援 Azure 監視器代理程式。

必要條件

您需要:

PowerShell 指令碼

若要為多個 VM 或虛擬機器擴展集啟用 VM 深入解析,請使用 PowerShell 指令碼 Install-VMInsights.ps1。 Azure PowerShell 資源庫中提供指令碼。 此指令碼會根據您指定的參數逐一查看虛擬機器或虛擬機器擴展集。 指令碼可用來啟用下列項目的 VM 深入解析:

  • 您訂用帳戶中的每個虛擬機器和虛擬機器擴展集。
  • -ResourceGroup 所指定的範圍資源群組。
  • -Name 指定的 VM 或虛擬機器擴展集。 您可以使用萬用字元來指定多個資源群組、VM 或擴展集。

確認您使用的是已啟用 Enable-AzureRM 相容性別名的 Az PowerShell 模組 1.0.0 版或更新版本。 執行 Get-Module -ListAvailable Az 以尋找版本。 若要升級,請參閱安裝 Azure PowerShell 模組。 如果您在本機執行 PowerShell,請執行 Connect-AzAccount 以建立與 Azure 的連線。

若要取得指令碼引數詳細資料及使用方式範例的清單,請執行 Get-Help

Get-Help Install-VMInsights.ps1 -Detailed

使用指令碼,利用 Azure 監視代理程式和 Dependency Agent 或 Log Analytics 代理程式來啟用 VM 深入解析。

當您使用 Azure 監視器代理程式啟用 VM 深入解析時,指令碼會將資料收集規則 (DCR) 和使用者指派的受控識別 (UAMI) 關聯至 VM/虛擬機器擴展集。 UAMI 設定會傳遞至 Azure 監視器代理程式擴充功能。

Install-VMInsights.ps1 -SubscriptionId <SubscriptionId> `
[-ResourceGroup <ResourceGroup>] `
[-ProcessAndDependencies ] `
[-Name <VM or Virtual Machine Scale Set name>] `
-DcrResourceId <DataCollectionRuleResourceId> `
-UserAssignedManagedIdentityName <UserAssignedIdentityName> `
-UserAssignedManagedIdentityResourceGroup <UserAssignedIdentityResourceGroup> 

必要引數:

  • -SubscriptionId <String> Azure 訂用帳戶識別碼。
  • -DcrResourceId <String> 資料收集規則 (DCR) Azure 資源識別碼。 您可以使用 Vm-Insights 來指定來自不同 VM 或虛擬機器擴展集不同訂用帳戶的 DCR。
  • -UserAssignedManagedIdentityResourceGroup <String> 使用者指派的受控識別 (UAMI) 資源群組的名稱。
  • -UserAssignedManagedIdentityName <String> 使用者指派的受控識別 (UAMI) 名稱。

選擇性引數:

  • -ProcessAndDependencies 將此旗標設定為使用 Azure 監視代理程式 (AMA) 設定將 Dependency Agent 上線。 如果未指定,則只會將 Azure 監視代理程式 (AMA) 上線。
  • -Name <String> 要上線的 VM 或虛擬機器擴展集名稱。 如果未指定,則訂用帳戶或資源群組中的所有 VM 和虛擬機器擴展集都會上線。 使用萬用字元來指定多個 VM 或虛擬機器擴展集。
  • -ResourceGroup <String> 包含所要上線 VM 或虛擬機擴展集的資源群組名稱。 如果未指定,則訂用帳戶中的所有 VM 和虛擬機器擴展集都會上線。 使用萬用字元來指定多個資源群組。
  • -PolicyAssignmentName <String> 只包含與此原則相關聯的 VM。 指定 PolicyAssignmentName 參數時,會考慮參數 SubscriptionId 的 VM 部分。
  • -TriggerVmssManualVMUpdate [<SwitchParameter>] 觸發擴展集中 VM 執行個體的更新,其升級原則設定為 [手動]。
  • -WhatIf [<SwitchParameter>] 取得指令碼中命令預期效果的相關資訊。
  • -Confirm [<SwitchParameter>] 確認指令碼中的每個動作。
  • -Approve [<SwitchParameter>] 提供安裝核准,以在沒有確認提示列出 VM/虛擬機器擴展集的情況下啟動。

指令碼支援 -Name-ResourceGroup 的萬用字元。 例如,-Name vm* 會針對開頭為 "vm" 的所有 VM 和虛擬機器擴展集啟用 VM 深入解析。 如需詳細資訊,請參閱 Windows PowerShell 中的萬用字元

範例:

Install-VMInsights.ps1 -SubscriptionId 12345678-abcd-abcd-1234-12345678 `
-ResourceGroup rg-AMAPowershell  `
-ProcessAndDependencies  `
-Name vmAMAPowershellWindows `
-DcrResourceId /subscriptions/12345678-abcd-abcd-1234-12345678/resourceGroups/rg-AMAPowershell/providers/Microsoft.Insights/dataCollectionRules/MSVMI-ama-vmi-default-dcr `
-UserAssignedManagedIdentityName miamatest1  `
-UserAssignedManagedIdentityResourceGroup amapowershell

輸出具備下列格式:

Name                                     Account                               SubscriptionName                      Environment                          TenantId
----                                     -------                               ----------------                      -----------                          --------
AzMon001 12345678-abcd-123…              MSI@9876                              AzMon001                              AzureCloud                           abcd1234-9876-abcd-1234-1234abcd5648

Getting list of VMs or VM Scale Sets matching specified criteria.
VMs and Virtual Machine Scale Sets matching selection criteria :

ResourceGroup : rg-AMAPowershell
  vmAMAPowershellWindows


Confirm
Continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): 

(rg-AMAPowershell) : Assigning roles

(rg-AMAPowershell) vmAMAPowershellWindows : Assigning User Assigned Managed Identity edsMIAMATest
(rg-AMAPowershell) vmAMAPowershellWindows : Successfully assigned User Assigned Managed Identity edsMIAMATest
(rg-AMAPowershell) vmAMAPowershellWindows : Data Collection Rule Id /subscriptions/12345678-abcd-abcd-1234-12345678/resourceGroups/rg-AMAPowershell/providers/Microsoft.Insights/dataCollectionRules/MSVMI-ama-vmi-default-dcr already associated with the VM.
(rg-AMAPowershell) vmAMAPowershellWindows : Extension AzureMonitorWindowsAgent, type = Microsoft.Azure.Monitor.AzureMonitorWindowsAgent already installed. Provisioning State : Succeeded
(rg-AMAPowershell) vmAMAPowershellWindows : Installing/Updating extension AzureMonitorWindowsAgent, type = Microsoft.Azure.Monitor.AzureMonitorWindowsAgent
(rg-AMAPowershell) vmAMAPowershellWindows : Successfully installed/updated extension AzureMonitorWindowsAgent, type = Microsoft.Azure.Monitor.AzureMonitorWindowsAgent
(rg-AMAPowershell) vmAMAPowershellWindows : Installing/Updating extension DA-Extension, type = Microsoft.Azure.Monitoring.DependencyAgent.DependencyAgentWindows
(rg-AMAPowershell) vmAMAPowershellWindows : Successfully installed/updated extension DA-Extension, type = Microsoft.Azure.Monitoring.DependencyAgent.DependencyAgentWindows
(rg-AMAPowershell) vmAMAPowershellWindows : Successfully onboarded VM insights

Summary :
Total VM/VMSS to be processed : 1
Succeeded : 1
Skipped : 0
Failed : 0
VMSS Instance Upgrade Failures : 0

檢查 Azure 入口網站中的 VM/虛擬機器擴展集,以查看擴充功能是否已安裝或是使用下列命令:


az vm extension list --resource-group <resource group> --vm-name <VM name>  -o table 


Name                      ProvisioningState    Publisher                                   Version    AutoUpgradeMinorVersion
------------------------  -------------------  ------------------------------------------  ---------  -------------------------
AzureMonitorWindowsAgent  Succeeded            Microsoft.Azure.Monitor                     1.16       True
DA-Extension              Succeeded            Microsoft.Azure.Monitoring.DependencyAgent  9.10       True

下一步