chad
asked on
Powershell - Parsing xml file
yes, I am back and hopefully with a quick and easy one.
I have an xml file with about ten lines of info. One line will be very similar to:
<HostName>computer1</HostN ame>
The attached code gives me output of:
C:\temp\file.xml:3: computer1
the c:\temp\file.xml is the source xml file, the 3 is the line number in that file where the string was found .
I only want to see:
computer1
I know this should be easy and I have been searching onlilne for a while and had to give in to tossing out some powershell points :-)
what am I missing here? is there a better way to do this?
thanks,
C
I have an xml file with about ten lines of info. One line will be very similar to:
<HostName>computer1</HostN
The attached code gives me output of:
C:\temp\file.xml:3: computer1
the c:\temp\file.xml is the source xml file, the 3 is the line number in that file where the string was found .
I only want to see:
computer1
I know this should be easy and I have been searching onlilne for a while and had to give in to tossing out some powershell points :-)
what am I missing here? is there a better way to do this?
thanks,
C
$objTXTdbserver =New-item -type file "c:\powershell\OutputFiles\computer.txt" -force
$siteXML = "c:\temp\file.xml"
$dbServerA = select-string -path $siteXML -pattern "<HostName>"
$dbServerb = foreach-object {$dbServera -replace "<hostname>", ""}
$dbServerb = foreach-object {$dbServerb -replace "</hostname>", ""}
write-host $dbServerb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I guess that is really what I am looking for. I was up late on my end working on this and searching online for 'powershell parse txt'. If I did a google search for 'powershell parse xml' I would have found what I am really after. I like the parsing as XML results much better. I know I will be using more down the line. Now, I need to look into decrypting a file using another application so that the file can be read as xml.
BTW, what you posted above also worked great. I will save that for an ini file I will need to probe.
http://thepowershellguy.com/blogs/posh/archive/2007/12/30/processing-xml-with-powershell.aspx
Lesson to be learned here: Don't drink and code!
BTW, what you posted above also worked great. I will save that for an ini file I will need to probe.
http://thepowershellguy.com/blogs/posh/archive/2007/12/30/processing-xml-with-powershell.aspx
Lesson to be learned here: Don't drink and code!
ASKER
if ($line -match "HostName"?)
then I used the -replace x 2 to rid the tags.
I looks like it will do what I need but is it the cleanest or best practice?
thanks,
C
Open in new window