user951376
user951376

Reputation: 33

Creating an output to powershell from an ini - Advanced

I am trying to pull data from a .ini file using the PSIni module command Get-IniContent. I have a script that works but it generates more data than I need.

My ini file looks like this:

[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

I would like to exclude anything from [General settings] from my output. The only data I want to see is the [KPROD] section keys and values and any values from the [KDEV] and [KTEST] sections that are different to those in [KPROD].

Here is my current code:

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"

Here is the result currently:

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: 

I would like the output to look like the following:

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

Upvotes: 1

Views: 63

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23395

I think this gives you what you're looking for:

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

In case you're not familiar with it, the above is using the automatic variable $_ which exists within the ForEach-Object loop and on each iteration contains the current item being enumerated, which in our case is the name of the setting from the section we're checking.

Upvotes: 2

Related Questions