Solved

Powershell - Parsing xml file

Posted on 2010-08-27
3
1,000 Views
Last Modified: 2012-05-10
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</HostName>

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

Open in new window

0
Comment
Question by:kabaam
  • 2
3 Comments
 
LVL 11

Author Comment

by:kabaam
ID: 33548084
I have gone with using foreach to look at each line for
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
$siteXML = "c:\temp\File.xml"

$DBX = get-content $siteXML

foreach ($line in $DBX)
   { if ($line -match "<HostName>?")
        {
         $HostName = $line -replace "<hostname>", ""
         $HostName = $HostName -replace "</hostname>", ""
         $HostName = $HostName -replace " ", ""
        }
     }   
        write-host $HostName

Open in new window

0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 33548556

It depends really...

You could do this:

Get-Content $siteXML |
  Where-Object { $_ -Match '<Hostname>' } |
  ForEach-Object { $_ -Replace '<\/?hostname>|\s' }

Or you could read the file as XML, .NET has a lot of XML handling components, so if you want more than Hostname you can do more.

Chris

PS it's early in the morning, I'm hoping it's not too early for me to get the regular expressions above right.
0
 
LVL 11

Author Comment

by:kabaam
ID: 33549739
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!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now