Parse log file using powershell

Hi,
Am pretty new to Powershell and would require anyone of your assistance in setting up a script which parse thru a log file and provide me output for my requirements below.
I would like to parse the Main log file for Barra Aegis application(shown below) using powershell.

Main log = C:\BARRALIN\barralin.log
Model specific log = C:\BARRALIN\log\WG*.log

Requirements :

1. scroll to the bottom of the log file and look for name called "GL Daily" and see the latest date which in the example log below is "20150203"
note : Name "GL Daily" and date keep changing in log file
2. Once entry is found i would like to have a check to see all 3 entries PREPROCESS, TRANSFER, POSTPROCESS are sucess.
3. If all 3 are success i would like to the script to identify the respective Model specific log number and print it out.
E.g if you see the sample log below for "GL Daily", it is preceded by number "1718" hence script should append the model log path with "WG00" along with 1718, finally it should look something like this  C:\BARRALIN\log\WG001718.log.
4. If all 3 items or anyone of them are in "failed" state then print the same log file info with WG001718.log

Any help on this would be much appreciated.

Thank You.

Main log file :

START BARRALINK            Check Auto Update                                                1716   43006  20150203 
    Trgt/Arch c:\barralin                                               
    PREPROCESS           success   0 preprocessor: no error                   
    TRANSFER             success   1 Host success: files received             
    POSTPROCESS          success   0 Postprocessor: no error                  
    CONFIRMATION         success   2 No Confirm needed                        
STOP  43105  20150203 

START Aegis                GL Monthly                                                       1716   43117  20150203 
    Trgt/Arch K:\barraeqr\aegis\qnt\gleqty                              
    PREPROCESS           success   0 preprocessor: no error                   
    TRANSFER             success   1 Host success: files received             
    POSTPROCESS          success   0 Postprocessor: no error                  
    CONFIRMATION         success   2 No Confirm needed                        
STOP  44435  20150203

START Aegis                UB Daily                                                       1717   43107  20150203 
    Trgt/Arch K:\barraeqr\aegis\qnt\gleqty                              
    PREPROCESS           success   0 preprocessor: no error                   
    TRANSFER             success   1 Host success: files received             
    POSTPROCESS          success   0 Postprocessor: no error                  
    CONFIRMATION         success   2 No Confirm needed                        
STOP  44435  20150203 


START Aegis                GL Daily                                                         1718   44437  20150203 
    Trgt/Arch K:\barraeqr\aegis\qnt\gleqty                              
    PREPROCESS           success   0 preprocessor: no error                   
    TRANSFER             success   1 Host success: files received             
    POSTPROCESS          success   0 Postprocessor: no error                  
    CONFIRMATION         success   2 No Confirm needed                        
STOP  50309  20150203 

 
February 3rd, 2015 12:45pm

I have not tested it but this should get you started.

$Source = Get-Content "c:\temp\test.log"
$Dest = "c:\temp"
$Prefix = "WG00"

$option = [System.StringSplitOptions]::RemoveEmptyEntries
$log = ""

$lineNumber = $Source.Count - 1
while ($lineNumber -ge 0) {
    $CurrentLine = $Source[$lineNumber]
    if ($CurrentLine -match "GL Daily") {
        #get number
        $numberArray = $CurrentLine.Split(" ", $option)
        $number = $numberArray[4]

        #get date
        $dateArray = $Source[($lineNumber+6)].Split(" ", $option)
        $date = $dateArray[2]

        #get status
        $logFilePath = "$Dest\$Prefix" + $number + ".log"

        If (($Source[($lineNumber+2)] -match "success") -and ($Source[($lineNumber+3)] -match "success") -and 
            ($Source[($lineNumber+4)] -match "success") -and ($Source[($lineNumber+5)] -match "success")) {
            Out-file -FilePath $logFilePath -InputObject "$number - All Successful" -Append
        } else {
            Out-file -FilePath $logFilePath -InputObject "$number - One or more failed" -Append
        }
        break        
    }
    $lineNumber--
}

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2015 1:57pm

We do not produce scripts on demand.  Please post a copy of your script and a specific quesiotn or error.

See forummpossting guidelines for help: https://social.technet.microsoft.com/Forums/en-US/a0def745-4831-4de0-a040-63b63e7be7ae/posting-guidelines?forum=ITCG

http://sincealtair.blogspot.com/2010/04/how-to-ask-questions-in-technical-forum.html

February 3rd, 2015 2:03pm

Thank you jalapeno42..that was really helpful.

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2015 11:03am

Thank You jrv for providing me the guidelines for posting things in a forum..i would have them as reference when posting items in forum.
February 5th, 2015 11:04am

Hi All,

I was writing a function in power shell to send email and i was looking to attach lines as and when required to the body of the email. but am not able to get this done..Here's my code

Function Email ()
{ 

$MailMessage = New-Object System.Net.Mail.MailMessage
$SMTPClient = New-Object System.Net.Mail.SmtpClient -ArgumentList "mailhost.xxx.com"
$Recipient = "to@xx.com"

If ($MessageBody -ne $null) 
{ 
$MessageBody = "The details of Barra $strsessionProduct model is listed below 
`rHostName : $localhost 
`r Model Run Date : $Date 
`r  Model Data Date : $DateList1 
`r`n Click for full job log"+ "\\"+$localhost+"\E$\Local\Scripts\Logs "
}

$MailMessage.Body = $MessageBody

If ($Subject -ne $null) {
$MailMessage.Subject = $Subject
}

$Sender = "to@xx.com"
$MailMessage.Sender = $Sender
$MailMessage.From = $Sender
$MailMessage.to.Add($Recipient)

If ($AttachmentFile -ne $null) { $MailMessage.Attachments.add($AttachmentFile)}
$SMTPClient.Send($MailMessage) 
}

$Subject = "Hello"  
$AttachmentFile = ".\barralin.log"
$MessageBody = "Add this line to Body of email along with existing"
Email -Recipient "" -Subject $Subject -MessageBody $MessageBody -AttachmentFile $AttachmentFile

as you can see before calling Email function i did add some lines to $MessageBody and was expecting that it would print the lines for $MessageBody in Email Function along with the new line. But thats not the case.

I have tried to make $MessageBody as an Array and then add contents to array

$MessageBody += "Add this line to Body of email along with existing"
$MessageBody = $MessageBody | out-string

Even this didnt work for me. Please suggest me any other means to get this done.
THank You

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 9:57am

Why try to use a function?  Appending a string is as simple as

$a = "one" $a += "two" $a

onetwo

You may want to try the Send-MailMessage cmdlet also.


February 20th, 2015 10:23am

My Script has multiple condition check and when every condition met it would send an email and including send-mailmessage cmdlet would add up more lines to the script.

Hence thought it would be easy for me to create a function and call it. But i can try using Send-MailMessage cmdlet.

But for the adding text to body of email. i did try the above option you have mentioned and was not able achieve the result.
Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 10:37am

You may wish to review this link about functions.
February 20th, 2015 10:54am

Hi jalapeno42,

here's what am trying to achieve, i have already declared a $Mbody outside the function and within the function am again assigning $Mbody to some values and overall my body of the email should contain

Success Value is equal to 10, in my case am getting only Value is equal to 10.

I have used your logic to create it as an array and concatenate but isnt working.

Function UpdateSuccess
{
$MBody += "Value is equal to 10"
$MBody = $MBody
Send-MailMessage -To $To -From $from -Body $MBody -SmtpServer "mailhost.gmail.com" -Subject $Subject -Port 25
}
Exit

Function UpdateFailure
{
$MBody += "Value is not equal to 10"
Send-MailMessage -To $To -From $from -Body $MBody -SmtpServer "mailhost.gmail.com" -Subject $Subject -Port 25
}


$To = "to@gmail.com"
$Subject = "Hello How are you"
$MBody = "Success"
$from = "from@gmail.com"
$a = "10"

If ($a -eq 10) {
UpdateSuccess
echo "Success"}
else { UpdateFailure
echo "Failure" }
any thoughts please??
Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 2:06pm

You cannot pass an array to a "Body" element.  A body element must be a pure string.

February 20th, 2015 2:09pm

hi jrv, thank you even if i pass pure string i wouldn't get expected email body as 

Success Value is equal to 10.

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 3:25pm

hi jrv, thank you even if i pass pure string i wouldn't get expected email body as 

Success Value is equal to 10.

That is what you asked for.  I was just commenting that you cannot, as you posted above, use an array.

What you are asking is very unclear to me.

February 20th, 2015 3:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics