powershell - 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
解决方案
$_
表示当前处理的对象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 上的初学者。
推荐阅读
- php - MySql date range query not showing all records
- angular - 如何更改角材料(表格)中的边框底部颜色
- docker - docker NGINX 服务器子位置问题
- android - UnsatisfiedLinkError libreactnativejni.so App Bundle
- http - URI 的注册已经存在 - 如何以编程方式检查/删除注册?
- asp.net-core - 如何在 URL 中的某些后缀上引发 HTTP404?
- javascript - o["postmore"] 不可迭代
- c++ - 有成员的类的术语
- javascript - 如何在 ejs 中访问函数内的数组对象
- python - 文本文件的最后一行缺少换行符;如何应对?