首页 > 解决方案 > Powershell 复制邮箱权限

问题描述

我们正在使用 Office365,通常当我们需要创建一个新用户时,它必须是另一个员工的精确副本。通常这些用户可以访问不同的共享邮箱,我想创建一个 powershell 脚本,将共享邮箱的权限从特定用户复制到新用户,以便他们都可以访问相同的共享邮箱。

我能够使用以下命令从一个用户那里获得权限:

Get-Mailbox | Get-MailboxPermission -User t.test@company.com

然后我可以使用这个输出来设置权限:

Add-MailboxPermission -Identity example@company.com -AccessRights FullAccess -InheritanceType All -AutoMapping:$true -User t.test@company.com
Add-RecipientPermission -Identity example@company.com -AccessRights SendAs -Confirm:$false -Trustee t.test@company.com

但如果我能用 1 个脚本做到这一点,那就太好了。所以我尝试了以下方法:

$FUser = Read-Host "Enter the mail adress of the user you want to copy mailbox permissions from"
$TUser = Read-Host "Enter the mail adress of the user you want to set mailbox permissions for"

$GPerm = Get-Mailbox | Get-MailboxPermission -User $FUser

$GPerm | ForEach-Object { $_ 
Add-MailboxPermission -Identity $_ -AccessRights FullAccess -InheritanceType All -AutoMapping:$true -User $TUser
Add-RecipientPermission -Identity $_ -AccessRights SendAs -Confirm:$false -Trustee $TUser
}

但这给了我以下错误:

Cannot process argument transformation on parameter 'Identity'. Cannot convert value "Microsoft.Exchange.Management.RecipientTasks.MailboxAcePresentationObject" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "Cannot convert hashtable to an 
object of the following type: Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not supported in restricted language mode or a Data section."
    + CategoryInfo          : InvalidData: (:) [Add-MailboxPermission], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-MailboxPermission
    + PSComputerName        : outlook.office365.com

标签: powershelloffice365

解决方案


$_表示当前处理的对象ForEach-Object。当您$_在该命令中运行时,您会看到如下内容:

Identity             User                 AccessRights                 IsInherited Deny 
--------             ----                 ------------                 ----------- ---- 
FirstName LastName   another.user@aa.com {FullAccess, ReadPermission}  False       False

正如您在Add-MailboxPermission文档中看到的(同样适用,Add-RecipientPermission但我会让您找到并检查自己):

-身份

Identity 参数指定要将权限分配给用户的邮箱。您可以使用任何唯一标识邮箱的值。

例如:

姓名

显示名称

别名

专有名称 (DN)

规范 DN

域名\账户名

电子邮件地址

图形用户界面

旧版ExchangeDN

SamAccountName

用户 ID 或用户主体名称 (UPN)

因此,您可以看到您需要指定任何唯一标识符。您可以使用$_.Identity来提供邮箱标识。

提示:在这种特殊情况下,通过组分配权限可能对您很有用,因为它比将权限从一个用户复制到另一个用户要容易得多。


请注意 - 我上面解释的内容几乎是 PowerShell 的基本内容。我建议看一些在线课程来提高你的 PowerShell 技能。可以开始的示例是PowerShell:MVA 上的初学者


推荐阅读