使用 Powershell 队列邮箱导出

Queue Mailbox Export with Powershell

我已经使用 powershell 4 个月了,并且学习了一些基本的东西,例如如何循环 csv、创建函数、导出等。但是,我试图让它变得更复杂,并开始为我目前工作的公司构建一个 Leavers 脚本。到目前为止一切顺利,我设法编译了一个工作脚本,我自动地:导入一个包含用户名的初始 CSV,然后循环它们并在目标目标上创建文件夹。接下来是邮箱导出(我实际上需要帮助的地方和这个 post) 一切都很好,但问题是它从 CSV 循环每个人并推动 Exchange 一次导出所有内容。我不喜欢这样,因为它过度使用了服务器,而且我看到更大的邮箱失败了,所以我决定尝试构建一个队列,其中邮箱一个接一个地导出。

这就是我目前使用的,它同时推动所有人:

$dest = "%\Desktop\#SCRIPTS\Modules\Exports\Temp\Target.csv"
$targetStorage = "\SERVER\Targetfolders"
$Connection = "http://Exchange-Server.companydomain.com/PowerShell/"



Set-ExecutionPolicy RemoteSigned
    $UserCredential = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $Connection -Authentication Kerberos -Credential $UserCredential
    Import-PSSession $Session
    #########################################################################
    $mails = Import-Csv $dest
    ForEach ($mail in $mails)
    {
        New-MailboxExportRequest -Mailbox "$($mail.Name)@CompanyDomain.com" -FilePath "$($targetStorage)$($mail.Name)$($mail.Name).pst" | Out-Null
    }

这是我昨天想到的:

$dest = "%\Desktop\#SCRIPTS\Modules\Exports\Temp\Target.csv"
$targetStorage = "\SERVER\Targetfolders"
$Connection = "http://Exchange-Server.companydomain.com/PowerShell/"



$mails = Import-Csv $dest
ForEach ($mail in $mails) {

Get-MailboxExportRequest -Mailbox $mail.Name
if (Get-MailboxExportRequest -Status InProgress) {
        Write-Host "Mailboxes are still exporting, do not cancel the script!!"
        }

else { Get-MailboxExportRequestStatistics -Identity $mail.Name | **Some-Cmdlet** -Like 'PercentComplete' -eq '100' | New-MailboxExportRequest -Mailbox $mail.Name -FilePath "$($synStorage)$($mail.Name)$($mail.Name).pst"}

}

在这个脚本中我添加了"somecmdlet" -Like 'PercentComplete' -eq '100',其中100表示​​从Get-MailboxExportRequestStatistics导出的百分比,所以当上一次导出已经达到100时% 从 CSV 执行下一次导出。

我可以使用哪个 cmdlet 来 "tag" PercentComplete 家伙?

Name          StatusDetail SourceAlias PercentComplete
----          ------------ ----------- ---------------
MailboxExport Completed    Dummy.User  100

抱歉这么长post,但我尽量说得清楚:)

谢谢!

========================== 根据 Xiakit 的建议编辑:==========================

$mails = Import-Csv $dest
    ForEach ($mail in $mails) {

    Get-MailboxExportRequest -Mailbox $mail.Name
    if (Get-MailboxExportRequest -Status InProgress) {
            Write-Host "Mailboxes are still exporting, do not cancel the script!!"
            }

    else { 
    New-MailboxExportRequest -Mailbox $mail.Name -FilePath "$($synStorage)$($mail.Name)$($mail.Name).pst"
        while($requeststatus.status -notlike "Completed"){
            Start-Sleep -seconds 10
            $RequestStatus = Get-MailboxExportRequest -Identity $mail.Name
        }
    }
}

但是脚本结束并且从不循环 :( 输出:

    Mailboxes are still exporting, do not cancel the script!!
Name           Mailbox                                                      Status    
----           -------                                                      ------    
MailboxExport  ommited/Enterprise/Users/Vlatko Completed 
MailboxExport1 ommited/Enterprise/Users/Vlatko Completed 
MailboxExport2 ommited/Enterprise/Users/Vlatko InProgress
MailboxExport  omitted/Enterprise/Users/Dummy User      Queued    
MailboxExport  omitted/Enterprise/Users/Dummy User      Queued    
MailboxExport  omitted/Enterprise/Users/Dummy User      Queued    
Mailboxes are still exporting, do not cancel the script!!

还有一个Get-MailboxExportRequest,你可以查看是否完成了一个请求:

while($requeststatus.status -notlike "Completed"){
    Start-Sleep -seconds 10
    $RequestStatus = Get-MailboxImportRequest -Identity "whatever"
}

只要您的请求未完成,这就会循环。

为了结束传奇,我也向 TechNet 寻求帮助,因为他们当然是 Microsoft,他们应该知道每个 Powershell 问题的答案:)非常感谢 Xiakit 为解决我的问题所做的努力(我仍然热衷于解决这个问题,因为我已经开始它并且我想完成它)但是这里是排队邮箱导出的解决方案:

    $mails = Import-Csv $dest
ForEach ($mail in $mails) {
    New-MailboxExportRequest -Mailbox $mail.Name -FilePath "$($synStorage)$($mail.Name)$($mail.Name).pst"

    do {
        Write-Host "Queued"
        Start-Sleep -s 5 
       } while(Get-MailboxExportRequest -Mailbox $mail.Name -Status Queued)

    do {
         Write-Host "InProgress" 
         Start-Sleep -s 5 
        } while(Get-MailboxExportRequest -Mailbox $mail.Name -Status InProgress)

    If(Get-MailboxExportRequest -Mailbox $mail.Name -Status Complete){
        Write-Host "A export request complete"
        Get-MailboxExportRequest | Remove-MailboxExportRequest -Confirm:$false
    }

    If(Get-MailboxExportRequest -Mailbox $mail.Name -Status Failed){
        Write-Host "A error occur"
        }
    }

希望这对试图在公司中为多个用户自动执行离职流程的其他管理员派上用场!!

感谢支持!

弗拉特科