Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

website.ps1 - powershell script by ActiveXperts Software

website.ps1 reads data from a web page and searches for a pattern.

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


website.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
#     WebSite.ps1
# Description: 
#     Read data from a web page and search for a predefined pattern. 
#     This function uses ActiveXperts Network Component.
#     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased.
#     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
# Declare Parameters:
#     1) strURL (string) - URL of the web site, without http or https prefix
#     2) strPattern (string) - Text pattern to search for in the specified site
# Usage:
#     .\WebSite '<URL>' '<Text Pattern>'
# Sample:
#     .\WebSite 'www.activexperts.com/network-monitor/demopage' 'Welcome to ActiveXperts Demo Page'
#################################################################################

# -- Declare Parameters
param( [string]$strUrl = '', [string]$strPattern = '' )


#################################################################################
# // --- Main script ---
#################################################################################

# -- Clear screen and clear error
cls
$Error.Clear()

# -- Validate parameters, return on parameter mismatch
if( $strUrl -eq '' -or $strPattern -eq '' )
{
  $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\WebSite.ps1 <URL> <Text pattern>'
  echo $res
  exit
}

# The script
$objHttp = new-object -comobject AxNetwork.HttpEx

$strData = $objHttp.Get( $strUrl )

if( $objHttp.LastError -ne 0 ) 
{
  $res = 'UNCERTAIN: No response received from remote server'
  echo $res
  exit
}

if( $strData -Match $strPattern )
{
  $res = 'SUCCESS: Web site is available, text pattern found DATA: 1'
}  
else
{
  $res = 'ERROR: Web site is available, text pattern not found DATA: 0'
}

echo $res
exit


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

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