Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

service.ps1 - powershell script by ActiveXperts Software

service.ps1 checks whether a specified service is running.

Use service.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select service.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.


service.ps1 script code

#################################################################################
# ActiveXperts Network Monitor PowerShell script.
# For more information about ActiveXperts Network Monitor, visit the ActiveXperts 
# Network Monitor web site at http://www.activexperts.com
#################################################################################
# Script
#     Service-CheckService.ps1
# Description:
#     Checks if a service, specified by strService, is running on the machine specified by strHost.
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to check
#     2) strService (string) - Name of the service
#     3) strCredentials (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:
#      .\Service-CheckService.ps1 '<Hostname | IP>' '<Service name>' '[alt-credentials]' 
# Sample:
#      .\Service-CheckService.ps1 'localhost' 'AxNmSvc'
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [string]$strService = '', [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 $strService -eq '' )
{
  echo 'UNCERTAIN: Invalid number of parameters - Usage: .\Service.ps1 "<Hostname | IP>" "<Service Name>" "[alt-credentials]"'
  exit
}

# Declare local variables by assigning an initial value to it
$lstServices = $null

$objAltCredentials = $null
$strExplanation = ''

# 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
  }
}

$lstServices = @( @( $strService, '' ),
                  @( '','' ) )

# -- Check processes
if( ( AxCheckServices $strHost ([ref]$lstServices) $objAltCredentials ([ref]$strExplanation) ) -ne $AXSUCCESS )
{
  echo $strExplanation
  exit
}


# -- Print script result
$res = 'SUCCESS: Service [' + $strService + '] is running on [' + $strHost + ']'
echo $res
exit


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

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