Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Powershell and ini files

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-20-2007   #1 (permalink)
Selko
Guest


 

Powershell and ini files

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


My System SpecsSystem Spec
Old 07-20-2007   #2 (permalink)
Kiron
Guest


 

Re: Powershell and ini files

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

My System SpecsSystem Spec
Old 07-20-2007   #3 (permalink)
Selko
Guest


 

Re: Powershell and ini files

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
>
>

My System SpecsSystem Spec
Old 07-20-2007   #4 (permalink)
Jacques Barathon [MS]
Guest


 

Re: Powershell and ini files

"Selko" <Selko@discussions.microsoft.com> wrote in message
news:E8DF5254-C606-472D-A814-8225FE596758@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

My System SpecsSystem Spec
Old 07-21-2007   #5 (permalink)
Selko
Guest


 

Re: Powershell and ini files

thank you very much ....

that works great


My System SpecsSystem Spec
Old 07-21-2007   #6 (permalink)
Kiron
Guest


 

Re: Powershell and ini files

Nice Jacques. If one needs to include empty keys, change the second regex
pattern to:

'(.+)=(.*)'

--
Kiron

My System SpecsSystem Spec
Old 07-22-2007   #7 (permalink)
Selko
Guest


 

Re: Powershell and ini files

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

"Jacques Barathon [MS]" wrote:

> "Selko" <Selko@discussions.microsoft.com> wrote in message
> news:E8DF5254-C606-472D-A814-8225FE596758@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
>
>

My System SpecsSystem Spec
Old 07-22-2007   #8 (permalink)
Gerd Schneider
Guest


 

Re: Powershell and ini files

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" wrote:

> 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
>
> "Jacques Barathon [MS]" wrote:
>
> > "Selko" <Selko@discussions.microsoft.com> wrote in message
> > news:E8DF5254-C606-472D-A814-8225FE596758@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
> >
> >

My System SpecsSystem Spec
Old 07-22-2007   #9 (permalink)
Selko
Guest


 

Re: Powershell and ini files

ok thanks i will try this out....

"Gerd Schneider" wrote:

> 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" wrote:
>
> > 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
> >
> > "Jacques Barathon [MS]" wrote:
> >
> > > "Selko" <Selko@discussions.microsoft.com> wrote in message
> > > news:E8DF5254-C606-472D-A814-8225FE596758@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
> > >
> > >

My System SpecsSystem Spec
Old 07-22-2007   #10 (permalink)
Brandon Shell
Guest


 

Re: Powershell and ini files

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" <GerdSchneider@discussions.microsoft.com> wrote in message
news:3BBB868A-16AF-40AC-B680-E91DDE403F14@microsoft.com...
> 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" wrote:
>
>> 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
>>
>> "Jacques Barathon [MS]" wrote:
>>
>> > "Selko" <Selko@discussions.microsoft.com> wrote in message
>> > news:E8DF5254-C606-472D-A814-8225FE596758@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
>> >
>> >


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Files created by Powershell are over 2X bigger than files created bytext editor or cmd.exe. tom.luxury PowerShell 4 05-27-2008 05:06 AM
Renumbering Files In Powershell? Barry S. PowerShell 3 10-27-2007 12:06 AM
Processing xml log files in PowerShell Mike Blake-Knox PowerShell 5 08-23-2007 11:00 AM
Powershell script files Mike Blake-Knox PowerShell 1 06-07-2007 10:20 AM
How does PowerShell use the help.xml files? Andrew Watt [MVP] PowerShell 1 11-15-2006 07:16 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51