Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

folder.vbs - vbscript script by ActiveXperts Software

folder.vbs checks folder size, count the number of files in a directory and all subdirectories, check the directory for a change.

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


folder.vbs script code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // © ActiveXperts Software B.V.
' //
' // For more information about ActiveXperts Network Monitor and VBScript, please
' // visit the online ActiveXperts Network Monitor VBScript Guidelines at:
' //    http://www.activexperts.com/support/network-monitor/online/vbscript/
' // 
' ///////////////////////////////////////////////////////////////////////////////  

Option Explicit
Const  retvalUnknown = 1        ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain)
Dim    SYSDATA, SYSEXPLANATION  ' SYSDATA is displayed in the 'Data' column in the Manager; SYSEXPLANATION in the 'LastResponse' column

' ///////////////////////////////////////////////////////////////////////////////
' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following 5 lines:
' Dim bResult
' bResult = CheckDirectorySize( "\\localhost\admin$", "", 300 )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"
' ////////////////////////////////////////////////////////////////////////////////////////

Function CheckDirectorySize( strHost, strAltCredentials, strPath, strLimitMB )
' Description: 
'     Check the directory size on a (remote) computer. Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strHost As String  - Hostname or IP address of the computer you want to monitor
'     2) strAltCredentials As String - Specify an empty string to use Metwork 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)'
'     3) strPath As String - UNC formatted directory path
'     4) strLimitMB As String - Maximum allowed directory size (MB)
' Usage:
'     CheckDirectorySize( "<Server>, "<Empty String | Server>", "<\\Server\Share\Path>", <Max_MB> )
' Sample:
'     CheckDirectorySize( "localhost", "", "\\localhost\admin$", 300  )

  Dim strAltLogin, strAltPassword
  Dim nSizeMB, nDiffMB
  
  CheckDirectorySize = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA             = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION      = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin        = ""
  strAltPassword     = ""
  
  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings, and logon
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
    If( Not netLogon( strHost, strAltLogin, strAltPassword, SYSEXPLANATION ) ) Then
      Exit Function
    End If    
  End If  

  nSizeMB                  = getFolderSizeMB( strPath )
  If( nSizeMB < 0 ) Then
    SYSEXPLANATION         = "Unable to retrieve directory size. Make sure that the directory exists and that Network Monitor can access it."
    CheckDirectorySize     = retvalUnknown 
  Else
    nDiffMB                = strLimitMB - nSizeMB
    SYSDATA                = nSizeMB
    SYSEXPLANATION         = "Directory size=[" & nSizeMB & " MB], maximum allowed size=[" & strLimitMB & " MB]"
    If( nDiffMB >= 0 ) Then
      CheckDirectorySize = True
    Else
      CheckDirectorySize = False
    End If
  End If

  ' If alternate login is used, logoff now   
  If( strAltLogin <> "" ) Then
    netLogoff( strHost )
  End If

End Function


' /////////////////////////////////////////////////////////////////////////////

Function CheckDirectoryCount( strHost, strAltCredentials, strPath, bIncludeDirsInCount, nMax )
' Description: 
'     Count the number of files in a directory and all subsirectories
' Parameters:
'     1) strHost As String  - Hostname or IP address of the computer you want to monitor
'     2) strAltCredentials As String - Specify an empty string to use Metwork 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)'
'     3) strPath - UNC formatted file path
'     4) bIncludeDirsInCount - Also count a directory as a file
'     5) nMax - Maximum number of files (and directories) allowed
' Usage:
'     CheckDirectoryCount( "<Server>, "<Empty String | Server>"", "<\\Server\Share\Path>", [ True | False ], <Max_Files> )
' Sample:
'     CheckDirectoryCount( "localhost", "", "\\localhost\c$\windows\system32", True, 10000 )

  Dim strAltLogin, strAltPassword

  Dim n  

  CheckDirectoryCount         = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA            = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION     = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager

  strAltLogin        = ""
  strAltPassword     = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings, and logon
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
    If( Not netLogon( strHost, strAltLogin, strAltPassword, SYSEXPLANATION ) ) Then
      Exit Function
    End If    
  End If 

  n  = cntFiles( strPath, bIncludeDirsInCount )
  If( n >= 0 ) Then
    SYSDATA               = n
    SYSEXPLANATION        = "Number of files=[" & n & "], maximum allowed=[" & nMax & "]"
    If( n > nMax ) Then
      CheckDirectoryCount          = False  
    Else
      CheckDirectoryCount          = True  
    End If
  End If

  ' If alternate login is used, logoff now   
  If( strAltLogin <> "" ) Then
    netLogoff( strHost )
  End If

End Function



' //////////////////////////////////////////////////////////////////////////////

Function CheckDirectoryChange( strHost, strAltCredentials, strPath )
' Description: 
'     Check the directory for a change. 
'     If the size of the directory changes with 1KB or more, the directory has changed.
' Parameters:
'     1) strHost As String  - Hostname or IP address of the computer you want to monitor
'     2) strAltCredentials As String - Specify an empty string to use Metwork 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)' 
'     3) strPath As String - UNC formatted directory path
' Usage:
'     CheckDirectoryChange( "<Server>, "<Empty String | Server>", "<\\Server\Share\Path>" )
' Sample:
'     CheckDirectoryChange( "localhost", "\\localhost\admin$", "" )
    
  Dim strAltLogin, strAltPassword    
  Dim strPrevSizeKB, strSizeKB

  CheckDirectoryChange  = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA               = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION        = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin           = ""
  strAltPassword        = ""
    
  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings, and logon
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
    If( Not netLogon( strHost, strAltLogin, strAltPassword, SYSEXPLANATION ) ) Then
      Exit Function
    End If    
  End If  

  strSizeKB                    = "" & getFolderSizeKB( strPath )
  If( strSizeKB = "-1" ) Then
    CheckDirectoryChange      = retvalUnknown  
    SYSDATA                   = ""
    SYSEXPLANATION            = "Directory does not exist"
    Exit Function
  End If    

  strPrevSizeKB                = getCacheValue( extractPlainFile( strPath ) )
  setCacheValue extractPlainFile( strPath ), strSizeKB
  If( strPrevSizeKB = "" ) Then
     CheckDirectoryChange      = True  
     SYSDATA                   = ""
     SYSEXPLANATION            = "Directory was not monitored before"
     Exit Function
  End If

  If( strPrevSizeKB <> strSizeKB ) Then
    CheckDirectoryChange      = False  
    SYSDATA                   = strSizeKB
    SYSEXPLANATION            = "Directory has changed since last check"
  Else
    CheckDirectoryChange      = True  
    SYSDATA                   = strSizeKB
    SYSEXPLANATION            = "Directory has not changed since last check"
  End If

  If( strAltLogin <> "" ) Then
    netLogoff( strHost )
  End If

End Function



' //////////////////////////////////////////////////////////////////////////////
' // --- Private Functions section ---
' // Private functions names should start with a lower case character, so they 
' // will not be listed in the Network Monitor's function browser.
' //////////////////////////////////////////////////////////////////////////////

Function netLogon( strHost, strAltLogin, strAltPassword, strSysExplanation )
  Dim objRemoteServer

  netLogon            = False
  strSysExplanation   = ""
  Set objRemoteServer = CreateObject( "ActiveXperts.RemoteServer" )

  If( strAltLogin = "" ) Then
    netLogon          = True
    Exit Function
  End If

  objRemoteServer.Connect strHost, strAltLogin, strAltPassword
  If( objRemoteServer.LastError <> 0 ) Then
    netLogon          = False
    strSysExplanation = "Login failed"
    Exit Function   
  End If

  netLogon            = True

End Function


' //////////////////////////////////////////////////////////////////////////////

Function netLogoff( strHost )

  Dim objRemoteServer

  netLogoff           = False

  Set objRemoteServer = CreateObject( "ActiveXperts.RemoteServer" )

  objRemoteServer.Disconnect strHost
  If( objRemoteServer.LastError <> 0 ) Then
      netLogoff       = False
      Exit Function   
  End If

  netLogoff           = True

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getCredentials( strHost, strAltCredentials, BYREF strAltLogin, BYREF strAltPassword, BYREF strSysExplanation )	

  Dim objNMServerCredentials
  
  strAltLogin = ""
  strAltPassword = ""
  strSysExplanation = ""
  
  getCredentials  = False    
  
  If( strAltCredentials = "" ) Then
    ' No alternate credentials specified, so login and password are empty and service credentials will be used
    getCredentials = True
    Exit Function
  End If
  
  Set objNMServerCredentials = CreateObject( "ActiveXperts.NMServerCredentials" )

  strAltLogin           = objNMServerCredentials.GetLogin( strAltCredentials )
  strAltPassword        = objNMServerCredentials.GetPassword( strAltCredentials )

  If( strAltLogin = "" ) Then
      getCredentials      = False
      strSysExplanation = "No alternate credentials defined for [" & strAltCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials"
      Exit Function
  End If   

  getCredentials = True 

End Function


' //////////////////////////////////////////////////////////////////////////////

Function getFileSizeKB( strPath )

' Description: 
'     Get the size of a file on a (remote) computer, in KB. 
'     Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strPath - UNC formatted file path

  Dim objFSO, objFile

  getFileSizeKB              = -1

On Error Resume Next  
  Set objFSO                 = CreateObject("Scripting.FileSystemObject")
  Set objFile                = objFSO.GetFile( strPath )
On Error Goto 0  
  If( Err.Number <> 0 ) Then
    getFileSizeKB          = -1
    Exit Function
  End If

  getFileSizeKB              = FormatNumber( objFile.Size / 1024, 0, 0, 0, 0 )

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getFolderSizeKB( strPath )
' Description: 
'   Get the size of a folderon a (remote) computer, in KB. 
'   Use network monitor service credentials to access the (remote) computer
' Parameters:
'   1) strPath - UNC formatted folder

  Dim objFSO, objFolder

  getFolderSizeKB            = -1

On Error Resume Next  
  Set objFSO                 = CreateObject("Scripting.FileSystemObject")
  Set objFolder              = objFSO.GetFolder( strPath )
On Error Goto 0  
  
  If( Err.Number <> 0 ) Then
    getFolderSizeKB        = -1
    Exit Function
  End If

  getFolderSizeKB            = FormatNumber( objFolder.Size / ( 1024 ), 0, 0, 0, 0 )

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getFolderSizeMB( strPath )

' Description: 
'   Get the size of a folderon a (remote) computer, in MB. 
'   Use network monitor service credentials to access the (remote) computer
' Parameters:
'   1) strPath - UNC formatted folder

  Dim objFSO, objFolder

  getFolderSizeMB            = -1

On Error Resume Next  
  Set objFSO                 = CreateObject("Scripting.FileSystemObject")
  Set objFolder              = objFSO.GetFolder( strPath )
On Error Goto 0  
  
  If( Err.Number <> 0 ) Then
    getFolderSizeMB        = -1
    Exit Function
  End If

  getFolderSizeMB            = CInt( objFolder.Size / ( 1024 * 1024 ) )

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getFileModDateString( strFile )

On Error Resume Next

  Dim fso, fsoFile, dtModDate

  getFileModDateString   = "" 

  Set fso                = CreateObject("Scripting.FileSystemObject")
  Set fsoFile            = fso.GetFile( strFile )
  dtModDate              = fsoFile.DateLastModified

  getFileModDateString   = "" & dtModDate  ' converts datetime format to string
  
On Error Goto 0  

End Function

' //////////////////////////////////////////////////////////////////////////////

Function extractPlainFile( strFilepath )

  Dim arr, i

On Error Resume Next

  arr = Split( strFilepath, "\" )
  extractPlainFile = arr( UBound( arr ) )
    
On Error Goto 0      

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getCacheValue( strEntry )

  Dim WshShell

On Error Resume Next

  getCacheValue = ""

  Set WshShell  = CreateObject("WScript.Shell")
  getCacheValue = WshShell.RegRead("HKLM\Software\ActiveXperts\Network Monitor\Server\VBScript_Cache\" & strEntry )

On Error Goto 0      
   
End Function

' //////////////////////////////////////////////////////////////////////////////

Sub setCacheValue( strEntry, strData )

  Dim WshShell
  Set WshShell = CreateObject("WScript.Shell")
  WshShell.RegWrite "HKLM\Software\ActiveXperts\Network Monitor\Server\VBScript_Cache\" & strEntry, strData, "REG_SZ"

End Sub

' //////////////////////////////////////////////////////////////////////////////

Function cntFiles( strFolder, bIncludeDirInCount )
  Dim objFolder, objSubFolders, objFso, o, n

On Error Resume Next

  cntFiles = -1

  Set objFso = Createobject( "Scripting.FileSystemObject" ) 
  Set objFolder = objFso.GetFolder(strFolder)
  If( Err.Number <> 0 ) Then
     Exit Function
  End If
  n = objFolder.files.count
  Set objSubFolders = objFolder.SubFolders 
  For Each o In objSubFolders
     n = n + cntFiles( o, bIncludeDirInCount )
     If( bIncludeDirInCount ) Then
       n = n + 1
     End If 
  Next

On Error Goto 0      
  
  Set objSubFolders = Nothing
  Set objFolder     = Nothing

  cntFiles = n
  
End Function