PowerShell script exchange
Post Reply
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 » 1 person likes this post

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 many different environments now and counting!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

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 many different environments now and counting!
chris.arceneaux
VeeaMVP
Posts: 667
Liked: 358 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Georgia, USA
Contact:

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

Post by chris.arceneaux » 1 person likes this post

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
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

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 many different environments now and counting!
chris.arceneaux
VeeaMVP
Posts: 667
Liked: 358 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Georgia, USA
Contact:

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

Post by chris.arceneaux » 1 person likes this post

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.
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 » 2 people like this post

Hi everybody,

things develop further. Not much time is available to progress faster. I am currently in the final steps to finish and polish the PowerShell script, which will be usable in very different scenarios.

Stay tuned 8)

Regards,
Didi7
Using the most recent Veeam B&R in many different environments now and counting!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Hi everybody,

currently I am trying to find out how to check if PST-Export via ...

Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath

... failed or was successful? Is something like this possible ...

$export_result = Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath

... and check if $export_result has the value False or True?

Anyone?

Regards,
Didi7
Using the most recent Veeam B&R in many different environments now and counting!
chris.arceneaux
VeeaMVP
Posts: 667
Liked: 358 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Georgia, USA
Contact:

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

Post by chris.arceneaux »

You can determine success/fail using standard PowerShell error handling methods. Here are a couple examples:

1) If you want the script to stop running if Export-VEXItem fails. Notice the ErrorAction flag.

Code: Select all

Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath -ErrorAction Stop
2) Use try/catch blocks to handle the error

Code: Select all

Try
{
	Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath
}
Catch
{
	# whatever code you want to handle error here. could be that you retry the export a second time...
	throw "PST export failed"
}
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Hi chris.arceneaux,

first of all thanks for replying so fast. If I unterstand it correctly, I need to add the parameter ...

-ErrorAction

... and I cannot return the result of Export-VEXItem into a variable with ...

$result = Export-VEXItem ...

But how do I work with ErrorAction here? Do you have another example? I would like to query a variable each time I use the Export-VEXItem cmdlet, so that I can count the number of failed and successful PST exports.

I don't want to stop the script but continue exporting the next mailbox of a database with Export-VEXItem. In every Exchange mailbox there is one system or administrative mailbox, which name can't be used as a filename, so the export fails.

Any idea?

Regards,
Didi7
Using the most recent Veeam B&R in many different environments now and counting!
jhoughes
Veeam Vanguard
Posts: 279
Liked: 112 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

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

Post by jhoughes »

The information that you are looking for is really just basic PowerShell and understanding exceptions and handling them.

I'd suggest reading more here: https://powershellexplained.com/2017-04 ... d-to-know/
Husband, Father, Solutions Architect, Geek Extraordinaire | @DenverVMUG, @AustinVMUG & @ATXPowerShell leader | VMware vExpert | Cisco Champion
chris.arceneaux
VeeaMVP
Posts: 667
Liked: 358 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Georgia, USA
Contact:

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

Post by chris.arceneaux »

Didi7 wrote:$result = Export-VEXItem
This command simply saves the output of Export-VEXItem to the variable $result. If the command fails, the $result variable will be null/empty.

PowerShell error handling is how to determine if a command has failed. As you said you don't want to hard stop if Export-VEXItem fails, here's some sample code more specific to what you're trying to do:

Code: Select all

$success = 0
$fail = 0
# Loop through mailboxes
foreach ($mailbox in $mailboxes)
{
  Try
  {
      Export-VEXItem -Mailbox $mailbox -To $exportpath
      $success++  # SUCCESS - increment by 1
  }
  Catch
  {
      $fail++  # FAILURE - increment by 1
      # you could also add code here to capture more info on the mailbox that failed
  }
} # end foreach loop
I also recommend reading the link that @jhoughes posted for more info. :D
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Ok guys, just to be sure I understand what you propose ...

I need to use 'Try' and 'Catch' to find out, if the Export-VEXItem command succeeded or failed?

You said the value of $result will be null/empty, if the command fails. What is the value of $result, if the command succeeds? I thought I could simply check for $False or $True, but that didn't work.

Regards,
Didi7
Using the most recent Veeam B&R in many different environments now and counting!
chris.arceneaux
VeeaMVP
Posts: 667
Liked: 358 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Georgia, USA
Contact:

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

Post by chris.arceneaux »

Didi7 wrote: What is the value of $result, if the command succeeds?
The value of $result in this scenario will be whatever the output of Export-VEXItem is as the command succeeded.
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Ok, in this case I just need to check if the value of $result is not equal Null/empty? I am little bit confused at the moment. Of course I will have a look at 'Try' and 'Catch' alternatively and have a read of ...

https://powershellexplained.com/2017-04 ... d-to-know/

Regards,
Didi7

P.S.:
The script to mass export all mailboxes of a specific Exchange database and a specific restore point and a specific Exchange server reached release candidate status, unfortunately I still have less time to work on it but the final result will hopefully please a lot of guys here.
Using the most recent Veeam B&R in many different environments now and counting!
chris.arceneaux
VeeaMVP
Posts: 667
Liked: 358 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Georgia, USA
Contact:

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

Post by chris.arceneaux »

Didi7 wrote: Ok, in this case I just need to check if the value of $result is not equal Null/empty?
This will work as well. Key thing to keep in mind if you go this route is you need to clear out the $result variable at the beginning of each loop (see code below). Otherwise, the variable still contains the value from the previous execution.

Code: Select all

$result = $null
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Hi chris.arceneaux,

key thing for me is, did it work (count +1 in a variable like succeeded) or did it not work (count +1 in a variable like failed). That's important for me to present a final summary screen of the mass PST export.

Will test that in a couple of hours and that finally brings me a lot further.

Thanks for pointing out to to clear out the variable at the beginning of each loop.

Everything else is working very good already, though I need to strip debug info, once everything is implemented and add some error query routines.

Regards,
Didi7
Using the most recent Veeam B&R in many different environments now and counting!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Something is still fishy here ...

$result = $null
$result = Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath

Even if the export to a pst-file is successful, the value is always $null, when I check with ...

If ($result -ne $null) {
$succeeded++
} Else {
$failed++
}

Maybe I am forced to use the -ErrorAction stuff?

Regards,
Didi7
Using the most recent Veeam B&R in many different environments now and counting!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Btw ...

Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath -ErrorAction SilentlyContinue

If the PST-export fails (maybe because the $exportpath contains characters, that are not allowed in a path), the error is presented in red, allthough I added -ErrorAction SilentlyContinue
Using the most recent Veeam B&R in many different environments now and counting!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Furthermore, I believe Try and Catch is worthless in this case, as an unsuccessful PST export, e.g. because the value in $exportpath is invalid, doesn't automatically stop the script, instead a red error is displayed and the script continues.

So far, I still have no idea how to find out, if Export-VEXItem cmdlet was successful or failed ...
Using the most recent Veeam B&R in many different environments now and counting!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Ok, I got it. For some reason (reading some urls) I used $error as the variable to use with Export-VEXItem and that completely failed ...

The following example works ...

$result = $null
$result = Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath

If ($null -ne $result) {
$failed++
}

The only thing that bugs me is the fact, that if I use '-ErrorAction SilentlyContinue' at the end of the cmdlet, a red error is still thrown out!

P.S.:
$null is a scalar and therefore needs to be on the left side of the equality comparison
Using the most recent Veeam B&R in many different environments now and counting!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Correction, only this works ...

$result = ""
$result = Export-VEXItem -Mailbox $mailboxes[$mailboxnumber] -To $exportpath

If ($null -ne $result) {
$failed++
}
Using the most recent Veeam B&R in many different environments now and counting!
pstextractor
Novice
Posts: 7
Liked: 3 times
Joined: Aug 02, 2022 7:36 pm
Contact:

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

Post by pstextractor »

Hey does anyone have a full script that does this?

I have an old veeam backup of an old exchange server. Management doesnt want it all sitting in veeam (dont ask, i did argue the point). They are more comfortable with having the data extracted to PST files. There are 500+ mailboxes and there is no way i am manually doing that.

So maybe someone has a script to extract all those PST files. Then i can archive them in the way management wants.
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Hi pstextractor (what a smart name :D ),

the script is currently in RC1 phase and on hold since Mid 2020 and still not ready for a public release.

Regards,
Didi7
Using the most recent Veeam B&R in many different environments now and counting!
pstextractor
Novice
Posts: 7
Liked: 3 times
Joined: Aug 02, 2022 7:36 pm
Contact:

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

Post by pstextractor »

I will beta test your script if you want. I mean i dont have anything to lose. It either makes the files or it doesnt right?

i had an old forums account but its locked to my old company so i made a new one.
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 » 1 person likes this post

Hello pstextractor,

the script works perfectly in different environments (means with VBR 9.x or higher and Exchange 2010 or higher and on different OSs), but it's not ready for public release.

Thanks
Using the most recent Veeam B&R in many different environments now and counting!
pstextractor
Novice
Posts: 7
Liked: 3 times
Joined: Aug 02, 2022 7:36 pm
Contact:

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

Post by pstextractor » 1 person likes this post

I dont get why you even responded then if you are just blowing smoke. Its a hard problem, so i guess i understand why you cant solve it for two years. No worries!
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 »

Quote:
I dont get why you even responded then if you are just blowing smoke

**********************************************************************************************

You have absolutely no idea, why it's still not released to public, pure speculation from your side. Development began in the beginning of 2020 and finished in the beginning of April 2020. I had to learn PowerShell programming first and then really started to understand, how I can use it in combination with Veeam's PowerShell cmdlets (it's none of my regular business). In the first place, it was done for my company and for nobody else. It worked very well and I intended to clean up the code and add some more error query routines but it ended up in no more available time (remember, none of my regular business).

Feel free to code your powershell script for yourself, if you cannot wait for a possible public release. Sorry, enough said for the time being.

Good luck!
Using the most recent Veeam B&R in many different environments now and counting!
albertwt
Veeam Legend
Posts: 879
Liked: 46 times
Joined: Nov 05, 2009 12:24 pm
Location: Sydney, NSW
Contact:

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

Post by albertwt »

Didi7 wrote: Aug 17, 2022 1:05 pm Hello pstextractor,

the script works perfectly in different environments (means with VBR 9.x or higher and Exchange 2010 or higher and on different OSs), but it's not ready for public release.

Thanks
Will it work for Exchange 2016 or even Exchange Online ?
--
/* Veeam software enthusiast user & supporter ! */
Didi7
Veteran
Posts: 490
Liked: 59 times
Joined: Oct 17, 2014 8:09 am
Location: Hypervisor
Contact:

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

Post by Didi7 » 1 person likes this post

Hello albertwt,

it perfectly works for Exchange 2010 (and iirc also Exchange 2016) as I could successfully mass export mailboxes into single PST files. System mailboxes were skipped, as the name of the mailbox is also the name of the PST file and the System mailbox has an invalid name, that cannot be used as a filename. In this case, an additional routine would be necessary to capture the name and check, if the filename of the PST would be valid and if not, it would need to be renamed somehow, but that's pure luxury.

Nevertheless, it should work for all onprem-versions of Exchange beyond 2010, but wasn't tested so far. It was tested with different Veeam B&R versions though but not with Veeam B&R 11.x, as the project is on hold since 2020.

If our company should go with Veeam Backup for Microsoft 365, I probably might adjust the script with the corresponding PowerShell cmdlets for that product as well, as they are different, but no guarantees at all, as this is a spare time project and it is on hold currently.

Sorry, too much debug information and too much routines to capture errors are still necessary and I might need to learn PowerShell again ;)

Best of luck so far.
Using the most recent Veeam B&R in many different environments now and counting!
pstextractor
Novice
Posts: 7
Liked: 3 times
Joined: Aug 02, 2022 7:36 pm
Contact:

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

Post by pstextractor » 1 person likes this post

The margins of this thread are clearly too small to contain his awesome script that totally exists.
Post Reply

Who is online

Users browsing this forum: Baidu [Spider] and 17 guests