【发布时间】: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