New-UnifiedGroup不适用于ErrorAction

时间:2020-04-28 05:42:41

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0 azure-powershell

我正在使用以下代码在O365中使用Powershell创建公共组:

Try
{
     New-UnifiedGroup -AccessType Public -Alias $groupIdentity -DisplayName $groupDisplayName -Owner $smtpAddress
}
Catch
{
     # Some exception handling statements
}

但是,我认为在失败的情况下它并没有赶上来。 经过一番调查后,我知道要在命令的末尾使用-ErrorAction stop才能使其生效。 但是当我执行以下操作时:

New-UnifiedGroup -AccessType Public -Alias $groupIdentity -DisplayName $groupDisplayName -Owner $smtpAddress -ErrorAction stop

此操作失败,并显示以下错误:

The "ErrorAction" parameter can't be used on the "New-UnifiedGroup" cmdlet because it isn't present in the role definition for the current user. Check the management roles assigned to you, and try again.

但是我还是被分配了Global Admin个角色,所以我不知道自己在做什么错。

2 个答案:

答案 0 :(得分:0)

您收到的错误是您无权使用该特定参数运行该命令。您必须先分配权限,然后才能运行此cmdlet。

尽管本主题列出了cmdlet的所有参数,但是如果某些参数未包含在分配给您的权限中,则您可能无权访问它们。要查找在您的组织中运行任何cmdlet或参数所需的权限,请参阅Find the permissions required to run any Exchange cmdlet

要检查是否可以运行带有特定参数的任何cmdlet,可以使用以下脚本:

# Define what you're looking for
$user   = 'joey@contoso.com'
$cmdlet = 'New-UnifiedGroup'
$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过滤所需的结果。

答案 1 :(得分:0)

Joey Caianswer说明了如何检查是否允许使用特定参数运行cmdlet。

我已经在O365租户上进行了检查,显然我不允许在-ErrorAction中使用New-UnifiedGroup,因此它似乎是Office 365中的默认设置。作为一种解决方法,我会使用以下内容可临时更改错误操作首选项:

$previousErrorAction = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
New-UnifiedGroup -AccessType Public -Alias $groupIdentity -DisplayName $groupDisplayName -Owner $smtpAddress
$ErrorActionPreference = $previousErrorAction