Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

disk-drives.ps1 - powershell script by ActiveXperts Software

disk-drives.ps1 checks all disks on a host.

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


disk-drives.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
#     Disk-Drives.ps1
# Description:
#     This function checks all disks on a computer specified by strHost.
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to monitor
#     2) 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:
#     .\Disk-Drives.ps1 '<Hostname | IP>' '[alt-credentials]'
# Sample:
#     .\Disk-Drives.ps1 'localhost'
#################################################################################

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

# -- Declare local variables by assigning an initial value to it
$strExplanation = ''
$strDisks = ''
$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 )
  {
    $res = 'UNCERTAIN: ' + $strExplanation
    echo $res
    exit
  }
}

# -- Get WMI object
$strWmi = 'Win32_DiskDrive'

if( $objAltCredentials -eq $null )
{
  $objWMIService = Get-WmiObject -ComputerName $strHost -Class $strWmi -ErrorVariable Error -ErrorAction SilentlyContinue
}
else
{
  $objWMIService = Get-WmiObject -ComputerName $strHost -Class $strWmi -Credential $objAltCredentials -ErrorVariable Error -ErrorAction SilentlyContinue
}

if( $Error -ne '' )
{
  $res = 'UNCERTAIN: ' + $Error
  echo $res
  exit
} 

if( $objWMIService -eq $null )
{
  $res = 'ERROR: Unable to access '' + $strHost + ''. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down'
  echo $res
  exit
}

foreach( $objDisk in $objWMIService )
{
  if( $objDisk.Status -ne 'OK' )
  {
    $res = 'ERROR: Status of Disk [' + $objDisk.Caption + '] is: [' + $objDisk.Status + ']'
    echo $res
    exit
  }
  
  if( $strDisks -ne '' )
  {
    $strDisks = $strDisks + ', '
  }

  $strDisks = $strDisks + '[' + $objDisk.Caption + ']'
}

# -- Print script result
$res = 'SUCCESS: All disks are OK; disks checked=' + $strDisks
echo $res
exit


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

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