![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| Guest | Re: Powershell and ini files thank you very much .... that works great |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Powershell and ini files Nice Jacques. If one needs to include empty keys, change the second regex pattern to: '(.+)=(.*)' -- Kiron |
My System Specs![]() |
| | #7 (permalink) |
| 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 Specs![]() |
| | #8 (permalink) |
| 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 Specs![]() |
| | #9 (permalink) |
| 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 Specs![]() |
| | #10 (permalink) |
| 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 Specs![]() |
![]() |
| 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 |