简体   繁体   English

在 Powershell 脚本中使用 XML

[英]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.我有一个脚本,我希望用户能够使用它来编辑 XML 文件,然后在关联的 PowerShell 脚本中使用他们的选项。 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) -但我不希望用户编辑脚本 - 我希望他们编辑 xml 文件,然后将这些 xml 项目设置为 PS 脚本中的变量,这样基本上当他们编辑 xml 文件时,他们正在编辑适当的值在脚本中)-

I tried to paste the xml file and the script I created here, but it didn't format correctly...我试图粘贴我在此处创建的 xml 文件和脚本,但格式不正确...

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?我在网上找到了很多关于使用 Xpath 的东西,但这对我来说似乎并不正确......我该如何完成这个任务,或者甚至寻找正确的方法来完成它?

Thanks, LG谢谢,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.只要您不需要将 XML 格式化为您自己的专有格式,就可以通过Import-Clixml命令使用 CliXML。

For example, consider this code to initially write your XML file:例如,考虑此代码最初编写您的 XML 文件:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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