0
votes

I am working on the below PowerShell script to allow input from .CSV file to allow bulk export as .PST

The script is below:

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

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

  Get-Content -Path $InputCSVPath |
  Get-MailBox | 
  % {
    Write-Host "Processing .... $($_.Name) ..." -ForegroundColor Green

    # Check if the file already exist or not
    if ( !(Test-Path -Path "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST" -PathType Leaf) ) {
        #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 $_.EmailAddress -FilePath "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST" -BadItemLimit 50 -AcceptLargeDataLoss -WhatIf
        # wait until error or processed:
        while ( ($req = Get-MailboxExportRequest $_.EmailAddress) | ? { $_.Status -match 'Queued|InProgress' } )
        { Start-Sleep 180 } 
        $req | Get-MailboxExportRequestStatistics -IncludeReport | Select-Expand Report | Out-File "C:\Logs\$($_.Alias).log"
    } else {
        Write-Host "The user $($_.Alias) with file $($_.PrimarySmtpAddress).PST is already existing" -f Orange
        "user $($_.Alias) with file $($_.PrimarySmtpAddress).PST" | Out-File -Append $ExportExistsCSVPath
        # This doesn't make sense, no stats available!
        # Get-MailboxExportRequestStatistics -IncludeReport | Select -Expand Report | Out-File "C:\Logs\$($_.Alias).log"
    }

    # I assume, whatever line I put here will be executed regardless of any of the condition above is met or not
    Remove-Mailbox -Identity $_.EmailAddress -Confirm $false -WhatIf
    Write-Host "Just for testing: Removing Mailbox $($_.PrimarySmtpAddress)" -ForegroundColor Red
  }

The input file content:

[email protected]
[email protected]

The error message is like in the below lines from the PowerShell console:

Processing .... Helpdesk ...
Cannot process argument transformation on parameter 'Mailbox'. Cannot convert value "Helpdesk" to type "Microsoft.Exchange.Configuration.Tasks.MailboxOrMailUserIdParameter". 
Error: "Cannot convert the "Helpdesk" value of type "Deserialized.Microsoft.Exchange.Data.Directory.Management.Mailbox" to type 
"Microsoft.Exchange.Configuration.Tasks.MailboxOrMailUserIdParameter"."
    + CategoryInfo          : InvalidData: (:) [New-MailboxExportRequest], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-MailboxExportRequest
    + PSComputerName        : PRDEXC01-VM

Cannot process argument transformation on parameter 'Identity'. Cannot convert value "Helpdesk" to type 
"Microsoft.Exchange.MailboxReplicationService.MailboxExportRequestIdParameter". Error: "Cannot convert the "Helpdesk" value of type 
"Deserialized.Microsoft.Exchange.Data.Directory.Management.Mailbox" to type "Microsoft.Exchange.MailboxReplicationService.MailboxExportRequestIdParameter"."
    + CategoryInfo          : InvalidData: (:) [Get-MailboxExportRequest], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-MailboxExportRequest
    + PSComputerName        : PRDEXC01-VM

Adding the header called EmailAddress, then I got this error:

The operation couldn't be performed because object 'EmailAddress' couldn't be found on 'PRDDC07-VM.domain.com'.
    + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=PRDEXC02-VM,RequestId=e03f8373-b637-4ab9-9514-f1f340739ab1,TimeStamp=26/09/2018 4:12:45 AM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 321AF5C3,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
    + PSComputerName        : PRDEXC02-VM

Processing .... Corporate Help Desk ...
Cannot validate argument on parameter 'Mailbox'. The argument is null. Provide a valid value for the argument, and then try running the command again.
    + CategoryInfo          : InvalidData: (:) [New-MailboxExportRequest], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,New-MailboxExportRequest
    + PSComputerName        : PRDEXC02-VM

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: (:PSObject) [Get-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Get-Mailbox
    + PSComputerName        : PRDEXC02-VM

% : The term 'Select-Expand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:11 char:3
+   % {
+   ~~~
    + CategoryInfo          : ObjectNotFound: (Select-Expand:String) [ForEach-Object], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.ForEachObjectCommand

How can I correct the above PowerShell code?

1

1 Answers

1
votes

Please see changes below. 'EmailAddress' is same as Proxy Addresses in AD, there could be several of these listed, each mailbox will only have one (1) 'PrimarySmtpAddress'. Have also added the '-Name' field on the New-MailboxExport, this will allow you to find the Mailbox Export easier.

This is listed on the 
  New-MailboxExportRequest -Mailbox $_.EmailAddress
  Get-MailboxExportRequest $_.EmailAddress
  Remove-Mailbox -Identity $_.EmailAddress

Should be
  New-MailboxExportRequest -Name $_.PrimarySmtpAddress -Mailbox $_.PrimarySmtpAddress
  Get-MailboxExportRequest $_.PrimarySmtpAddress
  Remove-Mailbox -Identity $_.PrimarySmtpAddress