我已经读过将对象传递到使用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
答案 0 :(得分:4)
显然,您无法将XML节点传递到脚本块中,因为您无法将它们序列化。根据{{3}},您需要将节点包装到新的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