3

I have a script its working and it is programmed to take backup for every mailbox for a month and store it to local shared drive. When I run the script it export whole database instead of specified date range. I am not a programmer. I searched a Google lot but not 100% successful. I want following modifications

  1. I want that script to export mailbox only from specific database not from all databases. I tried "-Database Databasename" in the script but it gives error.

  2. Script should export all mailboxes from above mentioned database for every month. Say If run this script on Dec. 9 2015 it should export from Nov. 9 to Dec. 9 2015. Dates has to changed automatically according to compilation date.

  3. Bottom part of the script wont run where ExportRequest starts.

This is the Script

$Export = Get-Mailbox

$endDate = Get-Date "00:00:00"

$startDate = $endDate.AddDays(-30)

$month = "{0:D2}" -f [int]$startDate.Month

$year = "{0:D4}" -f [int]$startDate.Year

Write-Host -NoNewline "Exporting items between $startDate and $endDate..."

$Export|%{$_|New-MailboxExportRequest -ContentFilter {(Received -ge $startDate) -and (Received -lt $endDate)} -FilePath "\\FileServer\EmailBackups\ExportTest\Test\$($_.alias).pst"}

Write-Host "Done."

Write-Host -NoNewline "Waiting for export to complete..."

After This Script Gives Error. But Mailboxes are exported full

While(!(Get-MailboxExportRequest -Mailbox $Exprot -Status Completed))

{

    #Sleep for a  few minutes
    Write-Host -NoNewline "."
    Start-Sleep -s 60
}

Write-Host "Done."

Write-Host -NoNewline "Removing Export Request..."

Get-MailboxExportRequest -Mailbox $Export -Status Completed | Remove-MailboxExportRequest

Write-Host "Done."

Please help to make this script perfect. Thanks

DavidPostill
  • 153,128
  • 77
  • 353
  • 394

2 Answers2

2

Export Exchange Mailboxes from a particular Exchange DB and from a particular Date Range with PowerShell to PST Files with Unique Names

  1. I want that script to export mailbox only from specific database not from all databases. I tried "-Database Databasename" in the script but it gives error.
  2. Script should export all mailboxes from above mentioned database for every month. Say If run this script on Dec. 9 2015 it should export from Nov. 9 to Dec. 9 2015. Dates has to changed automatically according to compilation date.
  3. Bottom part of the script wont run where ExportRequest starts.

For Question #1. It appears you just need to put in the Get-Mailbox -Database $ExchangeDB commands and then pipe that to the rest of the logic, etc. This would be where you set a variable $ExchangeDB and set that to your Exchange DB name. For some reason you have to set this command as a variable and then pipe it or there's some issue with concurrent pipelining, etc.

For Question #2. It seems that the data variable doesn't take well with the -ContentFilter parameter with the date string for some reason, but it does if you put the date minus the days to a variable, and concatenate that as part of a command and then use that command when it's executed.

For Question #3. It appears David may have answered this and it may just be a typo issue for this part where you're having trouble. Otherwise, feel free to test with this syntax in your loop to see what results you get as I don't think you need the -Mailbox $Export parts in the command:

Get-MailboxExportRequest -resultsize "unlimited" -Status Completed | Remove-MailboxExportRequest -confirm:$false

SCRIPT NOTE POINTS

  • One date variable set with the number of days to subtract from the current date $FromDate = (Get-Date).AddDays(-30).ToString("MM/dd/yyyy") since you'll get the data from that date to the current date.
  • The Get-Mailbox -Database $ExchangeDB command has to be set as a variable and then that variable piped to the other parts of the [dynamic command] $DynCMD variable or there's an issue with concurrent pipelining.
  • Use BOTH Received -ge and Sent -ge with -or rather than -and with the -ContentFilter parameter—this ensures you get both since the receive and sent date for email items in both directions.
  • This turned into quite a task for me so I just ended up making it so it dynamically builds a PowerShell script with all applicable variables, etc. set and then executes that script afterwards. The script is put into the %temp% Windows OS environmental variable location and if it exists before it runs, it deletes it and then builds it.

Issue Note

(I thought the Invoke-Command would work but I ran out of time so it was easier and much more timely for me to get everything piped to a PowerShell script with all applicable variables set per the criteria, and just execute it afterwards. The Invoke-Command has issues with variables concatenated to other variables and built dynamic commands, so this was simple and confirmed to work as expected to resolve your issues, other than #3, which I'm still not 100% certain of the issue there exactly so assuming the typo as David suggested unless the For Question #3. syntax I provided resolves.)


POWERSHELL EXAMPLE SCRIPT (PIPELINE LOOP)

(Export all Exchange mailboxes from a particular Exchange DB to a file share to PST files with the file name being the mailbox account aliase name)

NOTE: You will need to change two variables in this script to point to what your environment is configured with 1. the Exchange DB name [$ExchangeDB = "<Exchange_DB_Name>"and 2. the UNC path where the PST files are to be exported [$ExportDir = "\\<ServerName>\<ShareName>\<FolderPath>"] so those two variables need to be changed. Once those are changed, run from the Exchange Server PowerShell command console window which is how I tested it and confirmed it worked as expected.

$TempDir      = $env:Temp
$TempPSScript = "$TempDir\TempMBExport.ps1"
$ExchangeDB   = "Mail04-Original"
$ExportDir    = "\\FileServer\Emailbackups\ExportTest\MonthTest2"
$FromDate     = (Get-Date).AddDays(-30).ToString("MM/dd/yyyy")
$DynCMD       = '$GetMBCMD | % { $_ | New-MailboxExportRequest -ContentFilter {(Received -ge "' + $FromDate + '") -or (Sent -ge "' + $FromDate + '")} -FilePath "' + $ExportDir + '\$($_.alias).pst"}'

If (Test-Path $TempPSScript){
    Remove-Item $TempPSScript
}

'$GetMBCMD = Get-Mailbox -Database ' + $ExchangeDB + '' | Out-File -Append -Force "$TempPSScript"
$DynCMD | Out-File -Append -Force "$TempPSScript"

 & "$TempPSScript"

POWERSHELL EXAMPLE SCRIPT (FOREACH LOOP)

$TempDir      = $env:Temp
$TempPSScript = "$TempDir\TempMBExport.ps1"
$ExchangeDB   = "Mail04-Original"
$ExportDir    = "\\FileServer\Emailbackups\ExportTest\MonthTest2"
$FromDate     = (Get-Date).AddDays(-30).ToString("MM/dd/yyyy")
$DynCMD1      = 'ForEach ($u in (Get-Mailbox -database ' + $ExchangeDB + ')) {'
$DynCMD2      = 'New-MailboxExportRequest -ContentFilter {(Received -ge "' + $FromDate + '") -or (Sent -ge "' + $FromDate + '")} -Mailbox $u -FilePath "' + $ExportDir + '\$($u.alias).pst"}'

If (Test-Path $TempPSScript){
    Remove-Item $TempPSScript
}

$DynCMD1 | Out-File -Append -Force "$TempPSScript"
$DynCMD2 | Out-File -Append -Force "$TempPSScript"

 & "$TempPSScript"

Screen Print Exchange Management Console and PowerShell Console

enter image description here


Test Run

Below is what you can paste into the Exchange PowerShell Management console to build the Windows temp PowerShell script dynamically per run (at complitation meeting criteria). Once this runs in the Exchange PowerShell Management Shell, on the Exchange Server but not in PS, go to Start --> Run --> and type in %Temp% and press Enter.

From there see if the PS1 file is created named TempMBExport.ps1 and open it with Notepad and look at the logic. See what happens if you copy what's in that script into the Exchange PowerShell Management console and press Enter.

I'm not sure if there's an issue with this not going to the Windows Temp environmental variable folder if the file doesn't exist or the PS console not having access to execute it from there or if your version of Exchange doesn't like one of those commands so start with this and let's see what happens to troubleshoot.

This below script excludes the & "$TempPSScript" part so it does NOT execute the dynamically built and complitated %temp%\TempMBExport.ps1 script so confirming it's in the location and whether or not this logic will run in the Exchange PS Management console is a good next step to figure out the problem.

It's hard to troubleshoot in writing without access or remotely supporting so I'm a little limited here and have to speculate some unfortunately. Not sure what further options you'd have but those are some quick thoughts.

$TempDir      = $env:Temp
$TempPSScript = "$TempDir\TempMBExport.ps1"
$ExchangeDB   = "Mail04-Original"
$ExportDir    = "\\FileServer\Emailbackups\ExportTest\MonthTest2"
$FromDate     = (Get-Date).AddDays(-30).ToString("MM/dd/yyyy")
$DynCMD       = '$GetMBCMD | % { $_ | New-MailboxExportRequest -ContentFilter {(Received -ge "' + $FromDate + '") -or (Sent -ge "' + $FromDate + '")} -FilePath "' + $ExportDir + '\$($_.alias).pst"}'

If (Test-Path $TempPSScript){
    Remove-Item $TempPSScript
}

'$GetMBCMD = Get-Mailbox -Database ' + $ExchangeDB + '' | Out-File -Append -Force "$TempPSScript"
$DynCMD | Out-File -Append -Force "$TempPSScript"

LASTLY

When I run the PS as I stated above and open it with notepad just to see the logic, it builds it in this format with this sytax so perhaps try running this in your Exchange PS Management Shell and see what results you get. If it works this way then the issue has something to do with the script execution but you're not getting any errors but maybe this will shed some light on the topic as well.

$GetMBCMD = Get-Mailbox -Database Mail04-Original
$GetMBCMD | % { $_ | New-MailboxExportRequest -ContentFilter {(Received -ge "11/12/2015 00:00:00") -or (Sent -ge "11/12/2015 00:00:00")} -FilePath "\\FileServer\Emailbackups\ExportTest\MonthTest2\$($_.alias).pst"}
Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
1

After this the script gives an error

While(!(Get-MailboxExportRequest -Mailbox $Exprot -Status Completed))

It looks like you have a spelling mistake.

Replace $Exprot with $Export:

While(!(Get-MailboxExportRequest -Mailbox $Export -Status Completed))
DavidPostill
  • 153,128
  • 77
  • 353
  • 394
  • At least I could fix the **obvious** mistake. I really don't know PS very well. So I can't fix your other mistakes. – DavidPostill Dec 10 '15 at 15:16