Ошибка в Exchange PowerShell для экспорта почтового ящика из файла ввода - PullRequest
0 голосов
/ 21 сентября 2018

Я работаю над приведенным ниже сценарием 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 голосов
/ 21 сентября 2018

Пожалуйста, смотрите изменения ниже.«EmailAddress» такой же, как и у прокси-адресов в AD, их может быть несколько в списке, в каждом почтовом ящике будет только один (1) «PrimarySmtpAddress».Также добавлено поле '-Name' в New-MailboxExport, это позволит вам легче найти экспорт почтовых ящиков.

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
...