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

Ask your Windows 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.
User avatar
ITEngineer
Posts: 95
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Aug 21, 2018 7:36 am

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 */

User avatar
jvierra
Posts: 12899
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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} |

User avatar
ITEngineer
Posts: 95
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Aug 21, 2018 7:48 am

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: 95
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Aug 21, 2018 8:05 am

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 */

User avatar
jvierra
Posts: 12899
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Aug 21, 2018 8:07 am

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

User avatar
jvierra
Posts: 12899
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Aug 21, 2018 8:50 am

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: 95
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Aug 21, 2018 10:48 pm

Yes, many thanks for the clarification and the assistance in this matter Mr. Vierra.
:D
/* IT Engineer */

Locked