I'm working on a PowerShell script that will do the following:
- Use a function to grab the ini data and assign it to a hashtable (basically what Get-IniContent does, but I'm using one I found on this site).
- Check the nested keys (not the sections, but the keys of each section) to see if the value "NoRequest" exists.
- 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. For example, something like "Section [DataStuff] has a NoRequest value set to false." If a section doesn't contain a NoRequest key, or the value is set to true, then it can be skipped.
I believe I've accomplished the first two parts of this, but I'm unsure of how to proceed with the third step. Here's the code I have so far:
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")){
Write-Output $iniContents.$key.NoRequest
}
}
When I run the run the above code, it gives me the following expected output:
true
true
true
false
I know the INI file I'm testing with has four instances of NoRequest, and I know those are the values of them. I'm just not sure how to get the section name paired with each section that contains the NoRequest = false value while ignoring the true values.
Aucun commentaire:
Enregistrer un commentaire