你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 PowerShell 启用 VM 见解
本文介绍如何使用 PowerShell 在 Azure 虚拟机上启用 VM 见解。 此过程可用于:
- Azure 虚拟机
- Azure 虚拟机规模集
此脚本为 Log Analytics/Azure Monitoring Agent (AMA) 安装 VM 扩展,并在必要时安装 Dependency Agent 以启用 VM Insights。 如果已加入 AMA,则数据收集规则 (DCR) 和用户分配的托管标识 (UAMI) 也与虚拟机和虚拟机规模集相关联。
重要
旧版 Log Analytics 代理将于 2024 年 8 月弃用。 在此日期之后,Microsoft 将不再为 Log Analytics 代理提供任何支持。 请在 2024 年 8 月之前迁移到 Azure Monitor 代理,这样才能继续引入数据。
注意
从版本 1.10.1 开始支持 Azure Monitor 代理。
先决条件
你需要:
- 有关与 Azure Monitor 代理相关的先决条件,请参阅管理 Azure Monitor 代理。
- 请参阅支持的操作系统,以确保正在启用的虚拟机或虚拟机规模集的操作系统是受支持的。
- 若要为 Azure Monitor 代理启用网络隔离,请参阅使用专用链接为 Azure Monitor 代理启用网络隔离。
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 Monitoring Agent 和 Dependency Agent 或 Log Analytics Agent 启用 VM 见解。
使用 Azure Monitor 代理启用 VM 见解时,脚本会将数据收集规则 (DCR) 和用户分配的托管标识 (UAMI) 关联到 VM/虚拟机规模集。 UAMI 设置会传递到 Azure Monitor 代理扩展。
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 订阅 ID。-DcrResourceId <String>
数据收集规则 (DCR) Azure 资源 ID 标识符。 可以将来自不同订阅的 DCR 指定给启用了 Vm-Insights 的 VM 或虚拟机规模集。-UserAssignedManagedIdentityResourceGroup <String>
用户分配的托管标识 (UAMI) 资源组的名称。-UserAssignedManagedIdentityName <String>
用户分配的托管标识 (UAMI) 的名称。
可选参数:
-ProcessAndDependencies
将此标志设置为使用 Azure Monitoring Agent (AMA) 设置加入 Dependency Agent。 如果未指定,则仅加入 Azure Monitoring Agent (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
后续步骤
- 请参阅使用 VM 见解地图,查看已发现的应用程序依赖项。
- 请参阅查看 Azure VM 性能,了解瓶颈、整体利用率和 VM 的性能。