2013-08-04 40 views
0

我讀過,當您將它們傳遞到腳本塊start-job調用時序列化對象。這似乎對字符串和東西很好,但我試圖通過一個xml.XmlElement對象。我確定該對象是XMLElement之前我調用腳本塊,但在作業中,我得到此錯誤:將XmlElement傳遞到PowerShell後臺作業

Cannot process argument transformation on parameter 'node'. Cannot convert the "[xml.XmlElement]System.Xml.XmlElement" 
value of type "System.String" to type "System.Xml.XmlElement". 
    + CategoryInfo   : InvalidData: (:) [], ParameterBindin...mationException 
    + FullyQualifiedErrorId : ParameterArgumentTransformationError 
    + PSComputerName  : localhost 

那麼,我該如何讓我的XmlElement回來。有任何想法嗎?

對於它的價值,這是我的代碼:

$job = start-job -Name $node.LocalName -InitializationScript $DEFS -ScriptBlock { 
    param (
     [xml.XmlElement]$node, 
     [string]$folder, 
     [string]$server, 
     [string]$user, 
     [string]$pass 
    ) 
    sleep -s $node.startTime 
    run-action $node $folder $server $user $pass 
} -ArgumentList $node, $folder, $server, $user, $pass 

回答

3

顯然,你不能傳遞XML節點插入腳本塊,因爲你不能序列化。根據this answer,您需要將節點包裝到新的XML文檔對象中,並將其傳遞到腳本塊中。因此類似這樣的東西可能會起作用:

$wrapper = New-Object System.Xml.XmlDocument 
$wrapper.AppendChild($wrapper.ImportNode($node, $true)) | Out-Null 

$job = Start-Job -Name $node.LocalName -InitializationScript $DEFS -ScriptBlock { 
    param (
    [xml]$xml, 
    [string]$folder, 
    [string]$server, 
    [string]$user, 
    [string]$pass 
) 
    $node = $xml.SelectSingleNode('/*') 
    sleep -s $node.startTime 
    run-action $node $folder $server $user $pass 
} -ArgumentList $wrapper, $folder, $server, $user, $pass