PowerShell:遍历 .ini 文件
PowerShell: Looping through an .ini file
我正在编写将执行以下操作的 PowerShell 脚本:
- 使用函数获取 ini 数据并将其分配给哈希表(基本上是 Get-IniContent 所做的,但我使用的是我在该站点上找到的一个)。
- 检查嵌套键(不是sections,而是每个section的key)是否存在值"NoRequest"
- 如果一个部分包含一个 NoRequest 键,并且仅当 NoRequest 值为 false,那么我想 return 部分的名称、NoRequest 键和键的值。例如,类似 "Section [DataStuff] has a NoRequest value set to false." 如果某个部分不包含 NoRequest 键,或者该值设置为 true,则可以跳过它。
我相信我已经完成了前两部分,但我不确定如何进行第三步。这是我到目前为止的代码:
function Get-IniFile
{
param(
[parameter(Mandatory = $true)] [string] $filePath
)
$anonymous = "NoSection"
$ini = @{}
switch -regex -file $filePath
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
$iniContents = Get-IniFile C:\testing.ini
foreach ($key in $iniContents.Keys){
if ($iniContents.$key.Contains("NoRequest")){
if ($iniContents.$key.NoRequest -ne "true"){
Write-Output $iniContents.$key.NoRequest
}
}
}
当我 运行 运行 上面的代码时,它给了我以下预期输出,因为我知道 INI 中有四个 NoRequest 实例并且只有其中一个设置为错误:
false
我相信我已经解决了从文件中找到正确值的问题,但我不确定如何继续获取上面第 3 步中提到的正确输出。
你快到了。这将以您提到的形式输出一个字符串:
$key = "NoRequest" # They key you're looking for
$expected = "false" # The expected value
foreach ($section in $iniContents.Keys) {
# check if key exists and is set to expected value
if ($iniContents[$section].Contains($key) -and $iniContents[$section][$key] -eq $expected) {
# output section-name, key-name and expected value
"Section '$section' has a '$key' key set to '$expected'."
}
}
当然,既然你说..
If a section contains a NoRequest key, and ONLY IF the NoRequest value
is false, then I want to return the name of the section, the NoRequest
key, and the key's value.
..键名和值在输出中将始终相同。
我正在编写将执行以下操作的 PowerShell 脚本:
- 使用函数获取 ini 数据并将其分配给哈希表(基本上是 Get-IniContent 所做的,但我使用的是我在该站点上找到的一个)。
- 检查嵌套键(不是sections,而是每个section的key)是否存在值"NoRequest"
- 如果一个部分包含一个 NoRequest 键,并且仅当 NoRequest 值为 false,那么我想 return 部分的名称、NoRequest 键和键的值。例如,类似 "Section [DataStuff] has a NoRequest value set to false." 如果某个部分不包含 NoRequest 键,或者该值设置为 true,则可以跳过它。
我相信我已经完成了前两部分,但我不确定如何进行第三步。这是我到目前为止的代码:
function Get-IniFile
{
param(
[parameter(Mandatory = $true)] [string] $filePath
)
$anonymous = "NoSection"
$ini = @{}
switch -regex -file $filePath
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
$iniContents = Get-IniFile C:\testing.ini
foreach ($key in $iniContents.Keys){
if ($iniContents.$key.Contains("NoRequest")){
if ($iniContents.$key.NoRequest -ne "true"){
Write-Output $iniContents.$key.NoRequest
}
}
}
当我 运行 运行 上面的代码时,它给了我以下预期输出,因为我知道 INI 中有四个 NoRequest 实例并且只有其中一个设置为错误:
false
我相信我已经解决了从文件中找到正确值的问题,但我不确定如何继续获取上面第 3 步中提到的正确输出。
你快到了。这将以您提到的形式输出一个字符串:
$key = "NoRequest" # They key you're looking for
$expected = "false" # The expected value
foreach ($section in $iniContents.Keys) {
# check if key exists and is set to expected value
if ($iniContents[$section].Contains($key) -and $iniContents[$section][$key] -eq $expected) {
# output section-name, key-name and expected value
"Section '$section' has a '$key' key set to '$expected'."
}
}
当然,既然你说..
If a section contains a NoRequest key, and ONLY IF the NoRequest value is false, then I want to return the name of the section, the NoRequest key, and the key's value.
..键名和值在输出中将始终相同。