Exchange PowerShell中从输入文件导出邮箱的错误

时间:2018-09-21 08:17:01

标签: powershell scripting active-directory office365 exchange-server

我正在使用以下PowerShell脚本,以允许来自.CSV文件的输入,以允许批量导出为.PST

脚本如下:

$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
  }

输入文件内容:

Helpdesk@domain1.com
Support@domain2.org

错误消息类似于PowerShell控制台中的以下行:

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

添加名为 EmailAddress 的标头,然后出现此错误:

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

如何纠正上面的PowerShell代码?

1 个答案:

答案 0 :(得分:1)

请在下面查看更改。 “ EmailAddress”与AD中的代理地址相同,可能列出了多个,每个邮箱只有一个(1)“ PrimarySmtpAddress”。还已在New-MailboxExport上添加了“ -Name”字段,这将使您更轻松地找到Mailbox Export。

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