2013-08-04 42 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