PowerShell从简单的.ini文件读取单个值
我发现的所有内容看起来都过于复杂. 几乎就像我只需要阅读一个文本文件.
Everything I've found looks way over complex. It's almost like I just need to read a text file.
ADAP.ini包含以下内容:
ADAP.ini contains this, nothing else:
http://xxx.104.xxx.226
APP=2.3.6
DLL=2.3.6
使用Powershell, 我如何读取APP = value是什么? 或者DLL = value是什么?
Using Powershell, how can I read what APP=value is? and or what DLL=value is?
我会将值存储在变量中,以后再在Powershell脚本中使用它.
I would store the value in a variable and use it later in Powershell script.
这似乎是ConvertFrom-StringData
的一个好用例,它默认情况下会查找用等号分隔的键值对.
This looks like a good use case for ConvertFrom-StringData
which by default looks for key value pairs separated by the equals symbol.
由于您的.ini文件的第一行没有等号,因此我们需要跳过它以避免出现错误.只需使用Select -Skip 1
即可完成.
Because the first line of your .ini file doesn't have an equals we would need to skip it to avoid an error. This can be done simply with Select -Skip 1
.
代码如下:
$ADAP = Get-Content 'ADAP.ini' | Select -Skip 1 | ConvertFrom-StringData
然后可以通过将它们作为$ADAP
对象的命名属性来访问来获取APP和DLL的值,如下所示:
You can then get the values of APP and DLL by accessing them as named properties of the $ADAP
object, as follows:
$ADAP.APP
$ADAP.DLL