I have used powershell for 4 months now and learned some basic stuff e.g. how to loop csv, create functions, export etc. However I am trying to get more complex with it and started building a Leavers script for the company I am currently working for. So far so good I managed to compile a working script where I automatically: import an initial CSV containing usernames then loop them and create folder on the target dest., next comes the mailbox export (where I need help actually and the purpose of this post) and all fine with that but the problem is it cycles everyone from the CSV and pushes the Exchange to export all at once. I don't like that because it overutilize the Server and I saw larger mailboxes failed so I decided to try and build a Queue where Mailboxes are getting exported one by one.
This is what I am using currently where it pushes everyone at once:
$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
}
And this is something I came up with yesterday:
$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"}
}
On this script I added "somecmdlet" -Like 'PercentComplete' -eq '100', where the 100 represents the percentage of the export from Get-MailboxExportRequestStatistics, so when the previous export has reached 100% execute the next export from the CSV.
Any thoughts which cmdlet I could use to "tag" the PercentComplete guys ?
Name StatusDetail SourceAlias PercentComplete
---- ------------ ----------- ---------------
MailboxExport Completed Dummy.User 100
Sorry for the long post but I tried to make it as clear as possible :)
Thanks!
========================== Edit as per Xiakit's Advice:==========================
$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
}
}
}
However the script ends and never loops :( the output:
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!!
To cut the saga at the end I asked for TechNet for help as well since of course they are Microsoft and they should know the answer to every Powershell problem :) Bigh thanks to Xiakit for the effort to resolve my problem (on which I am still keen on working that out since I have started it and I want to finish it) however here is the solution in queuing Mailbox Exports:
$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"
}
}
Hope this will come in handy to other Admins trying to automate leaver processes in their company for multiple users!!
Thanks for the support!
Vlatko