Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

file-count.ps1 - powershell script by ActiveXperts Software

file-count.ps1 counts the number of files in a directory and all subdirectories.

Use file-count.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select file-count.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-count.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-Count.ps1
# Description:
#     Count the number of files in a directory and all subdirectories
# Declare Parameters:
#     1) strPath (string) - UNC formatted file path
#     2) bIncludeDirsInCount (int) - Also count a directory as a file
#     3) nMax (int) - Maximum number of files (and directories) allowed
#     4) 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:
# Usage:
#      .\File-Count.ps1 '<\\Server\Share\Path>' '<$true | $false>' <Max_Files> '[alt-credentials]'
# Sample:
#      .\File-Count.ps1 '\\localhost\c$\windows\system32' 'True' 10000
#################################################################################

# -- Declare Parameters
param( [string]$strPath = '', [string]$bIncludeDirsInCount = '', [int]$nMax = -1, [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 '' -or $bIncludeDirsInCount -eq '' -or $nMax -lt 0 )
{
  $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\File-Count.ps1 "<\\Server\Share\Path>" "<$true | $false >" <Max_Files> "[alt-credentials]"'
  echo $res
  exit
}

# -- Declare local variables by assigning initial value
$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.'
  echo $res
  exit
}

$nCountFiles = @( ( get-childitem $strPath | ?{!( $_.PSIsContainer ) } ) ).Count

if( $bIncludeDirsInCount -eq $true )
{
  $nCountDirectories =  @( ( get-childitem $strPath | ?{ $_.PSIsContainer } ) ).Count
  $nTotalCount = $nCountFiles + $nCountDirectories
}
else
{
  $nTotalCount = $nCountFiles
}

if( $nTotalCount -gt $nMax )
{
  $res = 'ERROR: '
}
else
{
  $res = 'SUCCESS: '
}

# -- Print script result
$res += 'Number of files=[' + [int]$nTotalCount + '], maximum allowed=[' + $nMax + '] DATA: ' + [int]$nTotalCount
echo $res


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

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