Powershell parsing a properties file that contains colons

ar.dll picture ar.dll · Jun 10, 2014 · Viewed 8k times · Source

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'

Answer

Alexander Obersht picture Alexander Obersht · Jun 10, 2014

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