PowerShell Script to monitor strings in Log file

PowerShell Script to monitor strings in Log file

Hello,

I have written a script to monitor certain strings in a Log file. The script works fine on the server itself but not via opmanager.

Please help.

param
(
    [Parameter(Position=1)]
    [DateTime]$StartDate = (Get-Date -Hour 0 -Minute 0 -Second 0 -Millisecond 0),
    [Parameter(Position=2)]
    [DateTime]$EndDate = (Get-Date -Hour 23 -Minute 59 -Second 59 -Millisecond 999),
    [Parameter(Position=3)]
    $FilePath = "c:\qp_data.log",
    [Parameter(Position=4)]
    $Strings = @("Getting", "tcp")
)

$data = Get-Content $filepath # logfile path

write-host '------------------------------------------------------'
write-host '------------------------------------------------------'
write-host Total lines read from file $data.count # printing stats of
write-host '------------------------------------------------------'

$match_string = ''
$IsMatchingRecordFound = ''
#looping throgh all lines
foreach ($line in $data)
{   
    #fetching date and converting into string format
    [DateTime]$date = [DateTime]::ParseExact($line.substring(1, 21), "MMddyyyy-HH:mm:ss:fff", (New-Object System.Globalization.CultureInfo "en-US"))
   
    #comparing date date with start and end
    if (($date -gt $StartDate) -and ($date -lt $EndDate))
    {           
        #matching string and array from file data
        $match_string =   $line | Select-String -Pattern $Strings -SimpleMatch
        $match_string
        if([string]::IsNullOrEmpty($match_string))
        {                                      
        }
        else
        {           
                $IsMatchingRecordFound="Yes"           
        }
    }
    $match_string=''
}
IF([string]::IsNullOrEmpty($IsMatchingRecordFound))
{                                      
    write-host 'No Records Found.'
}


$empty_StringMessage
write-host '------------------------------------------------------'
write-host '------------------------------------------------------'




















































When i run through Opmanager i get an error

D:\ManageEngine\OpManager\temp\OpManager_1201_1405533564435.ps1 : Cannot proces s argument transformation on parameter 'StartDate'. Cannot convert value "qpswe b1-qa.srsi-qps.net" to type "System.DateTime". Error: "The string was not recog nized as a valid DateTime. There is a unknown word starting at index 0." At line:1 char:35

I believe that As far as the error message, it seems that the Opmanager is passing "qpswe
b1-qa.srsi-qps.net" as the first argument. It should pass no arguments to use the defaults.













                New to ADManager Plus?

                  New to ADSelfService Plus?