Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 264
ar.dll Avatar asked Jun 10 '14 13:06

ar.dll


2 Answers

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
like image 100
Alexander Obersht Avatar answered Oct 16 '22 11:10

Alexander Obersht


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
like image 20
Raf Avatar answered Oct 16 '22 12:10

Raf