Powershell 新的UnifiedGroup没有';不要用错误动作

Powershell 新的UnifiedGroup没有';不要用错误动作,powershell,powershell-2.0,powershell-3.0,powershell-4.0,azure-powershell,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 } 但我不认为它能在失败的情况下被抓住。 经过一点调查,我知道需要在命令末尾使用-E

我正在使用以下代码在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.

但是,我再次分配了
全局管理员
角色,因此我不知道我做错了什么。

您收到的错误是,您无权使用该特定参数运行该命令。在运行此cmdlet之前,需要为您分配权限

尽管本主题列出了cmdlet的所有参数,但如果某些参数未包含在分配给您的权限中,则您可能无权访问这些参数。要查找在组织中运行任何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仅筛选所需的结果。

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

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

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

我也有同样的经历。我发现的问题是,当Microsoft生成错误时,设置全局ErrorActionPreference似乎并不总是触发catch块。@Robin任何此类行为的示例?使用全局管理员帐户,我们也有这个问题。