file-size.ps1 - powershell script by ActiveXperts Software
file-size.ps1 checks the size of a file.
Use file-size.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select file-size.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-size.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-Size.ps1
# Description:
# Checks the file size
# Declare Parameters:
# 1) strPath (string) - Path to the file to check
# 2) maxFileSize (int) - Maximal file size allowed
# 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-Size.ps1 '<Hostname | IP>' '<file>' <size> '[alt-credentials]'
# Sample:
# .\File-Size.ps1 'localhost' 'c:\windows\explorer.exe' 900
#################################################################################
# -- Declare Parameters
param( [string]$strHost = '', [string]$strPath = '', [int]$maxFileSize = -1, [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 $maxFileSize -lt 0 )
{
$res = 'UNCERTAIN: Invalid number of parameters - Usage: .\File-Size.ps1 "<Hostname | IP>" "<file>" <size> "[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 '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
}
$objFile = Get-ChildItem $strPath
$nFileLength = [System.Math]::Round( ( $objFile.Length / 1024 ) )
if( $nFileLength -gt $maxFileSize )
{
$res = 'ERROR: '
}
else
{
$res = 'SUCCESS: '
}
# -- Print script result
$res += 'Filesize[' + $nFileLength + '] Maximum allowed [' + $maxFileSize + '] DATA: ' + $nFileLength
echo $res
#################################################################################
# // --- Catch script exceptions ---
#################################################################################
trap [Exception]
{
$res = 'UNCERTAIN: ' + $_.Exception.Message
echo $res
exit
}
