0

我有一个脚本用于部署对供应商软件的升级。该脚本利用 xml 来映射不同的部署路径。我注意到这周在强制升级到 powershell 3 后停止工作。下面是一个小的演示脚本来说明这个问题:

$xml = [xml] (Get-Content (Get-Item (".\FooBar.xml")))
$foobar = $xml.Stuff.FooBars.Foobar 

$ScriptBlock = {        
    $foobar = $args[0]

    write-host "Inside the job..."
    write-host ("Foobar     : "+$foobar)
    write-host ("Foobar.Foo : "+$foobar.Foo)
    write-host ("Args       : "+$args)
}

write-host "Outside the job..."
write-host ("Foobar: "+$foobar)
write-host ("Foobar.Foo : "+$foobar.Foo)

Start-Job -ScriptBlock $ScriptBlock -Args $foobar | Out-Null        
While (Get-Job -State "Running") { Start-Sleep 2 }               

write-host ("Jobs Completed.")    
Get-Job | Receive-Job          
Remove-Job *      

我的xml:

<?xml version="1.0"?>
<Stuff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FooBars>
    <FooBar>
      <Foo>ThisIsAFoo</Foo>
      <Bar>ThisIsABar</Bar>
    </FooBar>
  </FooBars> 
</Stuff>

v2 上的输出:

Outside the job...
Foobar: System.Xml.XmlElement
Foobar.Foo : ThisIsAFoo
Jobs Completed.
Inside the job...
Foobar     : System.Collections.ArrayList System.Collections.ArrayList
Foobar.Foo : ThisIsAFoo
Args       : System.Collections.ArrayList

v3 上的输出:

Outside the job...
Foobar: System.Xml.XmlElement
Foobar.Foo : ThisIsAFoo
Jobs Completed.
Inside the job...
Foobar     : System.Collections.ArrayList System.Collections.ArrayList
Foobar.Foo : 
Args       : System.Collections.ArrayList

所以请注意,在 v2 Foobar.Foo 中可以确定,而在 v3 中则不能。有什么想法会导致这种情况以及最好的解决方法吗?范围问题?在我较大的脚本中,XmlElement 被传递给其他函数,因此我想避免将其转换为不同的对象或将其分解为其属性并单独发送它们。

4

1 回答 1

0

I just ran into the same issue. I haven't found the exact cause yet, but as a workaround, you can add the flag -PSVersion 2.0 to your start-job line.

于 2013-07-22T19:41:33.907 回答