简体   繁体   中英

Use of XML in Powershell scripts

I have a script with which I want users to be able to edit an XML file and have their options then utilized in the associated PowerShell script. But I don't want the users to edit the script - I want them to edit the xml file and then have those xml items set as variables in the PS script so that basically when they edit the xml file, they are editing the appropriate values in the script) -

I tried to paste the xml file and the script I created here, but it didn't format correctly...

HELP?!?! I am so lost. I find plenty of things online about using Xpath and that just doesn't seem to be correct for me....how do I go about accomplishing this, or even searching for the right way to accomplish it?

Thanks, LG

I'm hoping we'll get some more clarity when we see your code, but for now I have a suggestion.

As long as you don't need to have the XML formatted in your own proprietary format, use CliXML by way of the Import-Clixml command.

For example, consider this code to initially write your XML file:

# a hashtable with values you want to keep
$opts = @{
    val1 = 'Hello'
    val2 = 'World'
    val3 = 5
}
$opts | Export-Clixml -Path C:\options\options.xml

Now in your script where you want to use these options, just import them and use them:

$opts = Import-Clixml -Path C:\options\options.xml
$opts.val1
$opts.val2
$opts.val3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM