gpt4 book ai didi

powershell - 如何从powershell中的配置文件中获取变量

转载 作者:行者123 更新时间:2023-12-02 22:16:03 24 4
gpt4 key购买 nike

如何从 XML 的配置文件中获取变量?

我实际上是使用 .txt 配置文件执行此操作的。

有我的实际代码:

配置文件

[Repertoire de SPO (generalement C:\SPO)]
destinationn="C:\SPO"

[Nom du sous repertoire de SPO (generalement : SPO1)]
sous_destination="SPO1"

[Numero de la version de SPO vers laquelle vous souhaitez upgrade (par exemple : 1812.4)]
version="1812.4"

[Nom du pool dapplication lie a SPO (par defaut SPO_ADV)]
applicationPoolName="SPO_ADV"

[Chemin de livraison de la nouvelle version de SPO (par defaut \\path\to\somewhere)]
livraisonn="\\path\to\somewhere"

Powershell.ps1
Get-Content "$current_path\config.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }

$destinationn = $h.Get_Item("destinationn")

我想使用 .xml 配置文件做类似的事情。

最佳答案

使用 PowerShell 解析 XML 更容易。请参阅以下带有注释的示例:

#Translated XML config
@'
<root>
<destinationn>C:\SPO</destinationn>
<sous_destination>SPO1</sous_destination>
<version>1812.4</version>
<applicationPoolName>SPO_ADV</applicationPoolName>
<livraisonn>\\path\to\somewhere</livraisonn>
</root>
'@ | Out-File YourSampleConfig.xml

#read xml file, skip root
$config = ([xml](Get-Content YourSampleConfig.xml)).root

#get <destinationn> value
$config.destinationn

关于powershell - 如何从powershell中的配置文件中获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54036380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com