1

我读过当您将对象传递到名为 with 的脚本块时,它们会被序列化start-job。这似乎适用于字符串和事物,但我正在尝试传递一个xml.XmlElement对象。我确定该对象XMLElement在我调用脚本块之前是一个,但在工作中,我收到此错误:

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
4

1 回答 1

4

显然,您无法将 XML 节点传递到脚本块中,因为您无法将它们序列化。根据这个答案,您需要将节点包装到一个新的 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
于 2013-08-04T09:50:59.237 回答