This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Log Parser Powershell - todays date in the file name

Hi,

I'm trying to use the log parser PowerShell template to look in a text file for a string.

I can get it to work on a file like the below.

logfile.log

but I cant work out how to add todays date into the script argument? As the system that creates the log file adds the date so every day its a new log file.

todays date is 17/10/2016

logfile2016-10-17.log

feel like it should be something like

c:\logfile$get-date format- yyyy-mm-dd.log, error502

  • I'm assuming you mean this one?  Based on the notes, the file name cannot have a space in it, which means your command will fail immediately.  That said, you could try this to see if it works:

    "C:\logfile$(Get-Date -Format 'yyyy-mm-dd').log",error,match,2

    The types of quotes might be important.  I'd also verify your syntax, I'm not sure if there is a copy/paste issue, or I'm looking at a different template, but the arguments you provided don't match up with the ones required by the script.

  • Hi,

    That didn't work, but in my simplifying of the question that might be why it didn't work.

    That is the correct template im using. Below is the argument im using, with the wrong servername

         "\\${ip}\AudaNotification\Logs\AudaReceiver $(Get-Date -Format 'yyyy-mm-dd').log",<Error> <Location>AudaReceiver:Worker <AppError>Web service is unavailable - will continue to poll. error code,match,2

    but the error im getting is

         Output:      ==============================================

         Message: File "\\********\AudaNotification\Logs\AudaReceiver $(Get-Date -Format 'yyyy-mm-dd').log" not found.

         Errors:      ==============================================

         Test-Path : Illegal characters in path.

         At line:15 char:9

         + if ( !$(Test-Path $logfile_path) )

         +         ~~~~~~~~~~~~~~~~~~~~~~~

             + CategoryInfo          : InvalidArgument: (C:\Windows\syst...yy-mm-dd').log":String) [Test-Path], ArgumentException

             + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.TestPathCommand

    but its not getting the date? and adding to the filename

    I have to do it remotely as the box its on does not have powershell - 2003 box. But need this to work as moving from a lot simpler system Hostmon that can do this with its eyes closed without powershell!!

  • Hi,

    Just to let people know I used a VBScript instead to perform this task

         Script Argument

    /file:"\\**********\f$\AudaNotification\Logs\AudaReceiver ${Date}.log" /search:"error"

         Script Body

    '/file: [required] This is the UNC location for the file to be searched.
    ' ${Date} is a wildcard you can put anywhere in the filename to indicate the datestamp(current date) as part of the filename.
    ' * ${Date} must be used with strDateFormat!!
    ' Example: /file:"//myserver/myshare$/myfile.txt" or /file:"${IP}/myshare/myfile.txt"
    '----------------------------------------------------------------------------------------------------------
    '/search: [required] This is the string to be searched for in the filename.\
    'Example: /search:"error" or /search:"failed"
    '----------------------------------------------------------------------------------------------------------
    'DateFormat: [only required if used with ${Date} in file] This is the configuration for the format your datestamps are in.
    strDateFormat = "yyyy_mm_dd"
    ' Usage:
    ' yyyy - 4 digit year
    ' yy - 2 digit year
    ' mm - 2 digit month
    ' m - 1 digit month
    ' dd - 2 digit day
    ' d - 1 digit day
    ' Seperators: "-", ".", "_", "/"
    'Example: strDateFormat = "mm-dd-yyyy" or strDateFormat = "yy_m_d"
    '----------------------------------------------------------------------------------------------------------
    'FailIf: [optional] Specify to fail the check if the search string is found or not found
    strFailIf = "found"
    ' Usage:
    ' found
    ' not found
    'Example: strFailIf = "found" or strFailIf = "not found"
    '#############################################################
    strFileName = Wscript.Arguments.Named("file") 'Get file name from argument
    If strFileName = "" Then
        Wscript.Echo "Message: You must specify a File Name like this: /file:""blah.txt"""
        Wscript.Quit(4)
    End If

    strSearch = Wscript.Arguments.Named("search") 'Get search string form argument
    If strSearch = "" Then
        Wscript.Echo "Message: You must specify a string to search for like this: /search:""ERROR"""
        Wscript.Quit(4)
    End If

    If strFailIf="" Then strFailIf="found" End If
    If (strFailIf="found") Then
    strFound=3
    strNotFound=0
    ElseIf (strFailIf="notfound" or strFailIf="not found") Then
    strFound=0
    strNotFound=3
    Else
    strFound=4
    strNotFound=4
    Wscript.Echo "Message: ERROR, set strFailIf"
        Wscript.Quit(4)
    End If

    yyyy = Datepart("yyyy",Date())
    yy = Right(yyyy, 2)
    m = Datepart("m",Date())
    mm = Right("0" & m, 2)
    d = Datepart("d",Date())
    dd = Right("0" & d, 2)
    strDateFormat = "yyyy-mm-dd"

    If (InStr(1,strFileName,"${Date}",1) <> 0 AND strDateFormat <> "") Then
    strDateFormat = replace(strDateFormat, "-", " & ""-"" & ")
    strFileName = Replace(strFileName,"${Date}",eval(strDateFormat),1,-1,1)
    Else
    Wscript.Echo "Message: Error with ${Date} and strDateFormat. Please review instructions at top of script."
        Wscript.Quit(4)
    End If

    strPattern = strSearch

    Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If Not objFSO.FolderExists("temp") Then
    objFSO.CreateFolder("temp")
    End If

    PrevLogFile =  "temp\" & strSearch & "-" & mm & "-"  & dd-1 & "-"  & yyyy & ".txt"
    If objFSO.FileExists(PrevLogFile) Then
    set objPrevLogFile = objFSO.GetFile(PrevLogFile)
    objPrevLogFile.Delete
    End If

    PreviousFinds = 0
    LogFile = "temp\" & strSearch & "-" & mm & "-"  & dd & "-"  & yyyy & ".txt"
    If objFSO.FileExists(LogFile) Then
    Set objLog = objFSO.OpenTextFile(LogFile,1, True)

    If (IsEmpty(objLog) = True ) Then PreviousFinds = 0 End If
    PreviousFinds = objLog.ReadAll
    End If

    If objFSO.FileExists( strFileName ) Then
      Set objFile = objFSO.GetFile(strFileName)
      If( IsEmpty( objFile ) = True ) Then
          WScript.Echo "Message: OBJECT NOT INITIALIZED"
          WScript.Echo "Statistic: 0"
          WScript.Quit(4)
        End If
        
      else
        WScript.Echo "Message: The file """ & strFileName & """ was not found!<BR>This could indicate that the file does not exist or you do not have the correct credentials for monitoring. <BR>"
        WScript.Echo "Statistic: 0"
        WScript.Quit(strNotFound)
    End If

    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = strPattern
    objRegEx.IgnoreCase = True
    Set objReadFile = objFSO.OpenTextFile(strFileName, ForReading)
    x=1
    Do Until objReadFile.AtEndOfStream
        strSearchString = objReadFile.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString) 
        If colMatches.Count > 0 Then
            For Each strMatch in colMatches  
       If int(x) > int(PreviousFinds) Then 
        Wscript.Echo "Statistic: " & PreviousFinds+1
        Wscript.Echo "Message: New search for """ & strSearch & """ was found: " & strSearchString
         Set objLog = objFSO.OpenTextFile(LogFile, 2, True)
         objLog.WriteLine(PreviousFinds+1)
         objLog.Close
        Wscript.Quit(StrFound)
       End If
      x=x+1
            Next
        End If
    Loop
    If int(x) >1 Then
    Wscript.Echo "Statistic: " & PreviousFinds
        Wscript.Echo "Message: New search for """ & strSearch & """ was found, but previous searches did find """ & strSearch & """."
        Wscript.Quit(StrFound) 
    End If
    Wscript.Echo "Statistic: " & PreviousFinds
    Wscript.Echo "Message: Search for """ & strSearch & """ was not found."
    Wscript.Quit(strNotFound)
    objReadFile.Close