2016-01-07 120 views
0

我一直在打架一個愚蠢的問題,當我嘗試在特定命令中使用變量時出現錯誤,但是如果我使用實際變量值,它會起作用。爲什麼我不能在這裏使用變量以及解決方法是什麼?請幫助。變量都具有正確的值。我確認一切正常,直到執行最後一行...命令拋出錯誤中的變量

(此代碼中的最後一行拋出錯誤......)

##################################################################### 
# Prompt for user input regarding deleted and destination mailboxes # 
##################################################################### 

# Ask for Deleted Mailbox SMTP Address 
$DeletedMailbox = Read-Host "Enter Primary Address of Deleted Mailbox: " 

# Ask for Destination Mailbox SMTP Address 
$DestinationMailbox = Read-Host "Enter Primary Address of Destination Recovery Mailbox: " 

############################################################# 
# Query for the Exchange GUID information of both Mailboxes # 
############################################################# 
$DeletedGUID = Get-Mailbox -SoftDeletedMailbox -Identity $DeletedMailbox | 
       Select ExchangeGUID | FT -HideTableHeaders 

$DestinationGUID = Get-Mailbox -Identity $DestinationMailbox | 
        Select ExchangeGUID | FT -HideTableHeaders 

############################################################################ 
# Initiate the Restore Request from Deleted mailbox to destination mailbox # 
############################################################################ 
New-MailboxRestoreRequest -SourceMailbox $DeletedGUID -TargetMailbox $DestinationGUID -AllowLegacyDNMismatch 

錯誤就拋出....

Cannot process argument transformation on parameter 'SourceMailbox'. Cannot convert 
the "System.Collections.ArrayList" value of type "System.Collections.ArrayList" 
to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". 
    + CategoryInfo   : InvalidData: (:) [New-MailboxRestoreRequest], ParameterBindin...mationException 
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,New- MailboxRestoreRequest 
    + PSComputerName  : ps.outlook.com

回答

2

顯然$DeletedGUID是一個列表,而不是一個單一的元素。在一個循環中

New-MailboxRestoreRequest -SourceMailbox $DeletedGUID[0] ... 

或運行New-MailboxRestoreRequest:無論是從列表中選擇一個元素

$DeletedGUID | ForEach-Object { 
    New-MailboxRestoreRequest -SourceMailbox $_ ... 
} 

此外,取代

...| Select ExchangeGUID | FT -HideTableHeaders 

Select -Expand ExchangeGUID 

Format-* cmdlet用於輸出格式化,而不是用於進一步處理的內容。

+0

您需要將@ Ansgar的[0]建議應用於您的兩個GUID變量調用。 –

+0

假設兩個'Get-Mailbox'語句產生多個結果,是的。另一個選擇是隻挑選其中一個結果('select -First 1'),所以變量首先不包含集合。 –

+0

可能不是在這種情況下,但我已經看到了Powershell將決定是創建一個數組還是根據結果將單個值填充到變量中的場景。在這些場景中,我通常強制返回結果到一個數組中,然後返回第一個值,如果我確定這總是我想要的......'@(Get-Mailbox -SoftDeletedMailbox -Identity $ DeletedMailbox | Select -Expand ExchangeGUID)[0]' –