ntp.ps1 - powershell script by ActiveXperts Software
ntp.ps1 queries a time server.
Use ntp.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select ntp.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.
ntp.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
# Ntp.ps1
# Description:
# Query a time server.
# 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) strHost (string) - Specifies the remote NTP server
# Usage:
# .\NTP '<Hostname | IP>'
# Sample:
# .\NTP 'ntp.activexperts-labs.com'
#################################################################################
# -- Declare Parameters
param( [string]$strHost = '' )
# -- 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( [string]$strHost -eq '' )
{
$res = 'UNCERTAIN: Invalid number of parameters - Usage: .\ntp.ps1 "<Hostname | IP>"'
echo $res
exit
}
# -- Declare local variables by assigning an initial value to it
$strError = ''
$objAltCredentials = $null
$objNtp = new-object -comobject AxNetwork.Ntp
$objNtp.GetTime( $strHost )
if( $objNtp.LastError -ne 0 )
{
$res = 'ERROR: #' + $objNtp.LastError + ': ' + $objNtp.GetErrorDescription( $objNtp.LastError ) + ' DATA: 0'
}
else
{
$res = 'SUCCESS: NTP server queried, Result: ' + ( Get-date -y $objNtp.Year -mo $objNtp.Month -day $objNtp.Day) + ' ' + ( Get-Date -h $objNtp.Hour -Minute $objNtp.Minute -s $objNtp.Second -displayhint time) + '; time-difference: ' + $objNtp.LocalOffsetSeconds + ' seconds DATA: ' + $objNtp.LocalOffsetSeconds
}
# -- Print script result
echo $res
exit
#################################################################################
# // --- Catch script exceptions ---
#################################################################################
trap [Exception]
{
$res = 'UNCERTAIN: ' + $_.Exception.Message
echo $res
exit
}
