Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

services.ps1 - powershell script by ActiveXperts Software

services.ps1 checks whether a service is running on a host.

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


services.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
#     Services.ps1
# Description:
#     Checks if 1 or more services are running on the machine specified by strHost.
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to check
#     3) strServices (string) - List of services; the services are separated by the ';' character
#     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.ps1 '<Hostname | IP>' '<strService_1;strService_2;..;service_n>' '[alt-credentials]' 
# Sample:
#      .\Service.ps1 'localhost' 'ActiveXperts Network Monitor;power'
#################################################################################

# -- Declare Parameters
param ( [string]$strHost = '', [string]$strServices = '', [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
$lstProcesses = $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
  }
}

$arrServicesName = $strServices.Split( ';' )

# -- Split services name and put each name in an array with an empty description field
foreach( $strServiceName in $arrServicesName )
{
  $strServiceName += ',' # No need to specify a description!
  $arrServicesNameDesc += @( $strServiceName )
}

# -- Put each array created above into another array (bi-dimensional)
foreach( $arrServices in $arrServicesNameDesc )
{
  $arrService = $arrServices.Split( ',' )
  $lstServices += @( , @( $arrService[0], $arrService[1] )  )
}


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

# -- Print script result
$res = 'SUCCESS: Services [' + $strServices + '] are running on [' + $strHost + ']'
echo $res
exit


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


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