Thursday 19 July 2012

Powershell Fun (Key - Value Pairs)


Using Powershell to Import Key-Value Pairs from a file




Given a file named ConfigurationKeyValuePairs.conf with Key Value pairs that looks like: 

Key^Value
WebsiteHostName^localhost
QueryServiceHostName^localhost
ApplicationServiceHostName^localhost
ReportServerHostName^localhost
ReportServerPort^8080
IncludeExceptionDetailInFaults^true
ICacheServicePort^8050
Compilation.Debug^true   

You can extract these into a powershell variable with the following command:

Get-Content .\ConfigurationKeyValuePairs.conf | %{$configurationSettings = @{}} { if ($_ -match "(.*)\^(.*)") { $configurationSettings[$matches[1]]=$matches[2]; }}

The keys can now be referred to using 

$configurationSettings.Key


No comments:

Post a Comment