Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

share.ps1 - powershell script by ActiveXperts Software

share.ps1 checks the existence of a share on a host.

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


share.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
#     Share.ps1
# Description:
#     Check the existence of a share on a (remote) computer. Use network monitor service credentials to access the (remote) computer
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to check
#     2) strShare (string) - Name of the share
#     3) 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:
#     .\Share.ps1 '<Hostname | IP>' '<Share>' '[alt-credentials]' 
# Sample:
#     .\Share.ps1 'localhost' 'C$'
#################################################################################

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

# Declare local variables by assigning an initial value to it
$strExplanation = ''
$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 "AxGetCredentials" is implemented in "activexperts.ps1"
  if( ( AxGetCredentials $strHost  $strAltCredentials ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS )
  {
    echo $strExplanation
    exit
  }
}

# Get WMI object
$strWmi = 'Win32_Share'   

if( $objAltCredentials -eq $null )
{
  $objWmi = Get-WmiObject -ComputerName $strHost -Class $strWmi -ErrorVariable Error -ErrorAction SilentlyContinue
}
else
{
  $objWmi = Get-WmiObject -ComputerName $strHost -Class $strWmi -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( $objWmi -eq $null )
{
  $res = 'UNCERTAIN: 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
}


# -- Print script result 
foreach( $strWmiShare in $objWmi )
{
  if( $strWmiShare.Name -eq $strShare )
  {
    if( $strWmiShare.Status -eq 'OK' )
    {      
      $res = 'SUCCESS: Status of share [\\' + $strHost + '\' + $strShare + ']: ' + $strWmiShare.status
    }
    else
    {
      $res = 'ERROR: Status of share [\\' + $strHost + '\' + $strShare + ']: ' + $strWmiShare.status 
    }
    echo $res
    exit
  }
}

$res = 'ERROR: Share [\\' + $strHost + '\' + $strShare + '] does not exist'
echo $res
exit


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

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