PowerShell从简单的.ini文件读取单个值 [英] PowerShell to Read single value from simple .ini file

查看:438
本文介绍了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

这篇关于PowerShell从简单的.ini文件读取单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆