Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an XML document that describes requirements. If I had Powershell code between the Values tag, could I somehow extract it and then run it?

XML
<field name="c">
        <Required>TRUE</Required>
        <Description>	 Three letter country code where the individual receives company correspondence
	 Must be set to the appropriate ISO 3166-1 "alpha-3" code.  The most common values are "USA" (United States) and "CAN" (Canada).
        </Description>
        <Values>
            Get-ADUser -filter *
        </Values>
</field>


What I have tried:

$standard = [xml] (Get-Content "C:\Users\***\Documents\Standard.xml")
$fields = $standard.SelectNodes("//UsersDefaultOU/field")
$code = $fields[0].Values
Posted
Updated 9-Jun-16 10:17am
v2
Comments
Richard Deeming 9-Jun-16 16:10pm    
Sounds like you're looking for the Invoke-Expression[^] cmdlet.
Sergey Alexandrovich Kryukov 9-Jun-16 16:21pm    
Exactly. But it also takes parsing of the XML (I don't know why having it though).
Please see more detailed answer, Solution 1.
Your comment is credited.
—SA
Sergey Alexandrovich Kryukov 9-Jun-16 16:19pm    
I have no idea why would you need it, and not sure this is a good idea, but it make some good and practically important question, so I up-voted it.
—SA

1 solution

You have to achieve two different things: 1) parse XML in PowerShell, 2) when you extract some parameters and extract/compose a PowerShell command, execute this command.

  1. To start with, read this short but instructional article: Using PowerShell to read xml-files | Little more than out of the box development.

    From the other hand, PowerShell is a fully-fledged .NET language. Well, almost. First, you can learn how to use PowerShell to load an arbitrary assembly, first of all, from GAC, and write .NET code. When you learn it, you can use any part of .NET FCL. It offers some different ways of parsing XML. This is my short overview of them:
  2. The second issue is much less trivial. Here is where you can realize the power of PowerShell, compared to other technologies based on strictly compiled .NET languages: the problem is solved quite simply.

    The solution is: the cmdlet "Invoke-Expression".
    My credit goes to Richard Deeming with his comment to the question. :-)


—SA
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900