Adding Powershell Script inside XML

At times, you will require to read your data in XML format using Powershell Script. Possible reason to do that is such that you can amend the XML file without editing your original Powershell Script. That’s provided that the standardized Powershell Script can fulfill your requirements.

To cater for More flexibility, you can actually insert Powershell Script within your XML. Here is how you can perform that.

Note that this required a minor change in your Powershell Script. (Not to worry, you only need to edit this once and for all).

[sourcecode language=”powershell”]

$xml = [xml](Get-Content .\yourxml.xml)

$expression = $xml.Scripts.script

#Optionally you can put a foreach loop to run each of your script

Invoke-Expression $expression

[/sourcecode]

Below is the simple xml of mine

[sourcecode language=”xml”]
<scripts>
<script>   Write-Host "Halo";   Write-Host "Testing";
</script>
</scripts>
[/sourcecode]

The output looks like this
Adding Powershell script into XML

Leave a Reply

Your email address will not be published. Required fields are marked *