如果匹配(如果存在),则从ini创build输出到powershell,然后跳过

我正在尝试使用PSIniGet-IniContent )从.ini文件中提取数据。 我有一个工作的格式,但生成太多的数据。 我的ini文件如下所示:

 [General settings] gensetting1=random gensetting2=random gensetting3=random [KPROD] setting1=1 setting2=2 setting3=3 setting4=4 [KTEST] setting1=1 setting2=2 setting3=3 setting4=4 [KDEV] setting1=1 setting2=2 setting3=55 setting4=4 

我想从[General settings]排除任何东西在我的输出中填充。 因为他们显示空白,因为我不需要收集这些信息,我在下面的代码中指定。 我唯一能看到的数据是[KPROD]键和值, 如果[KDEV][KTEST]的值不同,我想显示不匹配的值。 这是我现在的代码:

 Import-Module psini $ini = Get-IniContent "D:\PShell\SF\871753.ini" Foreach ($key in $ini.keys) { Write-Host $key ; Write-Host "Settings1 and Settings2 are set to:" ($ini[$key].GetEnumerator() | Where-Object { $_.key -like "Setting1" -or $_.key -like "Setting2" } | Format-Table -HideTableHeaders | Out-String).trim(); Write-Host "Setting3 is set to: " ; ($ini[$key].GetEnumerator() | Where-Object { $_.key -like "Setting3" } | Format-Table -HideTableHeaders | Out-String).trim(); Write-Host "Setting4 is set to:" ; ($ini[$key].GetEnumerator() | Where-Object { $_.key -like "Setting4" } | Format-Table -HideTableHeaders | Out-String).trim(); Write-host "" } Read-Host -Prompt "Press Enter to exit" 

显示的结果如下所示,您可以使用带有6个键和20个键的.ini文件进行查看,该列表变得非常长。

 General settings Settings1 and Settings2 are set to: Setting3 is set to: Setting4 is set to: KPROD Settings1 and Settings2 are set to: setting1 1 setting2 2 Setting3 is set to: setting3 3 Setting4 is set to: setting4 4 KTEST Settings1 and Settings2 are set to: setting1 1 setting2 2 Setting3 is set to: setting3 3 Setting4 is set to: setting4 4 KDEV Settings1 and Settings2 are set to: setting1 1 setting2 2 Setting3 is set to: setting3 55 Setting4 is set to: setting4 4 Press Enter to exit: 

甚至有可能实现我的目标? 我会喜欢它看起来像…

 KPROD Settings1 and Settings2 are set to: setting1 1 setting2 2 Setting3 is set to: setting3 3 Setting4 is set to: setting4 4 KMDEV Setting3 is set to: setting3 55 

 Import-Module PSIni $Ini = Get-IniContent 'Example.ini' #List the name and value of all the KPROD keys Write-Host "`nKPROD Settings" $Ini['KPROD'].Keys | ForEach-Object { "$_ is set to $($Ini['KPROD'].$_)" } #Use a ForEach loop so we don't have to duplicate code to check the two other sections ForEach ($Section in 'KTEST','KDEV') { Write-Host "`n$Section Settings" $Ini[$Section].Keys | ForEach-Object { #Uses a ForEach-Object loop to check through all of the Keys in the current section and compare them to the same named key in the KPROD section, outputting them if they differ If ($Ini[$Section].$_ -ne $Ini['KPROD'].$_) { "$_ is set to $($Ini[$Section].$_)" } } }