file-lastline.ps1 - powershell script by ActiveXperts Software
file-lastline.ps1 checks the last line of a file for a pattern.
Use file-lastline.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select file-lastline.ps1. Configure the required parameter, or press 'Load a working sample'.
In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.
file-lastline.ps1 script code
################################################################################# # ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V. # For more information about ActiveXperts Network Monitor, visit the ActiveXperts # Network Monitor web site at http://www.activexperts.com ################################################################################# # Script # File-LastLine.ps1 # Description: # Check last line of a file for a pattern # Declare Parameters: # 1) strPath (string) - UNC formatted file path # 2) strPattern (string) - Search for this pattern in the last line of the file # 3) strAltCredentials (string, optional) - Specify an empty string to use Metwork Monitor service credentials. # To use alternate credentials, enter a server that is defined in Server Credentials table. # (To define Server Credentials, choose Tools->Options->Server Credentials)" # Usage: # .\File-LastLine.ps1 '<\\Server\Share\Path>' '<pattern>' '[alt-credentials]' # Sample: # .\File-LastLine.ps1 '\\localhost\c$\windows\windowsupdate.log' 'required' ################################################################################# # -- Declare Parameters param( [string]$strPath = '', [string]$strPattern = '', [string]$strAltCredentials = '' ) # -- Use _activexperts.ps1 with common functions . 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1' ################################################################################# # // --- Main script --- ################################################################################# # -- Clear screen and clear error cls $Error.Clear() # -- Validate parameters, return on parameter mismatch if( $strPath -eq '' -or $strPattern -eq '' ) { $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\File-LastLine.ps1 "<\\Server\Share\Path>" "<pattern>" "[alt-credentials]"' echo $res exit } # -- Declare local variables by assigning initial value $strExplanation = '' $objAltCredentials = $null # If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings if( $strAltCredentials -ne '' ) { # Get the Alternate Credentials object. Function "AxGetCredentials" is implemented in "activexperts.ps1" if( ( AxGetCredentials $strHost $strAltCredentials ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS ) { echo $strExplanation exit } } $exists = Test-Path $strPath # -- Checks for the file existence if( -not $exists ) { $res = 'ERROR: File ' + $strPath + ' does not exist.' echo $res exit } $content = Get-Content $strPath $result = $content[-1] -match $strPattern if( $result -ne 0 ) { $res = 'SUCCESS: Pattern[' + $strPattern + '] was found in line [' + ( $content.count -1 ) + '].' } else { $res = 'ERROR: Pattern[' + $strPattern + '] was not found in line[' + ($content.count - 1) + '].' } # -- Print script result echo $res exit ################################################################################# # // --- Catch script exceptions --- ################################################################################# trap [Exception] { $res = 'UNCERTAIN: ' + $_.Exception.Message echo $res exit }