【问题标题】:Office 365 Power Shell to Add Users based on department to a Shared MailboxOffice 365 Power Shell 将基于部门的用户添加到共享邮箱
【发布时间】:2020-03-19 20:37:26
【问题描述】:

我有下面的代码,它不是很有效,希望得到任何建议。我有基本的 powershell 编码技能,所以如果我的陈述完全错误,请原谅我。我正在尝试根据有效的部门变量来获取用户,然后将每个用户添加到无效的共享邮箱中。我认为问题在于变量或 for 循环,因为手动运行的命令可以正常工作。

提前谢谢你。

$UserCredential = Get-Credential
$Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionURI https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential –Authentication Basic –AllowRedirection
Set-ExecutionPolicy Unrestricted
Import-PSSession $Session
Import-Module msonline
Connect-MsolService -Credential $UserCredential
$DL = Get-MsolUser -All -Department "Staff" | select-object UserPrincipalName
ForEach ($UserPrincipalName in $DL) {
$DL | % {Add-MailboxPermission "shared-mailbox@domain.com"} -User $_ -AccessRights FullAccess -InheritanceType All}
Remove-PSSession $Session 

【问题讨论】:

    标签: powershell office365


    【解决方案1】:

    要获取特定部门的所有用户,您可以使用“where”条件。然后您可以通过管道将结果添加到共享邮箱的完全访问权限。

    Get-MsolUser -All | where{$_.department -eq "XXX"} | foreach{
     $UPN=$_.UserPrincipalName
    Add-MailboxPermission -Identity "Shared MB name" -User $UPN -AccessRights FullAccess -InheritanceType All
    }

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    相关资源
    最近更新 更多