【问题标题】:Error in Exchange PowerShell to export mailbox from inputfileExchange PowerShell 从输入文件导出邮箱时出错
【发布时间】:2019-02-25 14:41:45
【问题描述】:

我正在处理以下 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 代码?

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • @Dave,在包含电子邮件地址的 .CSV 文件中添加名为 EmailAddress 的标头后,仍然像上面更新的错误消息一样错误。
    猜你喜欢
    • 2017-08-01
    • 2014-02-28
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多