file-change.ps1 - powershell script by ActiveXperts Software
file-change.ps1 checks whether a file has changed on a host.
Use file-change.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select file-change.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-change.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-Change.ps1
# Description:
# Check if a file has changed on a computer. Use network monitor service credentials to access the (remote) computer.
# Declare Parameters:
# 1) strPath (string) - UNC formatted file path
# 2) 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:
# Usage:
# .\File-Change.ps1 '<\\Server\Share\Path>' '[alt-credentials]'
# Sample:
# .\File-Change.ps1 '\\localhost\c$\windows\windowsupdate.log'
#################################################################################
# -- Declare Parameters
param( [string]$strPath = '', [string]$strAltCredentials = '' )
# -- Use _activexperts.ps1 with common functions
. 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1'
#################################################################################
# // --- Private Functions section ---
#################################################################################
function getCacheValue( $strEntry )
{
try
{
# Hide errors
$ErrorActionPreference= 'silentlycontinue'
# Get cached moddate from registery
$key = 'HKLM:\Software\ActiveXperts\Network Monitor\Server\VBScript_Cache\'
$res = [string]( Get-ItemProperty -Path $key -Name $strEntry | select -exp $strEntry )
return $res
}
catch
{
$res = 'UNCERTAIN: File was not monitored before'
echo $res
exit
}
}
function extractPlainFile( $strPath )
{
$strPlainFile = $strPath.Split( '\' )
$strPlainFile = $strPlainFile[ $strPlainFile.Count-1 ]
return $strPlainFile
}
function setCacheValue( $strEntry, $strData )
{
$regKey = "HKLM:\Software\ActiveXperts\Network Monitor\Server\VBScript_Cache\"
if( -not ( Test-Path $regKey ) )
{
New-Item $regKey -ItemType Registry -Force | Out-Null
}
New-ItemProperty $regKey -Name $strEntry -Value $strData -Force | Out-Null
}
#################################################################################
# // --- Main script ---
#################################################################################
# -- Clear screen and clear error
cls
$Error.clear()
# -- Validate parameters, return on parameter mismatch
if( $strPath -eq '' )
{
$res = 'UNCERTAIN: Invalid number of parameters - Usage: .\File-Change.ps1 "<\\Server\Share\Path>" "[alt-credentials]"'
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 )
{
$res = 'UNCERTAIN: ' + $strExplanation
echo $res
}
}
$exists = Test-Path $strPath
# -- Checks for the file existence
if( -not $exists )
{
$res = 'ERROR: File ' + $strPath + ' does not exist.'
echo $res
exit
}
# -- Get new moddate from file
$strModDate = Get-Item $strPath | select LastWriteTime
$strModDate = $strModDate.LastWriteTime.ToString( 'MM/dd/yyyy h:mm:ss tt' )
$strPlainFile = extractPlainFile( $strPath )
$strPrevModDate = getCacheValue( $strPlainFile )
setCacheValue $strPlainFile $strModDate
if( $strPrevModDate -eq '' )
{
$res = "UNCERTAIN: File was not monitored before"
echo $res
exit
}
if( $strPrevModDate -ne $strModDate )
{
$res = "ERROR: File has changed since last check"
}
else
{
$res = "SUCCESS: File has not changed since last check"
}
# -- Print script result
echo $res
#################################################################################
# // --- Catch script exceptions ---
#################################################################################
trap [Exception]
{
$res = 'UNCERTAIN: ' + $_.Exception.Message
echo $res
exit
}
