Powershell and ini files

1927 views
Skip to first unread message

Selko

unread,
Jul 20, 2007, 3:08:00 PM7/20/07
to
hi
is it possible to read ini keys and values from powershell
because i need some values for my script....

i tried it with

get-content | where {$_ -match "key1" } |set-content ......

but this didnt work....

can you help me please....
king regards selko

Kiron

unread,
Jul 20, 2007, 4:17:30 PM7/20/07
to
You can put the data in a hash table:

get-content file.ini | foreach-object {$iniData = @{}}
{$iniData[$_.split('=')[0]] = $_.split('=')[1]}

...then access the key:

$iniData.key1

--
Kiron

Selko

unread,
Jul 20, 2007, 5:10:02 PM7/20/07
to
hy this works.... thanks
but what if i got a shema like this:

[exchangeserver]
server1=aaaaa
server2=bbbbb
server3=cccccc
[domaincontroller]
server1=ddddd
server2=eeeee
server3=ffffffff

so this dont work..... :(
because i got two times server1 f.e.
what do you think?

"Kiron" wrote:

> You can put the data in a hash table:
>
> get-content file.ini | foreach-object {$iniData = @{}}
> {$iniData[$_.split('=')[0]] = $_.split('=')[1]}
>

> ....then access the key:
>
> $iniData.key1
>
> --
> Kiron
>
>

Jacques Barathon [MS]

unread,
Jul 20, 2007, 6:30:17 PM7/20/07
to
"Selko" <Se...@discussions.microsoft.com> wrote in message
news:E8DF5254-C606-472D...@microsoft.com...

> hy this works.... thanks
> but what if i got a shema like this:
>
> [exchangeserver]
> server1=aaaaa
> server2=bbbbb
> server3=cccccc
> [domaincontroller]
> server1=ddddd
> server2=eeeee
> server3=ffffffff

This should work, although not thoroughly tested:

--- import-ini.ps1 ---
param ($file)

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

PS> $servers = import-ini servers.ini
PS> $servers["exchangeserver"]["server1"]
aaaaa
PS> $servers["domaincontroller"]["server1"]
ddddd

Hope that helps,
Jacques

Selko

unread,
Jul 21, 2007, 3:30:01 AM7/21/07
to
thank you very much ....

that works great


Kiron

unread,
Jul 21, 2007, 10:04:04 AM7/21/07
to
Nice Jacques. If one needs to include empty keys, change the second regex
pattern to:

'(.+)=(.*)'

--
Kiron

Selko

unread,
Jul 22, 2007, 3:38:00 AM7/22/07
to
hi this dont work if i got a string like this:

[servers]
name1=LDAP://testserver1.mydomain.com/OU=test,dc=mydomain,dc=com

why this script does not work with this string??

thanks

Gerd Schneider

unread,
Jul 22, 2007, 6:58:00 AM7/22/07
to
I'm not a guru on regex, but I as far as I understand "(.+)=(.+)" will split
your line to
<name>=name1=LDAP://testserver1.mydomain.com/OU=test,dc=mydomain,dc
<value>=com
Lazy regex parsing may change this behavior. Replace the line "(.+)=(.+)"
with "(.+?)=(.+)", this should end up in splitting the line as expected:
<name>=name1
<value>=LDAP://testserver1.mydomain.com/OU=test,dc=mydomain,dc=com

Gerd

BTW: I take a regular look into this DG for a while and wonder why you are
the first in months to ask for INI support. I also ran into this question a
few months ago and did not find INIs supported by PS or by .NET.

Selko

unread,
Jul 22, 2007, 7:28:01 AM7/22/07
to
ok thanks i will try this out....

Brandon Shell

unread,
Jul 22, 2007, 7:35:41 AM7/22/07
to
I would assume that is because XML is (in the long run) a much better
solution and most people that take up Powershell are use to working with
.NET apps and thier .conf XML files. That said, I think it would be nice to
have ini support.

"Gerd Schneider" <GerdSc...@discussions.microsoft.com> wrote in message
news:3BBB868A-16AF-40AC...@microsoft.com...

Matthias Tacke

unread,
Jul 22, 2007, 8:57:51 AM7/22/07
to
Brandon Shell wrote:
> I would assume that is because XML is (in the long run) a much better
> solution and most people that take up Powershell are use to working with
> ..NET apps and thier .conf XML files. That said, I think it would be
> nice to have ini support.
>

I had the idea of an export-ini from $ini, but there are a lot of ini
files not adhering to standards (or other standards) like comments or
section less entries at the beginning.

Its impossible to keep the order of the entries with possible comments
strayed in.

FWIW, here is my (not much elaborate) try to deal with all that.

- section less entries
- comments
- entries with a second equal sign
- empty entries

#--- import-ini.ps1 ---
param ($file)
$section = "_SectionLess"
$CommentCount = 0

$ini = @{}
$ini[$section] = @{}


switch -regex -file $file
{
"^\[(.+)\]$"
{
$section = $matches[1]
$ini[$section] = @{}

$CommentCount = 0
}
"^(;.*)$"
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount


$ini[$section][$name] = $value
}

"^([^=]+)=(.*)$"


{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
$ini


--
Greetings
Matthias

Chris

unread,
May 28, 2008, 4:59:14 AM5/28/08
to
Hi,

within a powershell script I want to read a few parameters from a ini file.
The ini looks like this:

;File which stores the user information
Source_CSV_File=AD_User_source.csv

;Domain where to create the users
Domain=test.local
;Domaincontroller to connect to (maybe not needed?)
DomainController=mydc.local

line with a ";" at the front are comments.

How can I get these parameters into the powershell script, so that I can work with them?

Thanks in advance

Marco Shaw [MVP]

unread,
May 28, 2008, 5:41:04 AM5/28/08
to

If I understand correctly, try something like this:

PS > $ini=get-content test.ini
PS > $ini[0]


;File which stores the user information

PS > $ini[1]
Source_CSV_File=AD_User_source.csv

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com

Alex K. Angelopoulos at

unread,
May 28, 2008, 8:35:27 AM5/28/08
to
Chris, Marco's demonstration is essentially line-by-line reading, without
specific data parsing. Presumably you want a quick-and-easy way to get the
data into some manageable form. Unfortunately, there's no generic
INI-reading component in Windows, and it didn't make it into PowerShell as a
specialty item either. That means it takes some adaptation to get a reader
working correctly. Could you share some more details about the INI file to
help make sure we know the kind of syntax it uses? There are many ways INI
files can be interpreted, unfortunately. Here are some specific questions:

+ Is this INI file from some specific application or tool? Knowing that can
sometimes help with determining what it allows and doesn't.
+ Does the INI have multiple sections - e.g., headers in brackets [ ]? Or
even one? Or is it an unstructured set of names and values?
+ Are the keys - the names on the left side of the (=) - unique? Or are
they sometimes repeated?
+ Are comments ever in-line, following data? Like this:
key = value ;comment
+ Is data ever split across lines?

The good news is that if this is a classic INI file it's going to be
straightforward to explain how to split out the data. The only troublesome
INI-style files I've encountered have been non-vanilla: REG, INF, - and oh
yeah, the data files used by the game Freelancer. But even those were all
PowerShell-parseable once we knew the right pattern. ; )


"Chris" wrote in message news:20085284591...@yahoo.de...

Shay Levi

unread,
May 28, 2008, 9:52:44 AM5/28/08
to

Hal Rottenberg

unread,
May 28, 2008, 9:03:30 AM5/28/08
to
Alex K. Angelopoulos wrote:
> The good news is that if this is a classic INI file it's going to be
> straightforward to explain how to split out the data. The only
> troublesome INI-style files I've encountered have been non-vanilla: REG,
> INF, - and oh yeah, the data files used by the game Freelancer. But even
> those were all PowerShell-parseable once we knew the right pattern. ; )

What were you doing with the INI files in Freelancer? I used to love that game...

--
Author, Tech Prosaic blog (http://halr9000.com)
Webmaster, Psi (http://psi-im.org)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)

Hal Rottenberg

unread,
May 28, 2008, 9:23:28 AM5/28/08
to
Chris wrote:
> within a powershell script I want to read a few parameters from a ini file.

This may be answering your question with a bigger question but who knows, it
might be useful.

http://www.myitforum.com/forums/m_157022/printable.htm

In there is a script to convert INI files to XML. Once you wrap your head
around it, it's actually easier to work with XML in PowerShell than in anything
else I've used.

Here's another answer, perhaps even more complex. :)

http://www.leeholmes.com/blog/ManagingINIFilesWithPowerShell.aspx

If you don't have to worry about sections (i.e. words in brackets which separate
the file into blocks), and other factors which Alex brought up, then it's
relatively easy to find the equal signs and interpret anything before them as
keys and anything after as values.

Alex K. Angelopoulos at

unread,
May 28, 2008, 11:16:32 AM5/28/08
to
Helping the guy who made the Asgard mod with version 3.0. Most of it was
doing automated debugging, which was a bit of a problem since almost all of
the FL data is in those disconnected INI files, but it was really easy to do
once you had the structure down. One of the core bits was a generic
.NET-based INI parsing component another modder had developed - originally
it was purely for FL, but he extended it into a generalized tool that
supported INF/REG formats. So art _does_ have real-world applications. : )


"Hal Rottenberg" <h...@halr9000.com> wrote in message
news:euLS7MMw...@TK2MSFTNGP05.phx.gbl...

Hal Rottenberg

unread,
May 29, 2008, 8:48:30 AM5/29/08
to
Alex K. Angelopoulos wrote:
> easy to do once you had the structure down. One of the core bits was a
> generic .NET-based INI parsing component another modder had developed -

Oh, this isn't OT at all. Where's the .NET assembly? We can use that in posh
no problem.

Alex K. Angelopoulos at

unread,
May 29, 2008, 12:31:49 PM5/29/08
to
I was looking for it after posting yesterday and haven't found it. The
developer was Bas Westerbaan (http://blog.w-nz.com/) and IIRC the original
tool's namespace was Intrepid.Corelib. I'm not finding the binary online
either, and most of the references to it are in his blog archive
(http://blog.w-nz.com/archives/2005/02/).

I don't recall the binary being anywhere permanent other than among the
Lancer's Reactor downloads, which of course are now long gone. I'll do some
more digging. A really simple privateprofile* API wrapper set would be
easier to use with traditional INI files than Bas' component, but his parser
could do a _lot_ of things you just can't do with INI files.


"Hal Rottenberg" <h...@halr9000.com> wrote in message

news:uf1#MpYwIH...@TK2MSFTNGP04.phx.gbl...

Joel (Jaykul) Bennett

unread,
May 31, 2008, 10:50:27 PM5/31/08
to
I know I'm really late to the party, but ... http://nini.sourceforge.net/
works great with ini files (as well as being able to save them to xml)
and is open source and free

Also, there's a script on the repository that works very well ... (how
come nobody looks there?)

Id : 160
Title : IniFile Functions 1.0
Description : A bunch of functions for working with .ini files...
Author : Joel Bennett
Date : 79 days ago
Link : http://powershellcentral.com/scripts/160

Alex K. Angelopoulos at

unread,
Jun 2, 2008, 9:22:02 PM6/2/08
to

"Joel (Jaykul) Bennett" <Jay...@huddledmasses.org> wrote in message
news:28dc51b3-adaf-446a...@m73g2000hsh.googlegroups.com...


> I know I'm really late to the party, but ... http://nini.sourceforge.net/
> works great with ini files (as well as being able to save them to xml)
> and is open source and free
>
> Also, there's a script on the repository that works very well ... (how
> come nobody looks there?)

A very good reason. Everyday, old-fashioned egregious oversight. ; )

Flowering Weeds

unread,
Jun 7, 2008, 4:41:56 PM6/7/08
to

And

> Chris, Marco's demonstration is essentially
> line-by-line reading, without specific data parsing.
>

Mmm did someone mention data parsing?

Perhaps Windows (any language) data parser!

Here using one's data with
Log Parser in PowerShell:

# The below uses the Log Parser
# Input format: TEXTLINE (TextLine Format)
# Parses entire lines out of generic text files
# LogParser.exe -h -i:textline

LogParser.exe "SELECT
EXTRACT_TOKEN(text,0,'=') AS Name,
EXTRACT_TOKEN(text,1,'=') AS NameValue
FROM theTestIni.ini
WHERE text NOT LIKE ';%'
AND
text IS NOT NULL " -i:textline

" "
"Done!"
" "

Or PowerShell "script" LogParser's data parsing,
field by field, with LogParser's scripting COM or
even .NET scripting usage, then use one's resulting
ini data right into one's PowerShell's "script" usage!

Microsoft.com Operations : Power Parsing...some
days you just need more power for your parser
http://blogs.technet.com/mscom/archive/2007/10/01/power-parsing-some-days-you-just-need-more-power-for-your-parser.aspx

And since Windows data parser is PowerShell's
"automation" data parser too, then any PowerShell
"user" should be able to help one data parse!

Also don't forget the data grid or chart output!

Notice: IIS does not need to be running
or installed in order to use Log Parser
for either data parsing or chart making.

Search the Internet
(and this newsgroup)
for usage of:

Microsoft's Log Parser
command line usage,
or fully script enabled
either in COM or .NET
(from the IIS group)

Just one of many examples of
PowerShell using Windows (any
language) data parser Log Parser.


Reply all
Reply to author
Forward
0 new messages