PowerShell获取用户电子邮件地址到Exchange

时间:2017-12-08 09:56:11

标签: powershell azure

尝试使用广告同步组执行自动结扎保留脚本,直至Azure。有人能帮忙解决下面的问题吗?

我正在尝试通过返回MsolGroupMembers来从MsolGroup获取所有用户,我想返回用户然后我需要获取他们的UPN /电子邮件地址,以便我可以将其管道进入交换在线命令为该组中的每个用户提供诉讼保留。

Import-Module MSOnline
$user="removed"

$file = "removed"

$password = Get-Content $File | ConvertTo-SecureString 

$cred = New-Object System.Management.Automation.PsCredential($user,$password)

Connect-MsolService -Credential $cred
$UserCredential = Get-Credential -Credential $cred

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

$usersOfGroup = Get-MsolGroupMember -GroupObjectId removedID -All


foreach ($user in $usersOfGroup)
{
    $mailbox = Get-Mailbox $user.EmailAddress 

    Write-Output $mailbox

    if ($mailbox.LitigationHoldEnabled -eq $false -and $mailbox.RecipientTypeDetails -eq "UserMailbox")
    {
        Write-Output "Current mailbox: " $mailbox.UserPrincipalName
        Set-Mailbox -Identity $mailbox.UserPrincipalName -LitigationHoldEnabled $true -LitigationHoldDuration 2555 -WarningAction SilentlyContinue
    }
}

Remove-PSSession $Session

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题。

    #Useful Microsoft documentation
    #https://technet.microsoft.com/en-us/library/dn767952(v=exchg.160).aspx

    #Connect to Exchange Online
    Import-Module MSOnline
    $O365Cred = Get-Credential
    $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell" -Credential $O365Cred -Authentication Basic -AllowRedirection
    Import-PSSession $O365Session -AllowClobber
    Connect-MsolService -Credential $O365Cred

    #Filter mailboxes for the HR Department, pipe to the set litigation command for 365 days
   Get-Recipient -RecipientTypeDetails UserMailbox -ResultSize unlimited -Filter 'Department -eq "HR"' | Set-Mailbox -LitigationHoldEnabled $true -LitigationHoldDuration 365
    Remove-PSSession -Id $O365Session.Id