folder-size.ps1 - powershell script by ActiveXperts Software
folder-size.ps1 checks the size of a folder.
Use folder-size.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select folder-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.
folder-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
# Folder-Size.ps1
# Description:
# Checks the size of the specified folder
# Declare Parameters:
# 1) strHost (string) - Hostname or IP address
# 2) strPath (string) - The path of the folder you want to check (UNC allowed)
# 3) numLimitMB (int) - Limit, in MB
# 4) strAltCredentials (string, optional) - Alternate credentials
# Usage:
# .\Folder-Size.ps1 '<Hostname | IP>' '<UNCpath>' <max_sizeMB> '[alt-credentials]'
# Sample:
# .\Folder-Size.ps1 'localhost' '\\localhost\c$\temp' 300
#################################################################################
# -- Declare Parameters
param( [string]$strHost = '', [string]$strPath = '', [int]$numLimitMB = -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( $strHost -eq '' -or $strPath -eq '' -or $numLimitMB -lt 0 )
{
$res = 'UNCERTAIN: Parameter error - Usage: .\Folder-Size.ps1 "<Hostname | IP>" "<directory>" <value in MB> "[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
}
}
# -- Check Path
if( -not ( Test-Path $strPath ) )
{
echo 'UNCERTAIN: Path not found'
exit
}
# -- Check directory
$colItems = ( Get-ChildItem $strPath -recurse | Measure-Object -property length -sum )
$size = $colItems.sum / 1MB
if( $objNmRemoteServer -ne $null )
{
$objNmRemoteServer.Disconnect( $strCredentials )
}
if( $size -le $numLimitMB )
{
$res = 'SUCCESS: '
}
else
{
$res = 'ERROR: '
}
# -- Print script result
$res += 'Directory size = ' + $size + ' MB maximum allowed = ' + $numLimitMB + ' MB DATA:' + [Math]::Ceiling( $size )
echo $res
#################################################################################
# // --- Catch script exceptions ---
#################################################################################
trap [Exception]
{
$res = 'UNCERTAIN: ' + $_.Exception.Message
echo $res
exit
}
