PowerShell script exchange
Post Reply
Didi7
Expert
Posts: 349
Liked: 37 times
Joined: Oct 17, 2014 8:09 am
Contact:

Export all Exchange mailboxes of a specific database each in a single PST via PowerShell ...

Post by Didi7 » Jan 09, 2020 5:40 pm

Hi everybody,

I am currently facing a new challenge to use PowerShell scripts to export all mailboxes of a specific OnPrem Exchange 2010 mailbox database each in a single PST-file at once. The GUI does not offer such an option and I could'nt find a forum thread that offers a solution.

I only have beginner experience with PowerShell, but at least I managed to open an Exchange restore point, open an Exchange item restore session and connect to a specific Exchange mailbox database but I was unable to connect to a specific mailbox and export this mailbox to a single PST-file.

In the end, I would like to collect all mailbox names in a variable called e.g. $mailboxnames and create some kind of a 'for next' loop, to create a single PST-file for every mailbox found in a specific Exchange mailbox database that runs automatically.

My limited experience ended in a PowerShell script, that does not end the VBR Exchange item restore session with ...

Stop-VBRExchangeItemRestoreSession

When I run the following example script ...

$credentials
$restorepoint = Get-VBRApplicationRestorePoint -Exchange
Start-VBRExchangeItemRestoreSession -RestorePoint $restorepoint[0]
$session = Get-VBRExchangeItemRestoreSession
$database = Get-VEXDatabase -Session $session -Name "Test-DB.edb"
$expmailbox = Get-VEXMailbox -Database $database -Name "Test-Mailbox"
Export-VEXItem -Mailbox $expmailbox -To "D:\RESTORE\Test-Mailbox.pst"

... credentials are queried, the most recent restore point of the Exchange 2010 server is opened, an Exchange item restore session is opened and the mailbox database Test-DB.edb is selected, the rest of the script doesn't work and the script returns to the command line.

When I try to manually close the VBR Exchange item restore session with ...

Stop-VBRExchangeItemRestoreSession

... command, PowerShell tells me the session is already closed, but an open session can still be seen in the VBR GUI. Cancelling the session tries to stop the session and finally ends with an orphaned session minutes later and directories that cannot be easily deleted under C:\VeeamFLR\ directory.

I will try to find more infos in the beginners guide, but maybe someone has more knowledge and can help in this case to reach my goal.

Best regards,
Didi7
Using the most recent Veeam B&R in 19 different environments now and counting!

Didi7
Expert
Posts: 349
Liked: 37 times
Joined: Oct 17, 2014 8:09 am
Contact:

Re: Export all Exchange mailboxes of a specific database each in a single PST via PowerShell ...

Post by Didi7 » Jan 14, 2020 10:54 am

Hi everybody, I am still investigating, but made further progress. I managed to export single mailboxes to a pst file. I tried to find out, if the variable $? delivers a True or False, when the PST-file cannot be created. I did not stop the VBR Exchange item restore session and tried to do that manually with ...

Stop-VBRExchangeItemRestoreSession -Session $session[0]

... which normally works but in this case after using this command, PowerShell tells me ...

Cannot validate argument on parameter 'Session'. Session is closed

But it is not closed, as in the GUI there is still a running task. Cancelling the task doesn't work and probably results in an orphaned session and problems with subdirectories in C:\VeeamFLR that cannot be deleted easily.

Why does Stop-VBRExchangeItemRestoreSession tells me the session is already closed, when it's not and how can I solve this on the PowerShell command line.

Any hints?
Using the most recent Veeam B&R in 19 different environments now and counting!

chris.arceneaux
Veeam Software
Posts: 159
Liked: 70 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Contact:

Re: Export all Exchange mailboxes of a specific database each in a single PST via PowerShell ...

Post by chris.arceneaux » Jan 14, 2020 2:03 pm

Hi @Didi7,

First step would be to double-check the session you're trying to close matches up with the one you see in the console. The variable $session[0] is simply choosing the first session in the array. Depending on your code, that may or may not be the session you are trying to close.

Another option after you're unable to close the session you see active in the console is to retrieve a list of all active sessions using Get-VBRExchangeItemRestoreSession. This would allow you to get a current list of sessions where you could then choose the session you want to close.

Didi7
Expert
Posts: 349
Liked: 37 times
Joined: Oct 17, 2014 8:09 am
Contact:

Re: Export all Exchange mailboxes of a specific database each in a single PST via PowerShell ...

Post by Didi7 » Jan 14, 2020 2:21 pm

Hello Chris,

yes $session[0] is the first session in the array. On my VBR Test-Server, there is only one Exchange server with one restore point, so it must be the first session in the array, but I could'nt close the session via PowerShell command line and decided to revert the VMware snapshot of my VBR Test-Server to clean C:\VeeamFLR. Next time, I will try to find out the current list of seesions with ...

Get-VBRExchangeItremRestoreSession

My guess is, that the list will be empty, as the session was always closed in this case, allthough the command Stop-VBRExchangeItemRestoreSession was not part of the PowerShell-script, instead stop.ps1 includes the command, so that I can decide, when to close the session myself.

I am sure, I will run into the same behavior again and will post the result.

I am close to finish the script. As soon, as I have more infos, I may post the result for other users and possible improvements.

Regards,
Didi7
Using the most recent Veeam B&R in 19 different environments now and counting!

chris.arceneaux
Veeam Software
Posts: 159
Liked: 70 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Contact:

Re: Export all Exchange mailboxes of a specific database each in a single PST via PowerShell ...

Post by chris.arceneaux » Jan 14, 2020 3:58 pm

Thanks for the follow-up! If able, please share your code with the community as it will be appreciated by many. You can share your code here as well as VeeamHub.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests