I'm testing a deployment script for my application on Windows Server 2012 with Powershell v3. The script runs fine on Win Server 2008 R2 and Win 7 with Powershell v2. The issue I'm running into now is that I can not access properties of XML variables passed via -ArgumentList.
I've been able to reproduce the issue on Win 7 and Win Server 2012 with Powershell v3 in a simple script that doesn't have any of the SharePoint, IIS, misc that my main script does.
Script (I think I borrowed this from a similar question I can't find now):
$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 *
The Input 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>
Output from 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
Output from 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 :
Note the missing Foobar.Foo value.
I've also tried the $using syntax in v3 but it does the same thing.
I'm using PS 3.0 and it does change data types. I modified your script to take a look:
The output I got was:
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: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.
I'm done searching. My head hurts when I go in depth on things.
The problem is you are trying to serialise an object of type XMLElement which does not serialise. The workaround is to clone the XMLElement and wrap it in a new XMLDocument.
As you can see from the following output 2 jobs were spawned and a single wrapped FooBar element is passed for formatting.
Try this to specify the version of PowerShell to run the job under: