Application Insights 代理(以前称为状态监视器 v2):详细说明Application Insights Agent (formerly named Status Monitor v2): Detailed instructions

本文介绍如何载入到 PowerShell 库和下载 ApplicationMonitor 模块。This article describes how to onboard to the PowerShell Gallery and download the ApplicationMonitor module. 包含了开始使用所需的最常见参数。Included are the most common parameters that you'll need to get started. 我们还提供了手动下载说明,以防你无法访问 Internet。We've also provided manual download instructions in case you don't have internet access.

获取检测密钥Get an instrumentation key

若要开始,需要一个检测密钥。To get started, you need an instrumentation key. 有关详细信息,请参阅创建 Application Insights 资源For more information, see Create an Application Insights resource.

以管理员身份使用提升的执行策略运行 PowerShellRun PowerShell as Admin with an elevated execution policy

以管理员身份运行Run as Admin

PowerShell 需要拥有管理员级别的权限才能对计算机进行更改。PowerShell needs Administrator-level permissions to make changes to your computer.

执行策略Execution policy

  • 说明:默认禁用 PowerShell 脚本的运行。Description: By default, running PowerShell scripts is disabled. 我们建议仅允许在当前范围运行 RemoteSigned 脚本。We recommend allowing RemoteSigned scripts for only the Current scope.
  • 参考:关于执行策略Set-ExecutionPolicyReference: About Execution Policies and Set-ExecutionPolicy.
  • 命令:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope ProcessCommand: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process.
  • 可选参数:Optional parameter:
    • -Force.-Force. 跳过确认提示。Bypasses the confirmation prompt.

示例错误Example errors

Install-Module : The 'Install-Module' command was found in the module 'PowerShellGet', but the module could not be
loaded. For more information, run 'Import-Module PowerShellGet'.

Import-Module : File C:\Program Files\WindowsPowerShell\Modules\PackageManagement\1.3.1\PackageManagement.psm1 cannot
be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at

PowerShell 先决条件Prerequisites for PowerShell

运行 $PSVersionTable 命令,审核 PowerShell 的实例。Audit your instance of PowerShell by running the $PSVersionTable command. 该命令生成以下输出:This command produces the following output:

Name                           Value
----                           -----
PSVersion                      5.1.17763.316
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.316
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3

这些说明是在运行 Windows 10 的计算机上编写并测试的,使用了上面所列的版本。These instructions were written and tested on a computer running Windows 10 and the versions listed above.

这些步骤会准备你的服务器,以从 PowerShell 库下载模块。These steps will prepare your server to download modules from PowerShell Gallery.


Windows 10、Windows Server 2016 和 PowerShell 6+ 支持 PowerShell 库。PowerShell Gallery is supported on Windows 10, Windows Server 2016, and PowerShell 6+. 有关早期版本的信息,请参阅安装 PowerShellGetFor information about earlier versions, see Installing PowerShellGet.

  1. 以管理员身份使用提升的执行策略运行 PowerShell。Run PowerShell as Admin with an elevated execution policy.

  2. 安装 NuGet 包提供程序。Install the NuGet package provider.

    • 说明:需要使用此提供程序才能与基于 NuGet 的存储库(例如 PowerShell 库)交互。Description: You need this provider to interact with NuGet-based repositories like PowerShell Gallery.
    • 参考:Install-PackageProviderReference: Install-PackageProvider.
    • 命令:Install-PackageProvider -Name NuGet -MinimumVersion Install-PackageProvider -Name NuGet -MinimumVersion
    • 可选参数:Optional parameters:
      • -Proxy.-Proxy. 指定请求的代理服务器。Specifies a proxy server for the request.
      • -Force.-Force. 跳过确认提示。Bypasses the confirmation prompt.

    如果未设置 NuGet,则会看到此提示:You'll receive this prompt if NuGet isn't set up:

    NuGet provider is required to continue
    PowerShellGet requires NuGet provider version '' or newer to interact with NuGet-based repositories. 
    The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
    'C:\Users\t\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
    'Install-PackageProvider -Name NuGet -MinimumVersion -Force'. Do you want PowerShellGet to install and import
    the NuGet provider now?
    [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
  3. 将 PowerShell 库配置为受信任的存储库。Configure PowerShell Gallery as a trusted repository.

    • 说明:默认情况下,PowerShell 库是不受信任的存储库。Description: By default, PowerShell Gallery is an untrusted repository.
    • 参考:Set-PSRepositoryReference: Set-PSRepository.
    • 命令:Set-PSRepository -Name "PSGallery" -InstallationPolicy TrustedCommand: Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted.
    • 可选参数:Optional parameter:
      • -Proxy.-Proxy. 指定请求的代理服务器。Specifies a proxy server for the request.

    如果 PowerShell 库不受信任,则会看到此提示:You'll receive this prompt if PowerShell Gallery isn't trusted:

    Untrusted repository
    You are installing the modules from an untrusted repository. 
    If you trust this repository, change its InstallationPolicy value 
    by running the Set-PSRepository cmdlet. Are you sure you want to 
    install the modules from 'PSGallery'?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):

    可以运行 Get-PSRepository 命令,确认此更改并审核所有 PowerShell 存储库。You can confirm this change and audit all PSRepositories by running the Get-PSRepository command.

  4. 安装最新版本的 PowerShellGet。Install the newest version of PowerShellGet.

    • 说明:此模块包含用于从 PowerShell 库中获取其他模块的工具。Description: This module contains the tooling used to get other modules from PowerShell Gallery. Windows 10 和 Windows Server 随附了版本。Version ships with Windows 10 and Windows Server. 需要 1.6.0 或更高版本。Version 1.6.0 or higher is required. 若要确定安装了哪个版本,请运行 Get-Command -Module PowerShellGet 命令。To determine which version is installed, run the Get-Command -Module PowerShellGet command.
    • 参考:安装 PowerShellGetReference: Installing PowerShellGet.
    • 命令:Install-Module -Name PowerShellGetCommand: Install-Module -Name PowerShellGet.
    • 可选参数:Optional parameters:
      • -Proxy.-Proxy. 指定请求的代理服务器。Specifies a proxy server for the request.
      • -Force.-Force. 忽略“已安装”警告并安装最新版本。Bypasses the "already installed" warning and installs the latest version.

    如果使用的不是最新 PowerShellGet 版本,则会看到此错误:You'll receive this error if you're not using the newest version of PowerShellGet:

    Install-Module : A parameter cannot be found that matches parameter name 'AllowPrerelease'.
    At line:1 char:20
    Install-Module abc -AllowPrerelease
    CategoryInfo          : InvalidArgument: (:) [Install-Module], ParameterBindingException
    FullyQualifiedErrorId : NamedParameterNotFound,Install-Module
  5. 重启 PowerShell。Restart PowerShell. 无法在当前会话中加载新版本。You can't load the new version in the current session. 新 PowerShell 会话会加载最新版 PowerShellGet。New PowerShell sessions will load the latest version of PowerShellGet.

这些步骤将从 PowerShell 库下载 Az.ApplicationMonitor 模块。These steps will download the Az.ApplicationMonitor module from PowerShell Gallery.

  1. 确保满足 PowerShell 库的所有先决条件。Ensure that all prerequisites for PowerShell Gallery are met.
  2. 以管理员身份使用提升的执行策略运行 PowerShell。Run PowerShell as Admin with an elevated execution policy.
  3. 安装 Az.ApplicationMonitor 模块。Install the Az.ApplicationMonitor module.
    • 参考:Install-ModuleReference: Install-Module.
    • 命令:Install-Module -Name Az.ApplicationMonitorCommand: Install-Module -Name Az.ApplicationMonitor.
    • 可选参数:Optional parameters:
      • -Proxy.-Proxy. 指定请求的代理服务器。Specifies a proxy server for the request.
      • -AllowPrerelease.-AllowPrerelease. 允许安装 alpha 和 beta 版本。Allows installation of alpha and beta releases.
      • -AcceptLicense.-AcceptLicense. 跳过“接受许可证”提示Bypasses the "Accept License" prompt
      • -Force.-Force. 忽略“不受信任的存储库”警告。Bypasses the "Untrusted Repository" warning.

手动下载并安装模块(脱机选项)Download and install the module manually (offline option)

如果出于任何原因无法连接到 PowerShell 模块,可以手动下载并安装 Az.ApplicationMonitor 模块。If for any reason you can't connect to the PowerShell module, you can manually download and install the Az.ApplicationMonitor module.

手动下载最新的 nupkg 文件Manually download the latest nupkg file

  1. 转到 to
  2. 在“版本历史记录”表中选择文件的最新版本。Select the latest version of the file in the Version History table.
  3. 在“安装选项”下选择“手动下载”。Under Installation Options, select Manual Download.

选项 1:安装到 PowerShell 模块目录中Option 1: Install into a PowerShell modules directory

将手动下载的 PowerShell 模块安装到 PowerShell 目录中,使之可被 PowerShell 会话发现。Install the manually downloaded PowerShell module into a PowerShell directory so it will be discoverable by PowerShell sessions. 有关详细信息,请参阅安装 PowerShell 模块For more information, see Installing a PowerShell Module.

使用 Expand-Archive (v1.0.1.0) 将 nupkg 作为 zip 文件解压缩Unzip nupkg as a zip file by using Expand-Archive (v1.0.1.0)

  • 说明:基础版本的 Microsoft.PowerShell.Archive (v1.0.1.0) 无法解压缩 nupkg 文件。Description: The base version of Microsoft.PowerShell.Archive (v1.0.1.0) can't unzip nupkg files. 使用 .zip 扩展名重命名文件。Rename the file with the .zip extension.

  • 参考:Expand-ArchiveReference: Expand-Archive.

  • 命令:Command:

    $pathToNupkg = "C:\az.applicationmonitor.0.3.0-alpha.nupkg"
    $pathToZip = ([io.path]::ChangeExtension($pathToNupkg, "zip"))
    $pathToNupkg | rename-item -newname $pathToZip
    $pathInstalledModule = "$Env:ProgramFiles\WindowsPowerShell\Modules\az.applicationmonitor"
    Expand-Archive -LiteralPath $pathToZip -DestinationPath $pathInstalledModule

使用 Expand-Archive (v1.1.0.0) 解压缩 nupkgUnzip nupkg by using Expand-Archive (v1.1.0.0)

  • 说明:使用最新版本的 Expand-Archive 解压缩 nupkg 文件但不更改扩展名。Description: Use a current version of Expand-Archive to unzip nupkg files without changing the extension.

  • 参考:Expand-ArchiveMicrosoft.PowerShell.ArchiveReference: Expand-Archive and Microsoft.PowerShell.Archive.

  • 命令:Command:

    $pathToNupkg = "C:\az.applicationmonitor.0.2.1-alpha.nupkg"
    $pathInstalledModule = "$Env:ProgramFiles\WindowsPowerShell\Modules\az.applicationmonitor"
    Expand-Archive -LiteralPath $pathToNupkg -DestinationPath $pathInstalledModule

选项 2:手动解压缩并导入 nupkgOption 2: Unzip and import nupkg manually

将手动下载的 PowerShell 模块安装到 PowerShell 目录中,使之可被 PowerShell 会话发现。Install the manually downloaded PowerShell module into a PowerShell directory so it will be discoverable by PowerShell sessions. 有关详细信息,请参阅安装 PowerShell 模块For more information, see Installing a PowerShell Module.

若要将模块安装到任何其他目录中,请使用 Import-Module 手动导入模块。If you're installing the module into any other directory, manually import the module by using Import-Module.


DLL 会通过相对路径进行安装。DLLs will install via relative paths. 请将此包的内容存储到所需的运行时目录中,并确认访问权限允许读取但不允许写入。Store the contents of the package in your intended runtime directory and confirm that access permissions allow read but not write.

  1. 将扩展名更改为“.zip”,并将包的内容解压缩到所需的安装目录中。Change the extension to ".zip" and extract the contents of the package into your intended installation directory.
  2. 找到 Az.ApplicationMonitor.psd1 的文件路径。Find the file path of Az.ApplicationMonitor.psd1.
  3. 以管理员身份使用提升的执行策略运行 PowerShell。Run PowerShell as Admin with an elevated execution policy.
  4. 通过 Import-Module Az.ApplicationMonitor.psd1 命令加载模块。Load the module by using the Import-Module Az.ApplicationMonitor.psd1 command.

通过代理路由流量Route traffic through a proxy

监视专用 Intranet 中的计算机时,需要通过代理路由 HTTP 流量。When you monitor a computer on your private intranet, you'll need to route HTTP traffic through a proxy.

用于从 PowerShell 库下载并安装 Az.ApplicationMonitor 的 PowerShell 命令支持 -Proxy 参数。The PowerShell commands to download and install Az.ApplicationMonitor from the PowerShell Gallery support a -Proxy parameter. 编写安装脚本时,请查看上述说明。Review the preceding instructions when you write your installation scripts.

Application Insights SDK 需要将应用的遥测数据发送给 Microsoft。The Application Insights SDK will need to send your app's telemetry to Microsoft. 建议在 web.config 文件中配置应用的代理设置。We recommend that you configure proxy settings for your app in your web.config file. 有关详细信息,请参阅 Application Insights 常见问题解答:代理传递For more information, see Application Insights FAQ: Proxy passthrough.

启用监视Enable monitoring

使用 Enable-ApplicationInsightsMonitoring 命令以启用监视。Use the Enable-ApplicationInsightsMonitoring command to enable monitoring.

有关如何使用此 cmdlet 的详细说明,请参阅 API 参考See the API reference for a detailed description of how to use this cmdlet.

后续步骤Next steps

查看遥测:View your telemetry:

添加更多遥测:Add more telemetry:

使用 Application Insights 代理执行更多操作:Do more with Application Insights Agent:

  • 使用我们的指南对 Application Insights 代理进行故障排除Use our guide to troubleshoot Application Insights Agent.