将XmlElement传递给powershell中的后台作业
我已经读到,当您将对象传递到名为with start job的脚本块时,对象将被序列化。这对于字符串和其他东西似乎很好,但我正在尝试传入一个xml.xmlement对象。在调用脚本块之前,我确信该对象是一个XMLElement,但在作业中,我遇到以下错误:将XmlElement传递给powershell中的后台作业,powershell,Powershell,我已经读到,当您将对象传递到名为with start job的脚本块时,对象将被序列化。这对于字符串和其他东西似乎很好,但我正在尝试传入一个xml.xmlement对象。在调用脚本块之前,我确信该对象是一个XMLElement,但在作业中,我遇到以下错误: Cannot process argument transformation on parameter 'node'. Cannot convert the "[xml.XmlElement]System.Xml.XmlElement" va
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
显然,您无法将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
显然,您无法将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