我们正在使用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
答案 0 :(得分:0)
$_
代表ForEach-Object
中当前处理的对象。当您在该命令中运行$_
时,您将看到类似以下的内容:
Identity User AccessRights IsInherited Deny
-------- ---- ------------ ----------- ----
FirstName LastName another.user@aa.com {FullAccess, ReadPermission} False False
您可以在Add-MailboxPermission
文档中看到(同样适用于Add-RecipientPermission
,但我会让您查找并检查一下自己):
-身份
Identity参数指定要分配到的邮箱 用户的权限。您可以使用唯一的任何值 标识邮箱。
例如:
名称
显示名称
别名
专有名称(DN)
规范DN
域名\帐户名
电子邮件地址
GUID
LegacyExchangeDN
SamAccountName
用户ID或用户主体名称(UPN)
因此,您可以看到您需要指定任何唯一标识符。您可以使用$_.Identity
提供邮箱身份。
提示:在这种特殊情况下,通过组分配权限可能会很有用,因为与将权限从一个用户复制到另一个用户相比,管理起来要容易得多。
仅需注意-我上面解释的内容基本上是PowerShell的基础。我建议看一些在线课程来提高您的PowerShell技能。何时开始的示例是PowerShell: Beginner on MVA。