Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

drive-exists.ps1 - powershell script by ActiveXperts Software

drive-exists.ps1 checks whether a drive exists.

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


drive-exists.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
#     Drive-Exists.ps1
# Description:
#     Checks if the drive letter exists.
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to monitor
#     2) strDrive (string) - Drive letter of the drive your want to monitor
#     3) strAltCredentials (string, optional) - The alternate credentials of the chosen host
# Usage:
#     .\DriveExists.ps1 '<Hostname | IP>' '<Drive Letter>' '[alt credentials]'
# Sample:
#     .\DriveExists.ps1 'localhost' 'c:'
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [string]$strDrive = '', [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 $strDrive -eq '' )
{
  $res = 'UNCERTAIN: Parameter error - Usage: .\DriveExists.ps1 "<Hostname | IP>" "<Drive Letter>" "[alt credentials]"'
  echo $res
  exit
}

# -- Declare local variables by assigning initial value
$strExplanation = ''
$objAltCredentials = $null
$strFilter = 'DeviceID = "' + $strDrive + '"'
$strWmi = 'Win32_LogicalDisk'

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

if( $objAltCredentials -eq $null )
{
  $objDisks = Get-WmiObject -ComputerName $strHost -Class Win32_LogicalDisk -Filter $strFilter  -ErrorVariable Error -ErrorAction SilentlyContinue
}
else
{
  $objDisks = Get-WmiObject -ComputerName $strHost -Class Win32_LogicalDisk -Filter $strFilter -Credential $objAltCredentials -ErrorVariable Error -ErrorAction SilentlyContinue
}

# If anything went wrong Powershell sets the error in the global array $Error
if( $Error -ne '' )
{
  $res = 'UNCERTAIN: ' + $Error
  echo $res
  exit
} 

if( $objDisks -eq $null )
{
  $res = 'ERROR: Drive [' + $strDrive + '] was not found on host [' + $strHost + ']'
}
else
{
  $res = 'SUCCESS: Drive [' + $strDrive + '] was found on host [' + $strHost + ']'
}

# -- Print script result
echo $res
exit


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

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