2013-03-23 95 views
0

我試圖從一臺服務器上運行的直流下面的腳本,我不斷收到錯誤的PowerShell調用命令

Cannot bind parameter 'Identity'. Cannot convert value "1" to type "Microsoft.ActiveDirectory.Management.ADComputer". Error: 
"Invalid cast from 'System.Char' to 'Microsoft.ActiveDirectory.Management.ADComputer'." 
    + CategoryInfo   : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException 
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADComputer 
    + PSComputerName  : dc-test.com 

Script代碼:

$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $password 
$list = gc c:\test.txt 
#example of what i would contain $i= Workstation1-"ou=test,dc=test,dc=com" 

foreach ($i in $list) 
{ 
    $s=$i.Split('-') 

    $ScriptBlock = { 
    param ($s) 
    Import-Module ActiveDirectory 

    get-adcomputer $s[0] | Move-ADObject -TargetPath $s[1] 
    } 

    invoke-command -computer dc.test.com -Argu $s -scriptblock $ScriptBlock -cred $Credentials 
} 
} 

,當我在DC運行它工作正常。有人能指引我朝着正確的方向嗎?

回答

0

這裏的問題是,您將array作爲參數傳遞給參數-ArgumentList。這不會像你期望的那樣工作。作爲一個整體傳遞數組,不是將數組的每個元素傳遞給給定的參數。只有一個,因此只使用傳遞數組的第一個元素。

要了解是怎麼回事試試這個:

$script = { 
    param ($array?) 
    $array?[0] 
} 

$array = 'a1-b2-c3'.Split('-') 

Invoke-Command -ScriptBlock $script -ArgumentList $array 
Invoke-Command -ScriptBlock $script -ArgumentList (,$array) 

所以你可以確保你的磁盤陣列不會(使用一元逗號)或者只是改變代碼,並假設你會得到兩個參數被銷燬另:

$ScriptBlock = { 
    param ($comp, $target) 
    Import-Module ActiveDirectory 
    get-adcomputer $comp | Move-ADObject -TargetPath $target 
} 

BTW:我懷疑可能存在的問題與當前TARGETPATH - 它會傳遞與引號cmdlet的,所以Move-ADObject可能會失敗。