Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

file-content.ps1 - powershell script by ActiveXperts Software

file-content.ps1 checks the contents of a file by matching a pattern or regular expression.

Use file-content.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select file-content.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-content.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-Content.ps1
# Description:
#     Check the contents of the file
# Declare Parameters:
#     1) strPath (string) - UNC formatted file path
#     2) strPattern (string) - Search for this pattern in the file
#     3) strAltCredentials (string, optional) - Specify an empty string to use Network 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-Content.ps1 '<\\Server\Share\Path>' '<pattern>' '[alt-credentials]'
# Sample:
#      .\File-Content.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-Content.ps1 "<\\Server\Share\Path>" "<pattern>" "[altCredentials]"'
  echo $res
  exit
}

# -- Declare local variables by assigning an initial value to it
$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 'getCredentials' is implemented in '_activexperts.ps1'
  if( ( AxGetCredentials $strHost $strAltCredentials ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS )
  {
    echo $strExplanation
  }
}

$exists = Test-Path $strPath

# -- Checks for the file existence
if( -not $exists )
{
  $res = 'ERROR: File ' + $strPath + ' does not exist.'
  echo $res
  exit
}  

$content = select-string -pattern $strPattern -path $strPath

if( $content -eq $null )
{
  $res = 'ERROR: Pattern [' + $strPattern + '] was not found.'
}
else
{
  $res = 'SUCCESS: Pattern [' + $strPattern + '] was found.'
}

# -- Print script result 
echo $res


#################################################################################
# // --- Catch script exceptions ---
#################################################################################

trap [Exception]
{
  $res = 'UNCERTAIN: ' + $_.Exception.Message
  echo $res
  exit
}