i tried it with
get-content | where {$_ -match "key1" } |set-content ......
but this didnt work....
can you help me please....
king regards selko
get-content file.ini | foreach-object {$iniData = @{}}
{$iniData[$_.split('=')[0]] = $_.split('=')[1]}
...then access the key:
$iniData.key1
--
Kiron
[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
>
>
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
that works great
'(.+)=(.*)'
--
Kiron
[servers]
name1=LDAP://testserver1.mydomain.com/OU=test,dc=mydomain,dc=com
why this script does not work with this string??
thanks
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.
"Gerd Schneider" <GerdSc...@discussions.microsoft.com> wrote in message
news:3BBB868A-16AF-40AC...@microsoft.com...
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
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
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
+ 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...
http://www.leeholmes.com/blog/ManagingINIFilesWithPowerShell.aspx
---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
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)
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.
"Hal Rottenberg" <h...@halr9000.com> wrote in message
news:euLS7MMw...@TK2MSFTNGP05.phx.gbl...
Oh, this isn't OT at all. Where's the .NET assembly? We can use that in posh
no problem.
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...
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
"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. ; )
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.