According to both get-help about_data
and http://technet.microsoft.com/en-us/library/hh848302 , [xml]
literals are allowed in a data section
However when I try to create a data section with the xml block from the documentation
data{
[XML] @'
<p> Hello, World </p>
'@
}
Poweshell complains with an error message
The '[XML]' operator is not allowed in restricted language mode or a Data section.
At C:\Users\Alex\AppData\Local\Temp\150a9138-ebc3-4af1-8aec-73bf724fdcf5.ps1:2 char:6
+ [XML] <<<< @'
+ CategoryInfo : ParserError: (XML:Token) [], ParseException
+ FullyQualifiedErrorId : OperatorNotSupportedInDataSection
Am I doing something stupid, misreading the documentation or is there an error in the documentation?
It isn't a documentation error, but is semantically splitting hairs. You cannot cast in data language, because if you could cast all of the side-effects of casting could happen ( If I recall correctly the thing that actually killed not being able to cast was seeing that casting certain strings to file streams allowed access on disk).
You can keep the XML as a string inside of the data section (that's what's meant by an "XML literal" - the xml string- not the XML data type), and include the cast outside:
$info = @(data {
@'
<p> Hello, World </p>
'@
}) -as [xml]
Incidentally, I tend to believe that data language (and language modes overall) are one of the coolest things about PowerShell (and not just because I tested it). Data Language / No Language mode allow you to secure the PowerShell environment to a finite set of commands, which lets you open up small parts of PowerShell to the rest of the world. Language Modes are heavily used in both Exchange (which was the major feature driver for them) and in PowerShell Pipeworks (a free web language my company built atop PowerShell).
Knowing about another feature of data language, -SupportedCommand, gives you another road to make this work.
You can make a small function, let's call it New-XmlDocument, and "support" using that in data language.
function New-XmlDocument([Parameter(Mandatory=$true,Position=0)][string]$xml) {
$xml -as [xml]
}
data -SupportedCommand New-XmlDocument {
New-XmlDocument @'
<p>Hello World</p>
'@
}
Data Language is powerful PowerShell. Use wisely.
Hope this Helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With