如何在 powershell 脚本中读取属性文件的值
How to read values of the properties file in the powershell script
我有一个属性文件 properties.txt,其中包含以下值
filename = atc
extension = zip
path = D:\root
and so on ..
所以我在文本文件中有一些属性,我想在 .ps1 文件中使用这些值
例如,当我想使用 .ps1 文件中的路径时,我希望 .ps1 文件从 properties.txt
文件中读取。我正在尝试上面的代码,但它不起作用。我想读取所有值
$props_file = Get-Content "D:\code"
$props = ConvertFrom-StringData ($properties.txt)
谁能帮帮我?
这个怎么样?
$fileContents = get-content c:\path\properties.txt
$properties = @{}
foreach($line in $fileContents)
{
write-host $line
# ,2 tells split to return two substrings (a single split) per line
$words = $line.Split('=',2)
$properties.add($words[0].Trim(), $words[1].Trim())
}
$properties
然后你可以使用这样的值
$properties.Extension
$properties.path
我有一个属性文件 properties.txt,其中包含以下值
filename = atc
extension = zip
path = D:\root
and so on ..
所以我在文本文件中有一些属性,我想在 .ps1 文件中使用这些值
例如,当我想使用 .ps1 文件中的路径时,我希望 .ps1 文件从 properties.txt
文件中读取。我正在尝试上面的代码,但它不起作用。我想读取所有值
$props_file = Get-Content "D:\code"
$props = ConvertFrom-StringData ($properties.txt)
谁能帮帮我?
这个怎么样?
$fileContents = get-content c:\path\properties.txt
$properties = @{}
foreach($line in $fileContents)
{
write-host $line
# ,2 tells split to return two substrings (a single split) per line
$words = $line.Split('=',2)
$properties.add($words[0].Trim(), $words[1].Trim())
}
$properties
然后你可以使用这样的值
$properties.Extension
$properties.path