file.vbs - vbscript script by ActiveXperts Software
file.vbs checks file existence, file content, last line of a file for a pattern, file size, file change.
Use file.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select file.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.
file.vbs script code
' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor - VBScript based checks
' // For more information about ActiveXperts Network Monitor and VBScript, visit
' // http://www.activexperts.com/support/network-monitor/online/vbscript/
' ///////////////////////////////////////////////////////////////////////////////
Option Explicit
' Declaration of global variables
Dim SYSDATA, SYSEXPLANATION ' SYSDATA is displayed in the 'Data' column in the Manager; SYSEXPLANATION in the 'LastResponse' column
' Constants - return values
Const retvalUnknown = 1 ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain)
' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following lines:
' Dim bResult
' bResult = CheckFileExistence( "localhost", "\\localhost\c$\windows\windowsupdate.log", "" )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"
' /////////////////////////////////////////////////////////////////////////////
Function CheckFileExistence( strHost, strAltCredentials, strPath )
' Description:
' Check the existence of a file
' Parameters:
' 1) strHost As String - Hostname or IP address of the computer you want to monitor
' 2) strPath - UNC formatted file path
' 3) 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)
' Usage:
' CheckFileExistence( "<Server>", "<Empty String | Server>", "<\\Server\Share\Path>" )
' Sample:
' CheckFileExistence( "localhost", "", "\\localhost\c$\windows\windowsupdate.log" )
Dim strAltLogin, strAltPassword
Dim objFileSys, objFileStream, nLine, strLine
CheckFileExistence = 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 = ""
nLine = 0
' 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
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 alternate login is used, logoff now
If( strAltLogin <> "" ) Then
netLogoff( strHost )
End If
End Function
' /////////////////////////////////////////////////////////////////////////////
Function CheckFileContent( strHost, strAltCredentials, strPath, strPattern )
' Description:
' Check the contents of the file
' Parameters:
' 1) strHost As String - Hostname or IP address of the computer you want to monitor
' 2) strPath - UNC formatted file path
' 3) 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)' Usage:
' 4) strPattern - Search for this pattern in the file
' Usage:
' CheckFileContent( "<Server>", "<Empty String | Server>", "<\\Server\Share\Path>", "<pattern>" )
' Sample:
' CheckFileContent( "localhost", "", "\\localhost\c$\windows\windowsupdate.log", "required" )
Dim strAltLogin, strAltPassword
Dim objFileSys, objFileStream, nLine, strLine
CheckFileContent = 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 = ""
nLine = 0
' 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
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 [" & strPattern & "] matched in line #" & nLine
Exit Function
End If
Loop
CheckFileContent = False
SYSEXPLANATION = "Pattern [" & strPattern & "] not matched; #lines parsed:[" & nLine & "]"
' If alternate login is used, logoff now
If( strAltLogin <> "" ) Then
netLogoff( strHost )
End If
objFileStream.Close()
End Function
' /////////////////////////////////////////////////////////////////////////////
Function CheckFileContentLastLine( strHost, strAltCredentials, strPath, strPattern )
' Description:
' Check last line of a file for a pattern
' Parameters:
' 1) strHost As String - Hostname or IP address of the computer you want to monitor
' 2) strPath - UNC formatted file path
' 3) 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)' Usage:
' 4) strPattern - Search for this pattern in the last line of the file
' Usage:
' CheckFileContentLastLine( "<Server>", "<Empty String | Server>", "<\\Server\Share\Path>", "<pattern>" )
' Sample:
' CheckFileContentLastLine( "localhost", "", "\\localhost\c$\windows\windowsupdate.log", "client" )
Dim strAltLogin, strAltPassword
Dim objFileSys, objFileStream, nLine, strLastLine
CheckFileContentLastLine = 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 = ""
nLine = 0
strLastLine = ""
' 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
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 [" & strPattern & "] matched in last line #" & nLine
Else
CheckFileContentLastLine = False
SYSEXPLANATION = "Pattern [" & strPattern & "] not matched in last line #" & nLine
End If
End If
End If
' If alternate login is used, logoff now
If( strAltLogin <> "" ) Then
netLogoff( strHost )
End If
objFileStream.Close()
End Function
' /////////////////////////////////////////////////////////////////////////////
Function CheckFileSize( strHost, strAltCredentials, strPath, strLimitKB )
' Description:
' Check the size of a file 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) strPath - UNC formatted file path
' 3) 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)' Usage:
' 4) strLimitKB As String - Maximum allowed file size (KB)
' Usage:
' CheckFileSize( "<Server>", "<Empty String | Server>", "<\\Server\Share\Path>", <Max_KB> )
' Sample:
' CheckFileSize( "localhost", "", "\\localhost\c$\program files\internet explorer\iexplore.exe", 900 )
Dim strAltLogin, strAltPassword
Dim nSizeKB, nDiffKB
CheckFileSize = 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
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 alternate login is used, logoff now
If( strAltLogin <> "" ) Then
netLogoff( strHost )
End If
End Function
' /////////////////////////////////////////////////////////////////////////////
Function CheckFileChange( strHost, strAltCredentials, strPath )
' Description:
' Check if a file has changed on a 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)' Usage:
' 3) strPath As String - UNC formatted file path
' Usage:
' CheckFileChange( "<Server>", "<Empty String | Server>", "<\\Server\Share\Path>" )
' Sample:
' CheckFileChange( "localhost", "", "\\localhost\c$\windows\windowsupdate.log" )
Dim strAltLogin, strAltPassword
Dim strPrevModDate, strModDate
CheckFileChange = 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
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 = True
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 alternate login is used, logoff now
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 = "Unable to login on [" & strHost & "] using login [" & strAltLogin & "]"
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
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 = CInt( objFile.Size / 1024 )
On Error Goto 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
On Error Goto 0
End Function
' //////////////////////////////////////////////////////////////////////////////
Function extractPlainFile( strFilepath )
On Error Resume Next
Dim arr, i
arr = Split( strFilepath, "\" )
extractPlainFile = arr( UBound( arr ) )
On Error Goto 0
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 )
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
Set objSubFolders = Nothing
Set objFolder = Nothing
cntFiles = n
On Error Goto 0
End Function
