You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用New-AzActivityLogAlert创建日志警报失败,提示无法加载Az.Monitor.internal

解决Az.Monitor模块无法加载Az.Monitor.internal的问题

问题详情

执行New-AzActivityLogAlert命令时触发以下错误:

New-AzActivityLogAlert : 无法加载模块'Az.Monitor.internal'。如需更多信息,请运行'Import-Module Az.Monitor.internal'。
At E:\Code\Code\ManageLogAnalyticsAlerts.ps1:28 char:5
New-AzActivityLogAlert -Name 'sometest' -ResourceGroupName $logWo ...
CategoryInfo : ObjectNotFound: (Az.Monitor.inte...ctivityLogAlert:String) [New-AzActivityLogAlert], CommandNotFoundException
FullyQualifiedErrorId : CouldNotAutoLoadModule,New-AzActivityLogAlert

触发错误的代码行:

New-AzActivityLogAlert -Name 'sometest' -ResourceGroupName $logWorkspace.ResourceGroupName -Action $activityGroupObject -Condition @($condition1,$condition3) -Location global -Scope $scope

完整执行代码:

$receiverName = $actionGroupName 
$actionGroupReceiver = New-AzActionGroupReceiver -Name $receiverName -EmailReceiver -EmailAddress 'emailstub@email.com' -UseCommonAlertSchema -WarningAction silentlyContinue

$actionGroup = Set-AzActionGroup -Name $actionGroupName -ResourceGroup $logWorkspace.ResourceGroupName -ShortName $actionGroupName -Receiver $actionGroupReceiver  -WarningAction silentlyContinue


Write-Host '----Action Group: ' $actionGroupName  ', Action Group Id: ' $actionGroup.Id ', created with Action Group Receiver: ' $receiverName ' in the log workspace: ' $logWorkspace.Name

$activityGroupObject = New-AzActivityLogAlertActionGroupObject -Id $actionGroup.Id  -WebhookProperty @{"sampleWebhookProperty"="SamplePropertyValue"}

$scope = "subscriptions/"+(Get-AzContext).Subscription.ID
$condition1=New-AzActivityLogAlertAlertRuleAnyOfOrLeafConditionObject -Equal Ingestion -Field category
# $condition2=New-AzActivityLogAlertAlertRuleAnyOfOrLeafConditionObject -Equal Error -Field level

$any1=New-AzActivityLogAlertAlertRuleLeafConditionObject -Field properties.Operation -Equal 'Data collection"'
$any2=New-AzActivityLogAlertAlertRuleLeafConditionObject -Field properties.incidentType -Equal Incident
$condition3=New-AzActivityLogAlertAlertRuleAnyOfOrLeafConditionObject -AnyOf $any1,$any2

New-AzActivityLogAlert -Name 'sometest' -ResourceGroupName $logWorkspace.ResourceGroupName -Action $activityGroupObject -Condition @($condition1,$condition3) -Location global -Scope $scope

环境信息

  • PowerShell版本:
$PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.18362.628
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.628
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
  • Az PowerShell模块版本:9.0.1

解决方案

1. 手动导入内部模块

先尝试手动加载Az.Monitor.internal模块,强制刷新加载:

Import-Module Az.Monitor.internal -Force

如果提示模块不存在,检查Az.Monitor的安装路径:

(Get-Module Az.Monitor).ModuleBase

进入该路径下的internal子文件夹,确认是否存在Az.Monitor.internal.psd1文件。若不存在,说明模块安装不完整。

2. 修复Az.Monitor模块安装

如果模块文件缺失,先更新模块:

Update-Module Az.Monitor -Force

若更新无效,卸载后重新安装指定版本:

Uninstall-Module Az.Monitor -AllVersions -Force
Install-Module Az.Monitor -RequiredVersion 9.0.1 -Force -AllowClobber

注意:执行卸载前需关闭所有依赖该模块的PowerShell会话。

3. 检查PowerShell执行策略

执行策略限制可能导致模块无法加载,查看当前策略:

Get-ExecutionPolicy

若为Restricted,修改为RemoteSigned(仅对当前用户生效):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

4. 统一Az模块版本

Az模块各组件版本不一致可能引发依赖问题,查看已安装的Az模块版本:

Get-Module -ListAvailable Az.* | Select-Object Name, Version

若存在版本不匹配的模块,统一更新到9.0.1:

Update-Module Az -RequiredVersion 9.0.1 -Force

内容的提问来源于stack exchange,提问作者Vowelman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:30:55