3

我正在使用 Powershell v3 在 Windows Server 2012 上测试我的应用程序的部署脚本。该脚本在 Win Server 2008 R2 和带有 Powershell v2 的 Win 7 上运行良好。我现在遇到的问题是我无法访问通过 -ArgumentList 传递的 XML 变量的属性。

我已经能够在一个简单的脚本中使用 Powershell v3 在 Win 7 和 Win Server 2012 上重现该问题,该脚本没有我的主脚本所具有的任何 SharePoint、IIS 和杂项。

脚本(我想我是从现在找不到的类似问题中借用的):

$xml = [xml] (Get-Content (Get-Item (".\input.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 "Outside the job..."
write-host ("Foobar: "+$foobar)
write-host ("Foobar.Foo : "+$foobar.Foo)

Start-Job -ScriptBlock $ScriptBlock -ArgumentList $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>

Powershell v2 的输出:

PS C:\Powershell3Issues> .\demo.ps1
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

Powershell v3 的输出:

PS C:\Powershell3Issues> .\demo.ps1
Outside the job...
Foobar: System.Xml.XmlElement
Foobar.Foo : ThisIsAFoo
Jobs Completed.
Inside the job...
Foobar     : System.Collections.ArrayList System.Collections.ArrayList
Foobar.Foo :

请注意缺少的 Foobar.Foo 值。

我也尝试过 v3 中的 $using 语法,但它做同样的事情。

4

3 回答 3

1

Try this to specify the version of PowerShell to run the job under:

Start-Job -ScriptBlock $ScriptBlock -ArgumentList $foobar -PSVersion 2.0
于 2013-03-22T20:48:30.153 回答
1

I'm using PS 3.0 and it does change data types. I modified your script to take a look:

$xml = [xml] (Get-Content .\input.xml)
$foobar = $xml.Stuff.FooBars.Foobar 
"Foobar Outside type = $($foobar.Gettype())"

$ScriptBlock = {        
    $foobar = $args[0]
    "Foobar Inside type = $($foobar.Gettype())"
}

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

The output I got was:

Foobar Outside type = System.Xml.XmlElement
Foobar Inside type = System.Collections.ArrayList System.Collections.ArrayList

I'll keep looking and see what I find.

Update:

When I run the script in PS 2.0 using the command powershell -version 2.0 I get an error saying:

Method invocation failed because [Deserialized.System.Xml.XmlElement] doesn't contain a method named 'Gettype'.
+ CategoryInfo          : InvalidOperation: (Gettype:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
+ PSComputerName        : localhost 

It changes from System.Xml.XmlElement to Deserialized.System.Xml.XmlElement, right? I'll keep looking and see what I find.

Workaround:

A workaround could be instead of passing an object just pass a string.

$xml_path = 'C:\input.xml'
$sb = {
    $xml = [xml](Get-Content $args[0])
    ...
}
Start-Job -ScriptBlock $sb -Args $xml_path

I'm done searching. My head hurts when I go in depth on things.

于 2013-03-22T20:49:05.140 回答
1

问题是您正在尝试序列化未序列化的 XMLElement 类型的对象。解决方法是克隆 XMLElement 并将其包装在新的 XMLDocument 中。

cls

$XMLDocument = [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>ThisIsAFoo1</Foo>
      <Bar>ThisIsABar2</Bar>
    </FooBar>
    <FooBar>
      <Foo>ThisIsAFoo2</Foo>
      <Bar>ThisIsABar2</Bar>
    </FooBar>
  </FooBars> 
</Stuff>
"@

Select-Xml -Xml $XMLDocument -XPath 'Stuff/FooBars/FooBar' | foreach {  
  $SelectedNode = $_.Node
  #$SelectedNode is now of type System.Xml.XmlElement which does not serialise
  #but System.Xml.XmlDocument will serialise so wrap a clone of $SelectedNode in a new XMLDocument 
  #then pass that as the argument
  $SerializationWrapper = New-Object System.Xml.XmlDocument  
  $SerializationWrapper.AppendChild($SerializationWrapper.ImportNode($SelectedNode, $true)) | Out-Null
  Start-Job -ScriptBlock {
    param($xml)
    Write-Output "Start Job"    
    'The type of deserialise object $xml is: ' + $xml.getType()    
    $sw = New-Object system.io.stringwriter 
    $writer = New-Object system.xml.xmltextwriter($sw) 
    $writer.Formatting = [System.xml.formatting]::Indented 
    $xml.WriteContentTo($writer) 
    $sw.ToString()  
    Write-Output "Finish Job"    
    Write-Output ""    
  } -ArgumentList $SerializationWrapper
}

Get-Job | Wait-Job | Receive-Job

正如您从以下输出中看到的那样,生成了 2 个作业,并传递了一个包装的 FooBar 元素进行格式化。

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
520    Job520          BackgroundJob   Running       True            localhost            ...                      
522    Job522          BackgroundJob   Running       True            localhost            ...                      
Start Job
The type of deserialise object $xml is: xml
<FooBar>
  <Foo>ThisIsAFoo1</Foo>
  <Bar>ThisIsABar2</Bar>
</FooBar>
Finish Job

Start Job
The type of deserialise object $xml is: xml
<FooBar>
  <Foo>ThisIsAFoo2</Foo>
  <Bar>ThisIsABar2</Bar>
</FooBar>
Finish Job
于 2013-06-26T06:01:36.540 回答