Some Scripts need to store configuration values in Ini- or XML-Files. What is the best way to do that?
Ini-Files
Using Ini-Files in Powershell is a challenge. Microsoft Currently has no real Access Solution for Ini-Files. The Community give us some ways to use a workaround.
Two other Methods are:
- Method 1: Google Groups Thread Powershell and ini Files
- Method 2: Managing INI files with PowerShell (Access over Windows API)
I Like the Opensource component Nini is a widely used Ini Reading Component for .Net. with alot of flexibility. The component is well documented and can have different Sources.Ok, gave a try with Nini 1.1.0….
1
2
3
4
5
6
7
8
9
10
11
C:\>$AssemblyRootPath = "c:\Temp\Nini-1.1.0\Nini\Bin\DotNet\2.0\Debug\2.0\Debug"
C:\>$IniFile = "C:\temp\IniTest\Test.ini"
C:\>[System.Reflection.Assembly]::LoadFrom((join-Path $AssemblyRootPath -childPath NIni.dll)) | out-null
C:\>$configSource = new-object Nini.Config.IniConfigSource($null)
C:\>$config = $configSource.AddConfig("SectionFromPowershell");
C:\>$config.Set("MyTestValue 2", "Value 2");
C:\>$configSource.save($IniFile)
C:\>$config = $configSource.AddConfig("AnotherIniSection");
C:\>$config.Set("Pair 1", "Values 1");
C:\>$config.Set("Pair 2", "Value 2");
C:\>$configSource.save($IniFile)
Try the Attached file for more Details take a look on TestNini.txt.