Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

memory-committed.ps1 - powershell script by ActiveXperts Software

memory-committed.ps1 checks commited memory on a host.

Use memory-committed.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select memory-committed.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-committed.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-Committed.ps1
# Description: 
#     Check commited memory (MB) on the computer specified by strHost
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to check
#     2) nMaxCommittedMB (int) - Maximum allowed committed memory (in MB)
#     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:
#     .\Memory-Committed.ps1 '<Hostname | IP>' <Max_MB> '[alt-credentials]'
# Sample:
#     .\Memory-Committed.ps1 'localhost' 800
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [int]$nMaxMB = 0, [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 $nMaxMB -eq 0 )
{
  $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\Memory-Committed.ps1 "<Hostname | IP>" <Max_MB> "[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
  }
}

# -- Get WMI object
$strWmi = 'Win32_PerfFormattedData_PerfOS_Memory'

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
}

$nCommittedMB = [math]::round( ( $objMem.CommittedBytes / ( 1024 * 1024 ) ),0 )
$nDiffMB = $nMaxMB - $nCommittedMB

if( $nDiffMB -gt 0 )
{
  $res =  'SUCCESS: '
}
else
{
  $res = 'ERROR: '
}

# -- Print script result
$res += 'Committed memory=[' + $nCommittedMB + ' MB], maximum allowed=[' + $nMaxMB + ' MB] DATA: ' + $nCommittedMB
echo $res
exit


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

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