从ini创建powershell的输出 - 高级

时间:2017-09-02 05:11:27

标签: powershell output ini

我正在尝试使用PSIni module命令Get-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

我想从输出中排除[常规设置]中的任何内容。我想看到的唯一数据是[KPROD]部分键和值以及[KDEV]和[KTEST]部分中与[KPROD]中的值不同的任何值。

这是我目前的代码:

ipmo psini
$ini = Get-IniContent "C:\Temp\test1.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"

目前的结果如下:

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

1 个答案:

答案 0 :(得分:2)

我认为这可以为您提供您所寻找的内容:

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].$_)" }
    }
}

如果您不熟悉它,上面使用的是$_循环中存在的自动变量ForEach-Object,并且每次迭代都包含当前枚举的项目,其中包含我们的案例是我们要检查的部分中的设置名称。