Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

file-exists.ps1 - powershell script by ActiveXperts Software

file-exists.ps1 checks whether a file exists.

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


file-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
#     File-Exists.ps1
# Description:
#     Checks if the specified file exists
# Declare Parameters:
#     1) strPath (string) - Path to the file to check
#     2) strAltCredentials (string, optional) - Alternate credentials
# Usage:
#      .\File-Exists.ps1 '<UNC path>' '[alt-credentials]'
# Sample:
#      .\File-Exists.ps1 '\\localhost\c$\windows\explorer.exe'
#################################################################################

# -- Declare Parameters
param( [string]$strPath = '', [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( $strPath -eq '' )
{
  $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\File-Size.ps1 "<Hostname | IP>" "<file>" "[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
  }
}

$exists = Test-Path $strPath

# Checks for the file existence
if( -not $exists )
{
  $res = 'ERROR: File [' + $strPath + '] does not exist.'
}
else
{
  $res = 'SUCCESS: File [' + $strPath + '] exists.'
}
  
# -- Print script result
echo $res
exit


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

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