Script Repository


Export mailbox archive to PST

June 23, 2020
2379

The script can be used in Business Rules, Custom Commands and Scheduled Tasks to export the content of an Exchange mailbox archive to a Personal Storage Table (PST) file. To add it to your rule, command or task, use the Run a program or PowerShell script action.

Note: To be able to export mailbox archive, the Adaxes service account must be assigned to the Mailbox Import Export role in Exchange. To use a different account, specify the credentials of the necessary account it in the Run as section of your Run a program or PowerShell script action.

Parameters:

  • $exchangeServer - specifies the Fully Qualified Domain Name (FQDN) of the Exchange Server that will be used to perform the export;
  • $filePath - specifies the path to the PST file with the archive contents;
  • $reportFilePath - specifies a path to a text file that will contain any errors that occur in the process of export.
You can use value references in both the PST file and the error file paths. When the script will be executed, the value references will be replaced with property values of the user on which it is executed. For example, if you specify \\server\share\Archive\%username%.pst, and the script is executed on a user whose username is jdoe, the path will be \\server\share\Archive\jdoe.pst.
Edit Remove
PowerShell
$exchangeServer =  "exchangeserver.domain.com" # TODO: modify me
$filePath = "\\server\share\Archive\%username%.pst" # TODO: modify me
$reportFilePath = "\\server\share\Archive\MailboxExportErrorReport_%username%.txt" # TODO: modify me
$waitTimeSeconds = 9 * 60 # TODO: modify me

# Script block to initiate the mailbox archive export
$scriptBlock = {
    param($exchangeServer, $filePath, $reportFilePath)

    try
    {
        # Connect to the Exchange Server
        $session = New-PSSession -configurationname Microsoft.Exchange -connectionURI "http://$exchangeServer/PowerShell"
        Import-PSSession $session -DisableNameChecking -AllowClobber | Out-Null

        # Clear all previous export requests with the same name
        Get-MailboxExportRequest -Name "%username%" | Remove-MailboxExportRequest -Confirm:$False
        try
        {
            # Initiate export of the mailbox archive
            New-MailboxExportRequest "%username%" -FilePath $filePath -Name "%username%" -IsArchive -ErrorAction Stop | Out-Null
        }
        catch
        {
            # Generate error report
            $errorMessage = 'Failed to initiate export of mailbox archive for %username%. Error message: ' + $_.Exception.Message # TODO: modify me
            Write-Error $errorMessage
            Out-File -InputObject $errorMessage -Append -FilePath $reportFilePath
        }
    }
    finally
    {
        # Close connection to the Exchange Server
        Remove-PSSession $session
    }

}

# Start Windows PowerShell as a separate process and run the script block
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $exchangeServer, $filePath, $reportFilePath
Wait-Job -Job $job -Timeout $waitTimeSeconds

if ($job.State -ne "Completed")
{
    return
}

# Get output from the separate PowerShell process
Receive-Job -Job $job


Comments ( 2 )
avatar
KVM
Mar 20, 2019
We get this error when running the script above, what should we use to sequentially get ever users mailbox archive to export?

Failed to initiate export of mailbox archive for %username%. Error message: The operation couldn't be performed
because '%username%' couldn't be found.
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
+ PSComputerName : localhost
avatar
Support
Mar 20, 2019

Hello,

 

Could you, please, specify how exactly you are executing the script? A screenshot of the Custom Command, Scheduled Task or Business Rule executing the script would be much appreciated. You can send it to our support email support[at]adaxes.com.


Also, could you provide us with the exact script you are using with all the modifications in TXT format?

Leave a comment