Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

memory-pagespersecond.ps1 - powershell script by ActiveXperts Software

memory-pagespersecond.ps1 checks pages per second swapped on a host.

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

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

# 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
} 
$nPages = $objMem.PagesPerSec
$nDiffPages = $nMaxPages - $nPages

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

# -- Print script result
$res += 'Pages per second=[' + $nPages + '], maximum allowed=[' + $nMaxPages + '] DATA: ' + $nPages
echo $res
exit


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

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