【问题标题】:Remove-CalendarEvents fails when using -ErrorAction STOP使用 -ErrorAction STOP 时,Remove-CalendarEvents 失败
【发布时间】:2019-11-27 19:28:04
【问题描述】:

使用 Remove-CalendarEvents -PreviewOnly 时,我可以检索会议事件,甚至可以在测试帐户上删除它们。但是,当我将 -ErrorAction Stop 添加到命令时,我在 AD 帐户上收到了以前没有引发任何错误的新错误。

try\catch 块用于捕获在找不到用户邮箱时引发的错误。这部分有效。但是 try\catch 也捕获了一个新错误:

The "ErrorAction" parameter can't be used on the "Remove-CalendarEvents" cmdlet because it isn't present in the role definition for the current user. Check
the management roles assigned to you, and try again.
At C:\Users\O365ExchangeAdmin\AppData\Local\Temp\tmp_waex0o3a.tea\tmp_waex0o3a.tea.psm1:55507 char:9
+         $steppablePipeline.End()
+         ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Remove-CalendarEvents], CmdletAccessDeniedException
    + FullyQualifiedErrorId : [Server=CY4PR0101MB2935,RequestId=ba4d86db-d362-432d-9892-4ea92b503356,TimeStamp=7/18/2019 7:18:03 PM] [FailureCategory=Cmdlet-C
   mdletAccessDeniedException] 896C46A1,Microsoft.Exchange.Management.StoreTasks.RemoveCalendarEvents
    + PSComputerName        : outlook.office365.com

当我删除 -ErrorAction STOP 参数时,命令成功完成,并让我查看会议事件。但是,如果我没有 -ErrorAction STOP,那么当我的脚本在帐户上失败时,我将无法登录,因为它没有邮箱。

Try{
    $output = Remove-EXOCalendarEvents -Identity $user.UserPrincipalName -QueryStartDate (Get-Date) -PreviewOnly -CancelOrganizedMeetings -Confirm:$false
}Catch [System.Management.Automation.RemoteException] {
    LogWrite "$($user.Name) could not be found, most likely they do not have a mailbox"
}

同时删除 [System.Management.Automation.RemoteException] 不会改变结果。

非常感谢您提供的任何帮助,谢谢!

【问题讨论】:

  • 检查是否允许在该命令上使用此参数。使用来自that article的两个cmdlet@
  • 查看 ManagementRoleEntry 我无权访问 ErrorAction。直到明天我才能更新这个,但我会再试一次。感谢您的评论,这看起来会奏效!

标签: powershell active-directory office365 exchange-server


【解决方案1】:

您收到的错误是您无权使用该特定参数运行该命令。在 Exchange Server 中,此类权限是通过使用管理角色来管理的。您可以查看this article以了解更多信息。

要检查您是否能够使用特定参数运行任何 cmdlet,您可以使用以下脚本:

# Define what you're looking for
$user   = 'john.doe@contoso.com'
$cmdlet = 'Remove-CalendarEvents'
$param  = 'ErrorAction'

# Find all your assignments
$assignments = Get-ManagementRoleAssignment -RoleAssignee $user -Delegating $false

# Find cmdlets you can run and filter only the one you specified
$assignments.role | Foreach-Object {Get-ManagementRoleEntry "$_\*" | Where-Object {$_.Name -eq $cmdlet -and $_.Parameters -contains $param}}

在最后一行中,我们正在迭代分配给您的所有角色并检查角色条目。它们的格式是RoleName\CmdletName,所以我们使用*(通配符)来获取所有信息。在最后一个管道之后,您使用Where-Object cmdlet 仅过滤您想要的结果。

【讨论】:

  • 这就是答案,不幸的是,使用这些权限创建新的管理角色比我预期的要困难。我们最终将脚本更改为不需要 -ErrorAction Stop 参数。不过感谢您提供的信息,仅通过阅读 Microsoft 的文档就学到了很多东西。
  • 遇到了同样的问题。我也没有打扰更改权限。相反,我使用的是: if ($error[0].categoryinfo.activity -eq "Remove-CalendarEvents") 在通话之后。
猜你喜欢
  • 1970-01-01
  • 2019-09-20
  • 2017-03-24
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
相关资源
最近更新 更多