header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Need help with parsing log files
Last Post 03 Dec 2008 02:58 PM by danjphillips. 13 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
danjphillipsUser is Offline
New Member
New Member
Posts:15
Avatar

--
01 Dec 2008 05:28 PM  
Hello

I'm working on a PowerShell script to read some backup log files to get some data back into a variable.  If the data that I get back is < a value I want it to email me.  I have the < and the email part, it's just the log parsing I'm having troubles with where to begin.  I'm looking at a txt file for the server name and folder path where the logs reside.  How can I parse the following results to only show the end number, for example:

Results
\\computer\c$\logtest\tsm_incr.cmd_1boqr_1.dat:31:Total number of objects inspected: 27,460
\\computer\c$\logtest\tsm_incr.cmd_1bkl8_1.dat:32:Total number of objects backed up: 149

The hard part I'm trying to figure out is you never know what the end number will be or how many spaces after the ":"

Any ideas?  Thanks
EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
01 Dec 2008 07:10 PM  
Split on spaces and take the last item in the array. I'll post code in a few.
EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
01 Dec 2008 07:12 PM  
So, this is command line, but you could easily convert to script:

$line = "\\computer\c$\logtest\tsm_incr.cmd_1boqr_1.dat:31:Total number of objects inspected: 27,460"
($line.Split(' '))[-1]
27,460
danjphillipsUser is Offline
New Member
New Member
Posts:15
Avatar

--
01 Dec 2008 08:07 PM  
Thank you.  Your example worked.  The problem I'm running into now is when I get that line read from a txt file into a variable I get the following error in my output.

Error Method
[Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'Split'.

Any ideas?  Here's the line in my code
($inspected.Split(' '))[-1]

If I remove everything and just have $inspected I show the output, but when I add the Split method it breaks with the abover error message
EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
01 Dec 2008 08:15 PM  
So these are match items from a regular expression execution I'm guessing? If that is the case then explicitly cast the match to string then split. I think this will work:

(($inspected.ToString()).split(' '))[-1]


You may have to play with the parenthesis, my internal parser is a little off today.
danjphillipsUser is Offline
New Member
New Member
Posts:15
Avatar

--
01 Dec 2008 08:20 PM  

HEY, IT WORKED!!!!!!!!  Thank you very much.  When you get a free minute can you explain, in a bit more detail on what exactly is happening here.

Thanks again for your help.

EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
01 Dec 2008 08:50 PM  
Basically, a string in .net has a split method as part of its definition. All classes in .net should have a .ToString() method that returns a string object. So here is essentially what we did:

$inspected.ToString() - Take the match object and execute its ToString() method to return a string representation. In this case, the string representation was the string value of the match. We got lucky because sometimes .ToString() just returns the namespace path for the object. In that case I can't remember for sure, but I think the match object probably has a .Value property or similar that returns a string. Regardless, our goal was to get to a string that held the value that we wanted to split.

($inspected.ToString()).Split(' ') - Take the string that we now have and split it on spaces returning an array of strings.

(($inspected.ToString()).Split(' '))[-1] - Get the last item of the array that we just got.
danjphillipsUser is Offline
New Member
New Member
Posts:15
Avatar

--
02 Dec 2008 03:19 PM  

Thanks for helping me better understand that.  One last question :)

Say I want to store that result into a variable like this.  I tried that it doesn't seem to work

$out = (($inspected.ToString()).split(' '))[-1]
$out

This would result in the following error message
System.Object[]

Any thoughts?

EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
02 Dec 2008 03:27 PM  
That is odd. It worked fine for me.

1# $inspected = "\\computer\c$\logtest\tsm_incr.cmd_1boqr_1.dat:31:Total number of objects inspected: 27,460"
2# (($inspected.ToString()).split(' '))[-1]
27,460
3# $out = (($inspected.ToString()).split(' '))[-1]
4# $out
27,460
danjphillipsUser is Offline
New Member
New Member
Posts:15
Avatar

--
02 Dec 2008 03:34 PM  

Below is the code I'm working on.  The c:\complist.txt is just a test text file with a computer name and a folder path to the log files I want to parse.  Putting the value from the array into a variable works, but only if I'm searching through one log file.  This is set on the -Last 2 using 'Select'.  If I change to -Last 1 it works.  My guess is how I'm doing my foreach loop

================================================

$getserverfromtxt = Get-Content "c:\complist.txt"
$lookforinspected = 'inspected'

foreach ($i in $getserverfromtxt)

{

$tsmfile = Get-ChildItem $i | Sort-Object -Property LastWriteTime | Select -Last 2

   $inspected = Select-String -SimpleMatch $lookforinspected $tsmfile

      $out = (($inspected.ToString()).split(' '))[-1] 
      $out

}

================================================

EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
02 Dec 2008 07:17 PM  
So, if I understand what is going on, then this line:

$tsmfile = Get-ChildItem $i | Sort-Object -Property LastWriteTime | Select -Last 2

Should look at all the files in a folder and store the last 2 files in $tsmfile. Is that right?
danjphillipsUser is Offline
New Member
New Member
Posts:15
Avatar

--
02 Dec 2008 07:18 PM  
Yes, you are correct.
EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
02 Dec 2008 07:30 PM  
I think you need to go through and understand what your variables hold at each point in the script. For instance you are handing 2 files to Select-String. I don't know if it supports that or not. Even if it does, I think the only way that your Split() will work is on a single string and I suspect the $inspected holds more than one element.
danjphillipsUser is Offline
New Member
New Member
Posts:15
Avatar

--
03 Dec 2008 02:58 PM  

I got it working.  You were right, my variable store didn't like the loop.  I redid the foreach and am able to pipe the information out to a text file.

Here's the code.

=====================================================

$tsmlogpath = "c:\logtest"
$lookforinspected = 'inspected'

foreach ($i in Get-ChildItem $tsmlogpath | Sort-Object -Property LastWriteTime | ` 
   Select -Last 4 | get-content | Select-String -SimpleMatch $lookforinspected)

{ (($i.ToString()).split(' '))[-1] | Out-File -Append c:\test.txt }


=====================================================

Thanks for your help with the .ToString as well as talking through the script a bit.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer