PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

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

Re: PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

Post by ITEngineer »

jvierra wrote: Mon Aug 20, 2018 11:53 pm Its a CSV. You have to reference it correctly. We've done this many times before.

Import-Csv -Path $ExportCSVPath |
Get-MailBox $_.PrimarySmtpAddress |
Hi Mr. Vierra,

Yes I have tried that as well, but the error remains the same:

The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
+ CategoryInfo : InvalidArgument: (@{PrimarySmtpAd...@Domain.com}:PSObject) [Get-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Get-Mailbox
+ PSComputerName : PRDEX03-VM.Domain.com
/* 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: PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

Post by jvierra »

Sorry. It should be like thi:

Code: Select all

Import-Csv -Path $ExportCSVPath |
      ForEach-Object{Get-MailBox $_.PrimarySmtpAddress} |
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

Post by ITEngineer »

This is my updated script as per your suggestion:

Code: Select all

$Server = 'PRDFS01-VM'
$ServerBackupUNCPath = "\\$Server\PST"
$InputCSVPath = 'C:\LOGS\Input.csv'
$ExportCSVPath = 'C:\LOGS\Output.csv'
$ExportExistsCSVPath = 'C:\LOGS\Exist.csv'

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

$Users = Get-Content -Path $InputCSVPath
&{
	foreach ($User in $Users) {
		Write-Host "Processing.... $User"
		Get-Mailbox $User.ToString() | Select PrimarySmtpAddress 
	} 
} | Export-Csv -Path $ExportCSVPath -NoTypeInformation

Import-Csv -Path $ExportCSVPath |
  ForEach-Object{Get-MailBox $_.PrimarySmtpAddress} | 
  % {
    Write-Host "Processing .... $($_.Name) ..." -ForegroundColor Green
	
	# Check if the file already exist or not
	$FileResult = Test-Path -Path "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST" -PathType Leaf
	
	if ( $FileResult -ne $True ) {
		#If there is no exported .PST file on the destination folder, then begin the export mailbox command and log if there any error to the AliasName.LOG file:
	    New-MailboxExportRequest -Mailbox $_ -FilePath "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST" -BadItemLimit 50 -AcceptLargeDataLoss -WhatIf
	} else {
		Write-Output "The user $($_.PrimarySmtpAddress).PST file is already existing"
		#Append the list of already existing list of users C:\LOGS\Exist.csv
		Get-MailboxExportRequestStatistics -IncludeReport | Select -Expand Report | Out-File "C:\LOGS\$($_.PrimarySmtpAddress).log"
	}
	
	# I assume, whetever line I put here will be executed regardless of any of the condition above is met or not
	#Remove-Mailbox -Identity $_ -Confirm $false -WhatIf
	Write-Host "Removing Mailbox $($_.PrimarySmtpAddress)" -ForegroundColor Red
  }
Now the error is now on this line of code:

Get-MailboxExportRequestStatistics -IncludeReport | Select -Expand Report | Out-File "C:\LOGS\$($_.PrimarySmtpAddress).log"
/* IT Engineer */
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

Post by ITEngineer »

jvierra wrote: Tue Aug 21, 2018 7:40 am Sorry. It should be like thi:

Code: Select all

Import-Csv -Path $ExportCSVPath |
      ForEach-Object{Get-MailBox $_.PrimarySmtpAddress} |
Many thanks for the suggestion Mr. Vierra,

it is progressing well, however it is now stuck with the below error:
The user First.Last@domain.com.PST file is already existing cmdlet Get-MailboxExportRequestStatistics at command pipeline position 1
Supply values for the following parameters:
Identity:
looks like it is stuck in the line to export the report when the user already exists: Get-MailboxExportRequestStatistics -IncludeReport | Select -Expand Report | Out-File "C:\LOGS\$($_.PrimarySmtpAddress).log"
/* 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: PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

Post by jvierra »

You have to stop using this &{} and learn how to use the ForEach-Object construct to manage the pipeline.

I recommend going here to sort out you understanding of how this works: https://mva.microsoft.com/en-us/trainin ... shell-8276
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

Post by jvierra »

Here is an example to help you along:

Code: Select all

Import-Csv -Path $ExportCSVPath |
    ForEach-Object{
	
        # Check if the file already exist or not
        if(Test-Path -Path "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST"){
               Write-Output "The user $($_.PrimarySmtpAddress).PST file is already existing"
               #Append the list of already existing list of users C:\LOGS\Exist.csv
               Get-MailboxExportRequestStatistics $_.PrimarySmtpAddress -IncludeReport | 
               Select -Expand Report | 
               Out-File "C:\LOGS\$($_.PrimarySmtpAddress).log"
        } else {
               #If there is no exported .PST file on the destination folder, then begin the export mailbox command and log if there any error to the AliasName.LOG file:
              New-MailboxExportRequest $_.PrimarySmtpAddress -FilePath "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST" -BadItemLimit 50 -AcceptLargeDataLoss -WhatIf
        }
	
    }
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: PowerShell to check file exist in the destination folder before exporting the Exchange email as .PST?

Post by ITEngineer »

Yes, many thanks for the clarification and the assistance in this matter Mr. Vierra.
:D
/* IT Engineer */
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