tags:

views:

1895

answers:

2

Hello.

I'm parsing simple (no sections) INI file in PowerShell. Here code I've came up with, is there any way to simplify it?

convertfrom-stringdata -StringData ( `
  get-content .\deploy.ini `
  | foreach-object `
    -Begin { $total = "" }  `
    { $total += "`n" + $_.ToString() } `
    -End { $total } `
).Replace("\", "\\")
+1  A: 

I'm not exactly sure what your source data looks like, or what your goal is. What exactly are you parsing for? Can you post a sample of the file? As-is, it looks like you're just concatenating carriage returns to the existing lines of the file and replacing \ with \.

Nor certain why you're using $.ToString() since $ is already a string object output by Get-Content.

Is the goal just to take a file containing a bunch of name=value pairs, and convert that to a hashtable? That's what ConvertFrom-StringData does, although that cmdlet is only available in the preview of PowerShell v2.

If your file looks like... key1=value1 key2=value2 key3=value3

Then all you should need is

ConvertFrom-StringData (Get-Content .\deploy.ini)

I'm not sure I understand why you're tacking on extra carriage returns. There's also no need to use the -Begin and -End parameters, at least not as far as I can see from what you've posted.

Don Jones
Your example does not work because of 'unified' get-content cmdlet output. It outputs System.Object[]: ConvertFrom-StringData : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'StringData'That's why I iterate wrought it and reassemble to string.
Artem Tikhomirov
That's not something I could have determined without looking at your source data, unfortunately. As I tried to indicate, I was trying to help answer your question but I need more information to do so accurately. I thought I made that clear - sorry.
Don Jones
No need to give him -1 for him being helpful. And he is stating he does not know for sure what you want.
Lars Truijens
Hm.. I've said, that I have simple INI file without sections. Isn't it clear enough? And get-content advise is wrong anyway. Try run (get-content anyFile.whatever).GetType() on any data. And of course person calming himself as PS expert in bio should know it or be able to check it before writing BS.
Artem Tikhomirov
An "ini file without sections" could be anything. I have INI files which use key:value, others which use key=value, and others which use an entirely different format. And as I sit here testing it, Get-Content is returning String objects when I give it a text file. If you don't want help, don't ask.
Don Jones
Aha, sure it returns. Try execute this "book author": (get-content 'Your Text Document.txt').GetType() -eq [System.String]
Artem Tikhomirov
BTW be sure to check this for your own education: http://en.wikipedia.org/wiki/INI_file
Artem Tikhomirov
Jeez, you're bitter.
Don Jones
+3  A: 

After searching internet on this topic I've found a handful of solutions. All of them are hand parsing of file data so I gave up trying to make standard cmdlets to do the job. There are fancy solutions as this witch support writing scenario.

There are simpler ones and as far as I need no writing support I've chose following very elegant code snippet:

Function Parse-IniFile ($file) {
  $ini = @{}
  switch -regex -file $file {
    "^\[(.+)\]$" {
      $section = $matches[1]
      $ini[$section] = @{}
    }
    "(.+)=(.+)" {
      $name,$value = $matches[1..2]
      $ini[$section][$name] = $value
    }
  }
  $ini
}

This one is Jacques Barathon's.

Artem Tikhomirov