File.vbs - File checking using ActiveXperts Network Monitor
ActiveXperts Network Monitor ships with a powerful set of pre-defined checks. Each individual check has a static number of configuration items. To monitor other items, or to combine monitoring items, you can make use of custom VBScript checks.
Most of the built-in checks have a VBScript equivalent, implemented as a Function in a VBScript (.vbs) file. Out-of-the-box, each VBScript function monitors the same items as the built-in check. Feel free to modify a function. The VBScript check can be customized by editing the VBScript function.
To add a new VBScript-based File check, do the following:
- On the 'Monitor menu', click 'New Monitoring Check (VBScript)'. The 'VBScript Check' dialog box appears;
- In the 'File selection box', select 'File.vbs';
- In the 'Function selection box', select one of the functions, for instance: 'CheckFileExistence';
- In the 'Function parameters group box' enter the required parameters. You can also load a working sample first by clicking on the 'Load a sample, click here' link.
To customize the above monitoring check, click on the 'Edit button' next to the 'File selection box'. Notepad will be launched. You can now make changes to the VBScript function(s).

File.vbs script source code
' /////////////////////////////////////////////////////////////////////////////// ' // ActiveXperts Network Monitor - VBScript based checks ' // (c) ActiveXperts Software B.V. ' // ' // For more information about ActiveXperts Network Monitor and VBScript, please ' // visit the online ActiveXperts Network Monitor VBScript Guidelines at: ' // https://www.activexperts.com/support/network-monitor/online/vbscript/ ' // ' /////////////////////////////////////////////////////////////////////////////// ' Option Explicit Const retvalUnknown = 1 Dim SYSDATA, SYSEXPLANATION ' Used by Network Monitor, don't change the names ' /////////////////////////////////////////////////////////////////////////////// ' // 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 = CheckFileExistence( "\\localhost\c$\windows\windowsupdate.log", "" ) ' WScript.Echo "Return value: [" & bResult & "]" ' WScript.Echo "SYSDATA: [" & SYSDATA & "]" ' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]" ' ///////////////////////////////////////////////////////////////////////////// Function CheckFileExistence( strPath, strCredentials ) ' Description: ' Check the existence of a file ' Parameters: ' 1) strPath - UNC formatted file path ' 2) strCredentials 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) ' Usage: ' CheckFileExistence( "<\\Server\Share\Path>", "<Empty String | Server>" ) ' Sample: ' CheckFileExistence( "\\localhost\c$\windows\windowsupdate.log", "" ) Dim objFileSys, objFileStream, nLine, strLine CheckFileExistence = retvalUnknown ' Unless indicated otherwise SYSDATA = "" ' Initally empty; will contain the size of the file (in KBs) SYSEXPLANATION = "" ' Set initial value nLine = 0 If( strCredentials <> "" ) Then If( Not login( strCredentials, SYSEXPLANATION ) ) Then Exit Function End If End If Set objFileSys = CreateObject("Scripting.FileSystemObject") If( objFileSys.FileExists( strPath ) <> True ) Then CheckFileExistence = False SYSEXPLANATION = "File [" & strPath & "] does not exist" Else CheckFileExistence = True SYSEXPLANATION = "File [" & strPath & "] exists" End If If( strCredentials <> "" ) Then logout( strCredentials ) End If End Function ' ///////////////////////////////////////////////////////////////////////////// Function CheckFileContent( strPath, strCredentials, strPattern ) ' Description: ' Check the contents of the file ' Parameters: ' 1) strPath - UNC formatted file path ' 2) strCredentials 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)' Usage: ' 3) strPattern - Search for this pattern in the file ' Usage: ' CheckFileContent( "<\\Server\Share\Path>", "<Empty String | Server>", "<pattern>" ) ' Sample: ' CheckFileContent( "\\localhost\c$\windows\windowsupdate.log", "", "required" ) Dim objFileSys, objFileStream, nLine, strLine CheckFileContent = retvalUnknown ' Unless indicated otherwise SYSDATA = "" ' Initally empty; will contain the size of the file (in KBs) SYSEXPLANATION = "" ' Set initial value nLine = 0 If( strCredentials <> "" ) Then If( Not login( strCredentials, SYSEXPLANATION ) ) Then Exit Function End If End If Set objFileSys = CreateObject("Scripting.FileSystemObject") If( objFileSys.FileExists( strPath ) <> True ) Then CheckFileContent = retvalUnknown SYSEXPLANATION = "File [" & strPath & "] does not exist" Exit Function End If set objFileStream = objFileSys.OpenTextFile( strPath, 1, False ) If( Err.Number <> 0 ) Then CheckFileContent = retvalUnknown SYSEXPLANATION = "Unable to open file [" & strPath & "]" Exit Function End If Do While objFileStream.AtEndOfStream <> True nLine = nLine + 1 strLine = objFileStream.ReadLine() If( InStr( LCase( strLine ), LCase( strPattern ) ) ) Then CheckFileContent = True SYSEXPLANATION = "Pattern matched in line: " & nLine Exit Function End If Loop CheckFileContent = False SYSEXPLANATION = "Pattern not matched (" & nLine & " lines parsed)" If( strCredentials <> "" ) Then logout( strCredentials ) End If objFileStream.Close() End Function ' ///////////////////////////////////////////////////////////////////////////// Function CheckFileContentLastLine( strPath, strCredentials, strPattern ) ' Description: ' Check last line of a file for a pattern ' Parameters: ' 1) strPath - UNC formatted file path ' 2) strCredentials 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)' Usage: ' 3) strPattern - Search for this pattern in the last line of the file ' Usage: ' CheckFileContentLastLine( "<\\Server\Share\Path>", "<Empty String | Server>", "<pattern>" ) ' Sample: ' CheckFileContentLastLine( "\\localhost\c$\windows\windowsupdate.log", "", "client" ) Dim objFileSys, objFileStream, nLine, strLastLine CheckFileContentLastLine = retvalUnknown ' Unless indicated otherwise SYSDATA = "" ' Initally empty; will contain the size of the file (in KBs) SYSEXPLANATION = "" ' Set initial value nLine = 0 strLastLine = "" If( strCredentials <> "" ) Then If( Not login( strCredentials, SYSEXPLANATION ) ) Then Exit Function End If End If Set objFileSys = CreateObject("Scripting.FileSystemObject") If( objFileSys.FileExists( strPath ) <> True ) Then CheckFileContentLastLine = retvalUnknown SYSEXPLANATION = "File [" & strPath & "] does not exist" Else set objFileStream = objFileSys.OpenTextFile( strPath, 1, False ) If( Err.Number <> 0 ) Then CheckFileContentLastLine = retvalUnknown SYSEXPLANATION = "Unable to open file [" & strPath & "]" Else Do While objFileStream.AtEndOfStream <> True nLine = nLine + 1 strLastLine = objFileStream.ReadLine() Loop If( InStr( LCase( strLastLine ), LCase( strPattern ) ) ) Then CheckFileContentLastLine = True SYSEXPLANATION = "Pattern matched in line: " & nLine Else CheckFileContentLastLine = False SYSEXPLANATION = strLastLine & "::" & "Pattern not matched in line " & nLine End If End If End If If( strCredentials <> "" ) Then logout( strCredentials ) End If objFileStream.Close() End Function ' ///////////////////////////////////////////////////////////////////////////// Function CheckFileSize( strPath, strCredentials, strLimitKB ) ' Description: ' Check the size of a file on a (remote) computer. Use network monitor service credentials to access the (remote) computer ' Parameters: ' 1) strPath - UNC formatted file path ' 2) strCredentials 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)' Usage: ' 3) strLimitKB As String - Maximum allowed file size (KB) ' Usage: ' CheckFileSize( "<\\Server\Share\Path>", "<Empty String | Server>", Max_KB ) ' Sample: ' CheckFileSize( "\\localhost\c$\program files\internet explorer\iexplore.exe", "", 900 ) Dim nSizeKB, nDiffKB CheckFileSize = retvalUnknown ' Unless indicated otherwise SYSDATA = "" ' Initally empty; will contain the size of the file (in KBs) SYSEXPLANATION = "" ' Set initial value If( strCredentials <> "" ) Then If( Not login( strCredentials, SYSEXPLANATION ) ) Then Exit Function End If End If nSizeKB = getFileSizeKB( strPath ) If( nSizeKB < 0 ) Then SYSEXPLANATION = "Unable to retrieve file size. Make sure that the file exists and that Network Monitor can access it." CheckFileSize = retvalUnknown Else nDiffKB = strLimitKB - nSizeKB SYSDATA = nSizeKB SYSEXPLANATION = "File size=[" & nSizeKB & " KB], maximum allowed size=[" & strLimitKB & " KB]" If( nDiffKB >= 0 ) Then CheckFileSize = True Else CheckFileSize = False End If End If If( strCredentials <> "" ) Then logout( strCredentials ) End If End Function ' ///////////////////////////////////////////////////////////////////////////// Function CheckFileChange( strPath, strCredentials ) ' Description: ' Check if a file has changed on a computer. Use network monitor service credentials to access the (remote) computer ' Parameters: ' 1) strPath - UNC formatted file path ' 2) strCredentials 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)' Usage: ' Usage: ' CheckFileChange( "<\\Server\Share\Path>", "<Empty String | Server>" ) ' Sample: ' CheckFileChange( "\\localhost\c$\windows\windowsupdate.log", "" ) Dim strPrevModDate, strModDate CheckFileChange = retvalUnknown ' Unless indicated otherwise If( strCredentials <> "" ) Then If( Not login( strCredentials, SYSEXPLANATION ) ) Then Exit Function End If End If strModDate = getFileModDateString( strPath ) If( strModDate = "" ) Then CheckFileChange = retvalUnknown SYSDATA = "" SYSEXPLANATION = "File does not exist or cannot be accessed" Exit Function End If strPrevModDate = getCacheValue( extractPlainFile( strPath ) ) setCacheValue extractPlainFile( strPath ), strModDate If( strPrevModDate = "" ) Then CheckFileChange = retvalUnknown SYSDATA = "" SYSEXPLANATION = "File was not monitored before" Exit Function End If If( strPrevModDate <> strModDate ) Then CheckFileChange = False SYSDATA = strModDate SYSEXPLANATION = "File has changed since last check" Else CheckFileChange = True SYSDATA = strModDate SYSEXPLANATION = "File has not changed since last check" End If If( strCredentials <> "" ) Then logout( strCredentials ) End If End Function ' ///////////////////////////////////////////////////////////////////////////// Function CountFiles( strPath, strCredentials, bIncludeDirsInCount, nMax ) ' Description: ' Count the number of files in a directory and all subsirectories ' Parameters: ' 1) strPath - UNC formatted file path ' 2) strCredentials 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)' Usage: ' 3) bIncludeDirsInCount - Also count a directory as a file ' 4) nMax - Maximum number of files (and directories) allowed ' Usage: ' CountFiles( "<\\Server\Share\Path>", "<Empty String | Server>", [ True | False ], Max_Files ) ' Sample: ' CountFiles( "\\localhost\c$\windows\system32", "", True, 10000 ) Dim n CountFiles = retvalUnknown ' Unless indicated otherwise SYSDATA = "" SYSEXPLANATION = "Unable to count files" If( strCredentials <> "" ) Then If( Not login( strCredentials, 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 CountFiles = False Else CountFiles = True End If End If If( strCredentials <> "" ) Then logout( strCredentials ) End If End Function ' ////////////////////////////////////////////////////////////////////////////// ' // ' // Private Functions ' // NOTE: Private functions are used by the above functions, and will not ' // be called directly by the ActiveXperts Network Monitor Service. ' // Private function names start with a lower case character and will ' // not be listed in the Network Monitor's function browser. ' // ' ////////////////////////////////////////////////////////////////////////////// Function getWMIObject( strComputer, strCredentials, BYREF objWMIService, BYREF strSysExplanation ) On Error Resume Next Dim objNMServerCredentials, objSWbemLocator, colItems Dim strUsername, strPassword getWMIObject = False Set objWMIService = Nothing If( strCredentials = "" ) Then ' Connect to remote host on same domain using same security context Set objWMIService = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &"\root\cimv2" ) Else Set objNMServerCredentials = CreateObject( "ActiveXperts.NMServerCredentials" ) strUsername = objNMServerCredentials.GetLogin( strCredentials ) strPassword = objNMServerCredentials.GetPassword( strCredentials ) If( strUsername = "" ) Then getWMIObject = False strSysExplanation = "No alternate credentials defined for [" & strCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials" Exit Function End If ' Connect to remote host using different security context and/or different domain Set objSWbemLocator = CreateObject( "WbemScripting.SWbemLocator" ) Set objWMIService = objSWbemLocator.ConnectServer( strComputer, "root\cimv2", strUsername, strPassword ) If( Err.Number <> 0 ) Then objWMIService = Nothing getWMIObject = False strSysExplanation = "Unable to access [" & strComputer & "]. Possible reasons: WMI not running on the remote server, Windows firewall is blocking WMI calls, insufficient rights, or remote server down" Exit Function End If objWMIService.Security_.ImpersonationLevel = 3 End If If( Err.Number <> 0 ) Then objWMIService = Nothing getWMIObject = False strSysExplanation = "Unable to access '" & strComputer & "'. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down" Exit Function End If getWMIObject = True End Function ' ////////////////////////////////////////////////////////////////////////////// Function login( strCredentials, strSysExplanation ) ' Login function for non-WMI based checks. Dim objCredentials, objRemoteServer Dim strUsername, strPassword login = False Set objCredentials = CreateObject( "ActiveXperts.NMServerCredentials" ) Set objRemoteServer = CreateObject( "ActiveXperts.RemoteServer" ) strUsername = objCredentials.GetLogin( strCredentials ) strPassword = objCredentials.GetPassword( strCredentials ) If( strUsername = "" ) Then login = False strSysExplanation = "No alternate credentials defined for [" & strCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials" Exit Function End If objRemoteServer.Connect strCredentials, strUsername, strPassword If( objRemoteServer.LastError <> 0 ) Then login = False strSysExplanation = "Login failed" Exit Function End If login = True End Function ' ////////////////////////////////////////////////////////////////////////////// Function logout( strCredentials ) ' Logout function for non-WMI based checks. Dim objRemoteServer logout = False Set objRemoteServer = CreateObject( "ActiveXperts.RemoteServer" ) objRemoteServer.Disconnect strCredentials If( objRemoteServer.LastError <> 0 ) Then logout = False Exit Function End If logout = 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 On Error Resume Next Dim objFSO, objFile getFileSizeKB = -1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile( strPath ) If( Err.Number <> 0 ) Then getFileSizeKB = -1 Exit Function End If getFileSizeKB = FormatNumber( objFile.Size / 1024, 0, 0, 0, 0 ) 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 End Function ' ////////////////////////////////////////////////////////////////////////////// Function extractPlainFile( strFilepath ) On Error Resume Next Dim arr, i arr = Split( strFilepath, "\" ) extractPlainFile = arr( UBound( arr ) ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function getCacheValue( strEntry ) On Error Resume Next Dim WshShell getCacheValue = "" Set WshShell = CreateObject("WScript.Shell") getCacheValue = WshShell.RegRead("HKLM\Software\ActiveXperts\Network Monitor\Server\VBScript_Cache\" & strEntry ) 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 Set objSubFolders = Nothing Set objFolder = Nothing cntFiles = n End Function