memory.ps1 - powershell script by ActiveXperts Software
memory.ps1 checks memory usage and available memory on a host.
Use memory.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select memory.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.
memory.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:
# Memory.ps1
# Description:
# Checks memory usage on a (remote) computer
# Declare Parameters:
# 1) strHost (string) - Hostname or IP address of the computer you want to monitor
# 2) strFlag (string) - Either 'free' or 'used'
# 3) numLimitMB (int) - Limit, in MB
# 4) strAltCredentials (string, optional) - Alternate credentials
# Usage:
# .\Memory.ps1 '<Hostname | IP>' '<Free | Used>' <numLimitMB> '[alt-credentials]'
# Sample:
# .\Memory.ps1 'localhost' 'Free' 50
#################################################################################
# -- Declare Parameters
param( [string]$strHost = '', [string]$strFlag = '', [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 $strFlag -eq '' -or $numLimitMB -lt 0 )
{
$res = 'UNCERTAIN: Parameter error - Usage: .\Memory.ps1 "<Hostname | IP>" "<Free> | Used>" <numLimitMB> "[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 'getCredentials' is implemented in '_activexperts.ps1'
if( ( AxGetCredentials $strHost $strAltCredentials ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS )
{
echo $strExplanation
exit
}
}
# -- Get WMI object
$strWmi = 'Win32_OperatingSystem'
if( $objAltCredentials -eq $null )
{
$objMem = Get-WmiObject -ComputerName $strHost -Class $strWmi -ErrorVariable Error -ErrorAction SilentlyContinue
}
else
{
$objMem = Get-WmiObject -ComputerName $strHost -Class $strWmi -Credential $objAltCredentials -ErrorVariable Error -ErrorAction SilentlyContinue
}
# -- If anything went wrong Powershell sets the error in the global array $Error
if( $Error -ne '' )
{
$res = 'UNCERTAIN: ' + $Error
echo $res
exit
}
if( $objMem -eq $null )
{
$res = 'UNCERTAIN: Unable to connect. Please make sure that PowerShell and WMI are both installed on the monitered system. Also check your credentials'
echo $res
exit
}
$freeMB = [math]::round( ( $objMem.FreePhysicalMemory / 1024 ), 0 )
$totalMB = [math]::round( ( $objMem.TotalVisibleMemorySize / 1024 ), 0 )
$usedMB = $totalMB - $freeMB
# -- Free memory
if( $strFlag.ToLower() -eq 'free' )
{
if( $freeMB -gt $numLimitMB )
{
$res = 'SUCCESS: '
}
else
{
$res = 'ERROR: '
}
$res = $res + 'Free physical memory=[' + $freeMB + ' MB], minimum required=[' + $numLimitMB + ' MB] DATA:' + $freeMB
}
# -- Used memory
if( $strFlag.ToLower() -eq 'used' )
{
if( $usedMB -lt $numLimitMB )
{
$res = 'SUCCESS: '
}
else
{
$res = 'ERROR: '
}
$res = $res + 'Used physical memory=[' + $usedMB + ' MB], maximum allowed=[' + $numLimitMB + ' MB] DATA: ' + $usedMB
}
# -- Print script result
echo $res
exit
#################################################################################
# // --- Catch script exceptions ---
#################################################################################
trap [Exception]
{
$res = 'UNCERTAIN: ' + $_.Exception.Message
echo $res
exit
}
