Windows Script to monitor file size on remote servers



Hello all,

I have recently written a script (WSF) to monitor the file size on
remote computers. As written it monitors the SYSTEM registry hive
file. Please post comments or recommendations on improvements. It
includes several subs and functions that I find very useful. The
tricky part was to determine %systemroot% on remote systems (not
needed on most systems).

To run it you will need an input text file with the names, IP
addresses, or FQDNs of the servers you wish to connect to. To monitor
more than 25 servers you must increase the aServerName(25,2) array
dimensions.

Hope you find it useful.

- David Spink

------
<Job id="FileSizeMon">
<Runtime>
<named
name ="T"
helpstring ="The number of minutes the script will run"
type ="String"
required ="True"
/>
<named
name ="F"
helpstring ="The polling frequency in seconds"
type ="String"
required ="True"
/>
<named
name ="InFile"
helpstring ="Path and filename containing server names(Defualt: c:
\temp\Servers.txt)"
type ="string"
required ="False"
/>
<Description>
This script will monitor the size of a file accross multiple servers.
The input file is a text file with one server name per line.
The output is in CSV format. The output is in CSV format.

Output file located at: [Working_dir]\FileSizeLog_{DateStamp}.CSV
</Description>
</runtime>

<Script Language ="VBScript">
'/////////////////////////////////////////////////////////////////////////////////////////////////////////
'<!--
'Filename : FileSizeMon.vbs;
'Date : 03/03/09;
'LastUpdated : 03/04/2009
'Version : 1.0.1;
'Comment : Created for customer;
'Author : David Spink;
'Company : company;
'Contact : email address
'Comment : This script will monitor the size of a file accross
multiple servers.
' The input file is a text file with one server name per
line. The output is in CSV format.
' The output is in CSV format. Output file located at: C:
\temp\scripts\FileSizeLog_<DateStamp>.CSV
'-->
'///////////////////////////////////////////////////////////////////////////////////////////////////////////
OPTION EXPLICIT

'Declare variables
Dim g_oFSO, sOutFile, g_oLog, g_sServerList, g_oServerList,
oArgs,objWMIService,colFiles, objFile 'global objects
Dim sStream 'Global string to read input
file lines
Dim aServerName(25,2) 'array to hold server names
and file path targets
Dim i, n, x 'Loop iteration variables
Dim sPollFrequency, sRuntime,sRunMinutes, sStartTime, sFinishTime,
sPolls 'variables to control the file polling duration/frequency

ForceCscript 'ensure script runs using
the cscript interpreter
Set oArgs=Wscript.Arguments.Named
Set g_oFSO = CreateObject("Scripting.FileSystemObject")
Set g_oLog = g_oFSO.CreateTextFile(Datestamp("FileSizeLog","csv"))
g_oLog.Writeline "Date,Time,ServerName,FileName,FileSize"


'Check for command line arguments - if not present show the command
usage info
If oArgs.Exists("T") = True AND oArgs.Exists("F")=True Then
If oArgs.Exists("InFile") = True Then
g_sServerList = oArgs("InFile")
Else
g_sServerList = "c:\temp\Servers.txt"
End If
sRunMinutes = oArgs("T")
sPollFrequency = oArgs("F")
Else
Wscript.echo "Use the '/?' switch to show help"
Wscript.Quit
End if


'-------calcluate the number of polls and the total runtime
sPolls = (sRunMinutes * 60 ) /sPollFrequency
Wscript.echo "Logging file size info every " & sPollFrequency & "
seconds for the next " & sRunMinutes & " minutes."


'--------get server names and populate server name array
set g_oServerList = g_oFSO.OpenTextFile(g_sServerList)
' Open Servername input file and assign values to array
Do Until g_oServerList.AtEndOfStream
aServerName(i,0) = g_oServerList.Readline
aServerName(i,1) = BuildWMIPath(GetSysRoot(aServerName(i,0))) & "\
\System32\\Config\\System"
i = i + 1
Loop

'--------Loop through servers and wait for next poll
For x=0 to sPolls
' Loop every once per polling frequency
Wscript.Echo "Polling...." & sPolls-x & " polls remaining"
For n=0 to UBound(aServerName) 'Get filesize data for each server
in the array
If aServerName(n,0) <> "" Then
GetFileSize aServerName(n,0),aServerName(n,1)
End If
Next
Wscript.Sleep sPollFrequency * 1000
Next

Wscript.Quit
'//////////////////////////////////////////////////////////////////////////////////////



'-----------------------------------
BuildWMIPath-----------------------
'--------function to convert file paths for use in WMI
Queries---------
Function BuildWMIPath(sPath)
'WMIpath example strFile = "c:\\Windows\\System32\\Config"
sPath = Replace(sPath,"\","\\")
BuildWMIPath = sPath
End Function


'--------------------------------------
GetSysRoot------------------------
'----Function to find the value for %systemroot% on a remote
computer----
Function GetSysRoot(sRemoteComputer)
Dim strKeyPath, strValueName, oReg, strValue
Const HKEY_LOCAL_MACHINE = &H80000002

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
_
sRemoteComputer & "\root\default:StdRegProv")

strKeyPath = "software\microsoft\windows NT\currentversion"
strValueName = "systemroot"
oReg.GetStringValue
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
GetSysRoot = strValue
End Function


'--------------------------------------
GetFileSize-------------------------
Sub GetFileSize(strComputer,strFile)
Dim sFileSize, sFileName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where name = '" & strFile & "'")
For Each objFile in colFiles
sFileSize = objFile.FileSize
sFileName = objFile.Name
Next
g_oLog.Writeline Date & "," & Time& "," & strComputer & "," &
sFileName & "," & sFileSize
End Sub


'------------------------Function to created a date-time stamped
filename------------------------
Function Datestamp(sPrefix,sExtension)
Dim sMonth, sDay, sHour, sMinute, sName, sRegKey
If Len(Month(date)) = 2 Then sMonth = Month(date) Else sMonth = "0" &
Month(Date)
If Len(Day(Date)) = 2 Then sDay = Day(Date) Else sDay = "0" & Day
(Date)
If Len(Hour(Now)) = 2 Then sHour = Hour(Now) Else sHour = "0" & Hour
(Now)
If Len(Minute(Now)) = 2 Then sMinute = Minute(Now) Else sMinute = "0"
& Minute(Now)
datestamp = sPrefix & "_" & Year(date)& sMonth & sDay & "_" & sHour &
sMinute & "." & sExtension
End Function


'------------------------------
ForceCscript------------------------------------
Sub ForceCscript 'Function to force the cscript environment
Dim qt, arg, sCommand, sArgs
If Instr(1,WScript.FullName,"wscript.exe",vbTextCompare) > 0 then
'script running under WScript.exe ...Rebuild arguments (if any)
sArgs = ""
qt = chr(34)
If Wscript.Arguments.count > 0 Then
For each arg in Wscript.Arguments
sArgs = sArgs & " " & qt & arg & qt
next
End If
sCommand = "cscript.exe " & qt & wscript.scriptfullname & qt & " " &
sargs 'build a new command line with: cscript.exe <scriptname>
<arguments>
oShell.Run sCommand,7
wscript.quit(0)
End If
End sub
'------------------------------------------------------------------------------------

'Exit out of XML script tag and close file
</script>
</job>
.



Relevant Pages

  • Monitoring Registry File Size on Remote Servers
    ... I have recently written a script to monitor the file size on ... or FQDNs of the servers you wish to connect to. ... Dim oArgs,objWMIService,colFiles, objFile ...
    (microsoft.public.scripting.vbscript)
  • Re: I need your help.
    ... Ping monitor multiple servers. ... I need to create a script that I monitor the ping servers, ... But you can think a log file and leave that down popup, ...
    (microsoft.public.scripting.vbscript)
  • Re: I need your help.
    ... Ping monitor multiple servers. ... I need to create a script that I monitor the ping servers, ... But you can think a log file and leave that down popup, ...
    (microsoft.public.scripting.vbscript)
  • Re: I need your help.
    ... Ping monitor multiple servers. ... I need to create a script that I monitor the ping servers, ... But you can think a log file and leave that down popup, ...
    (microsoft.public.scripting.vbscript)
  • Re: Slightly [OT] Network Monitoring/Alerting tools
    ... Of course if your server is prone to running out of resources you should also be collecting that data via snmp with thresholds to notify you before you have an outage or at least graphing it so you'll understand the trends. ... We were able to use the monitor to look for ... Most of the common things you would want are already built in so you don't have to script them - or install scripts on all the clients. ...
    (Fedora)

Loading