Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

ftp.ps1 - powershell script by ActiveXperts Software

ftp.ps1 checks whether a file existens on an FTP server.

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


ftp.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
#     Ftp.ps1
# Description:
#     Login to an FTP server, change the directory and check for file existence.
#     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) - Host name or IP address of the FTP host
#     2) strAccount (string) - FTP account; use #anonymous# for anonymous FTP access
#     3) strPassword (string) - FTP password; use an e-mail address for anonymous FTP access
#     4) strDir (string) - The directory where the file should be located
#     4) strFile (string) - The file to check 
# Usage:
#     .\Ftp.ps1 '<Hostname | IP>' '<Account>' '<Password>' '<Directory>' '<File>'
# Sample:
#     .\Ftp.ps1 'ftp.activexperts-labs.com' 'anonymous' 'me@myself.me' 'samples/network-monitor' 'readme.txt'
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [string]$strAccount = '', [string]$strPassword = '', [string]$strDir = '', [string]$strFile = '' )

# -- 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 $strAccount -eq '' -or $strPassword -eq '' -or $strDir -eq '' -or $strFile -eq '' )
{
  $res = 'UNCERTAIN: Parameter error - Usage: .\FTP.ps1 "<Hostname | IP>" "<Account>" "<Password>" "<Directory>" "<File>"'
  echo $res
  exit
}

$objFtpServer = new-object -comobject AxNetwork.FtpServer
$objFtpServer.Initialize
$objFtpServer.Connect( $strHost, $strAccount, $strPassword )
  
# -- Use binary transfer for GetFile calls
$objFtpServer.BinaryTransfer = $true

# -- Change directory
$objFtpServer.ChangeDir( $strDir )

if( $objFtpServer.LastError -ne 0 )
{
  $res = 'UNCERTAIN: Failed to change directory, error #' + $objFtpServer.LastError + ': ' + $objFtpServer.GetErrorDescription( $objFtpServer.LastError )
  echo $res
  exit
}

# -- Iterate over all files
$objFtpFile = $objFtpServer.FindFirstFile()

while( $objFtpServer.LastError -eq 0 )
{
  if( $objFtpFile.Name -eq $strFile )
  {
    $found = $true
    $objFtpServer.Disconnect()
  }
  $objFtpFile = $objFtpServer.FindNextFile()
}

if( $found -eq $true )
{
  $res = 'SUCCESS: File [' + $strFile + '] found on FTP server [' + $strHost + ']'
}
else
{
  $res = 'ERROR: File [' + $strFile + '] not found on FTP server [' + $strHost + ']'
} 

# -- Print script result
echo $res
exit  

$objFtpServer.Disconnect()


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

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