Need some help in modifying PowerShell to Export mailbox?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Need some help in modifying PowerShell to Export mailbox?

Post by ITEngineer »

Hi All,

I need some help in modifying the PowerShell script to export user mailbox as .PST from input file below:

The problem is that the input file is from the HR system as First Last name.
How to write the logs to .CSV file when the export failed?

Code: Select all

#Import remote session with exchange on my laptop where Outlook is installed
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDMAIL01-V/Powershell/ -Authentication Kerberos
Import-PSSession $Session
$ServerBackupUNCPath = "\\FILESERVER\OST\"
$CSVPath = "C:\LOGS\users.csv"
try{
$AllMailboxes = Import-CSV $CSVPath
foreach ($mbx in $AllMailboxes) { New-MailboxExportRequest -Mailbox $mbx -FilePath "$ServerBackupUNCPath$($mbx.Alias).pst" -baditemlimit
 50 -acceptlargedataloss; while ((Get-MailboxExportRequest -mailbox $i | Where-Object { $_.Status -eq "Queued" -or $_.Status -eq "InProgress" })) { Start-Sleep 180 } }
Write-Host
 "Processing...........$mbx.Alias................" -ForegroundColor Green
}
catch{
Write-Host "Exception has occured processing....$mbx.Alias...." -ForegroundColor Red
$_.Exception.Message
# Export the list of the failed mailboxes to export
Export-Csv -Path C:\LOGS\FailedExport.CSV -NoTypeInformation
}
Any help would be greatly appreciated.

Thanks
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need some help in modifying PowerShell to Export mailbox?

Post by jvierra »

Please try to learn how to correctly format your code. It will help us to read it and it will help you to see the more obvious mistakes.

Code: Select all

$ServerBackupUNCPath = '\\FILESERVER\OST\'
$CSVPath = 'C:\LOGS\users.csv'

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDMAIL01-V/Powershell/ -Authentication Kerberos
Import-PSSession $Session

$AllMailboxes = Import-CSV $CSVPath
foreach ($item in $AllMailboxes) {
    Write-Host "Processing...........$mbx.Alias................" -ForegroundColor Green
    $mbx = Get-Mailbox $item.Alias
    New-MailboxExportRequest -Mailbox $mbx -FilePath "$ServerBackupUNCPath$($mbx.Alias).pst" -baditemlimit 50 -acceptlargedataloss 
    while($true){
        if(Get-MailboxExportRequest -mailbox $mbx | 
            Where-Object { $_.Status -match 'Queued|InProgress'}){
        }else{
            break
        }
        Start-Sleep 180 
    }
}
It would actually be best if you send all export requests at once and assign them to a batch then just monitor the batch for completion.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Need some help in modifying PowerShell to Export mailbox?

Post by ITEngineer »

Hi All,

Thanks for the correction and the suggestion.

Since the Get-Mailbox accept SMTPAddress, so how to combine the script below which exports the list of SMTP email address from the Input file.

Code: Select all

$Users = Get-Content -Path "C:\TEMP\Input.CSV"
&{
	foreach ($User in $Users) {
		Write-Host "Processing.... $User"
		Get-Mailbox $User.ToString() | Select WindowsEmailAddress 
	} 
}| Export-Csv -Path "C:\TEMP\Output.csv" -NoTypeInformation
with the initial script?

Code: Select all

$ServerBackupUNCPath = '\\PRODFS01-VM\PST\'
$CSVPath = 'C:\LOGS\users.csv'

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDEXC05-VM/Powershell/ -Authentication Kerberos
Import-PSSession $Session

$AllMailboxes = Import-CSV $CSVPath
foreach ($item in $AllMailboxes) {
    Write-Host "Processing...........$mbx.Alias................" -ForegroundColor Green
    $mbx = Get-Mailbox $item.Alias
    New-MailboxExportRequest -Mailbox $mbx -FilePath "$ServerBackupUNCPath$($mbx.Alias).pst" -baditemlimit 50 -acceptlargedataloss 
    while($true){
        if(Get-MailboxExportRequest -mailbox $mbx | Where-Object {$_.Status -match 'Queued|InProgress'}){
        }else{
			# Write the log for which mailbox is error or cannot be exported:
			(Get-MailboxExportRequest -Identity $mbx | (Get-MailboxExportRequestStatistics -IncludeReport).Report > "C:\Logs\($mbx.Alias).log"
			
            break
        }
        Start-Sleep 180 
    }
}
Thanks in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need some help in modifying PowerShell to Export mailbox?

Post by jvierra »

We use "Import-CSv" to load CSV files. We then use the column name to access specific fields in a Csv.

Code: Select all

$users = Import-Csv C:\TEMP\Input.CSV
$emails = foreach ($user in $users) {
	Write-Host 'Processing....' $User.Alias
	Get-Mailbox $User.Alias
} 
$emails |  Select WindowsEmailAddress | Export-Csv C:\TEMP\Output.csv -NoTypeInformation
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Need some help in modifying PowerShell to Export mailbox?

Post by ITEngineer »

jvierra wrote: Thu Aug 16, 2018 7:30 am We use "Import-CSv" to load CSV files. We then use the column name to access specific fields in a Csv.

Code: Select all

$users = Import-Csv C:\TEMP\Input.CSV
$emails = foreach ($user in $users) {
	Write-Host 'Processing....' $User.Alias
	Get-Mailbox $User.Alias
} 
$emails |  Select WindowsEmailAddress | Export-Csv C:\TEMP\Output.csv -NoTypeInformation
Hi Mr. Vierra,

Thanks for the first draft of the code section like below:

Code: Select all

$InputCSVPath = 'C:\TEMP\Input.csv'
$ExportCSVPath = 'C:\TEMP\Output.csv'

$Users = Import-Csv -Path $InputCSVPath
$Emails = ForEach ($User in $Users) {
	Get-Mailbox $User.Alias
	Write-Host 'Processing....' $User.Alias
} 
$Emails | Select WindowsEmailAddress | Export-Csv $ExportCSVPath -NoTypeInformation
ii $ExportCSVPath
However, the result is all user email address in the system, not the few select First Last name people in the list?


and the console output is:
Processing....
WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items returned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual number of items, returning all items can take a long time and consume a large amount of memory. Also, we don't recommend storing the results in a variable. Instead, pipe the results to another task or script to perform batch changes.
Processing....
WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items returned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual number of items, returning all items can take a long time and consume a large amount of memory. Also, we don't recommend storing the results in a variable. Instead, pipe the results to another task or script to perform batch changes.
Processing....
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need some help in modifying PowerShell to Export mailbox?

Post by jvierra »

What is in your Csv. First you have to understand how the CmdLets work. It appears that you are just blindly throwing code at your problem.

Do some research on what a CSV is and how it is used in PowerShell. A good definition of a Csv is as follows:

https://en.wikipedia.org/wiki/Comma-separated_values

With this in mind look at you code and how it works.

"Firstname" and "Lastname" are not identifiers in Exchange/O365. You will need to either have HR give you a correct file or decide how you are going to convert the file (employeeNumber/employeeId).

To get a mailbox you will need either UPN or alias.

See: https://docs.microsoft.com/en-us/powers ... xchange-ps

A mailbox search will be on the following:

CommonName (CN), DisplayName, FirstName, LastName, Alias

Look and read about ANR which is used extensively by AD, Exchange and other tools. Only CN and alias are unique identifiers.

With an understanding of the above you will either have a solution or a less ambiguous question.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked