ar.dll
ar.dll

Reputation: 787

Powershell parsing a properties file that contains colons

If I have a .properties file that contains directories (which contain colons):

some_dir=f:\some\dir\etc
another_dir=d:\dir\some\bin

and then use ConvertFrom-StringData to convert Key=Value pairs from said properties file to a hash table:

$props_file = Get-Content "F:\dir\etc\props.properties"
$props = ConvertFrom-StringData ($props_file)
$the_dir = $props.'some_dir'
Write-Host $the_dir

Powershell throws an error (doesn't like colons):

ConvertFrom-StringData : Cannot convert 'System.Object[]' to the type 'System.String'    required by parameter 'StringData'. Specified method is not supported.
At line:3 char:32
+ $props = ConvertFrom-StringData <<<<  ($props_file)
+ CategoryInfo          : InvalidArgument: (:) [ConvertFrom-StringData],     ParameterBindingException
+ FullyQualifiedErrorId :     CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand

How do you get round this? I'd like to be able to just refer to the directories using the . notation:

$props.'some_dir'

Upvotes: 7

Views: 10818

Answers (3)

Alexander Obersht
Alexander Obersht

Reputation: 3275

Colons have nothing to do with the error you get. And yes, it can be achieved using ConvertFrom-StringData but, as already mentioned, you're feeding it an array instead of a string. Moreover, you need paths with double backslashes in your file because single backslashes are interpreted as escape characters.

Here's how to fix your code:

# Reading file as a single string:
$sRawString = Get-Content "F:\dir\etc\props.properties" | Out-String

# The following line of code makes no sense at first glance 
# but it's only because the first '\\' is a regex pattern and the second isn't. )
$sStringToConvert = $sRawString -replace '\\', '\\'

# And now conversion works.
$htProperties = ConvertFrom-StringData $sStringToConvert

$the_dir = $htProperties.'some_dir'
Write-Host $the_dir

Upvotes: 14

TheMadTechnician
TheMadTechnician

Reputation: 36297

You could parse the file as it's loaded, and populate an empty hashtable line-by-line.

$props = @{}
GC "F:\dir\etc\props.properties" | %{$props.add($_.split('=')[0],$_.split('=')[1])}

Upvotes: 3

Raf
Raf

Reputation: 10107

ConvertFrom-StringData expects a string and you are feeding it an array Get-Content cmdlet. Change $props_file to:

$props_file = (Get-Content "F:\dir\etc\props.properties") | Out-String

and you will get another error:

ConvertFrom-StringData : parsing "f:\some\dir\etc" - Unrecognized escape sequence \s.

You can get around it like so:

$props_file = Get-Content "F:\dir\etc\props.properties"
$props = @{}
$props_file | % {
    $s = $_ -split "="
    $props.Add($s[0],$s[1])
}
$the_dir = $props.'some_dir'
Write-Host $the_dir

Result:

f:\some\dir\etc

Upvotes: 3

Related Questions