[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use FileSystem object in vbscript to check C$ share on a server is accesible to verify server is alive...

Posted on 2007-11-19
5
Medium Priority
?
2,543 Views
Last Modified: 2010-08-05
Hi,

In question 22970139 an expert helped me develop the simple vbscript shown below that basically pings each server listed in an external text file, then returns a result for each server, formats all the results into a single string and then passes the string as the subject of a command line emailer (BMail). The script works a treat, but it was mentioned that I could also build in a FileSystem check to check the availability of the C$ share on each server being tested, as extra proof the server is alive.

So how in VBScript would I go about checking whether the C share on a remote server is 'accessible'?
SET objFSO = CreateObject("Scripting.FileSystemObject")
SET objInputfile = objFSO.OpenTextFile("serverhealthcheckserverlist.txt")
Set objShell = WScript.CreateObject( "WScript.Shell" )
resultsStr = ""
 
DO WHILE NOT objInputfile.AtEndOfStream
strComputer = objInputfile.readline
 
SET objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
IF InStr(strPingResults, "ttl") THEN
	resultsStr = resultsStr & strComputer & "_OK__"
ELSE
	resultsStr = resultsStr & strComputer & "_FAIL__"
END IF
LOOP
 
objShell.Run("bmail -s grj-exch01 -t ITalert@greatrail.com -f alert@greatrail.com -h -a ServerHealth -b " & resultsStr)
'msgbox(resultsStr)
 
Set objShell = Nothing
Set resultsStr = Nothing

Open in new window

0
Comment
Question by:grjitdept
5 Comments
 
LVL 9

Accepted Solution

by:
asawatzki earned 2000 total points
ID: 20317734
Try This:



SET objFSO = CreateObject("Scripting.FileSystemObject")
SET objInputfile = objFSO.OpenTextFile("serverhealthcheckserverlist.txt")
Set objShell = WScript.CreateObject( "WScript.Shell" )
resultsStr = ""
 
DO WHILE NOT objInputfile.AtEndOfStream
strComputer = objInputfile.readline
 
SET objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
IF InStr(strPingResults, "ttl") THEN
  On Error Resume Next
   err.number = 0
   set null = objFSO.GetFolder("\\" & strComputer & "\c$")
   set null = nothing
   If err.number = 0 then
	resultsStr = resultsStr & strComputer & "_OK__"
   Else
         resultsStr = resultsStr & strComputer & "_FAIL_Drive_Ping_OK_"
   End If
   err.number = 0
   on error goto 0
ELSE
	resultsStr = resultsStr & strComputer & "_FAIL__"
END IF
LOOP
 
objShell.Run("bmail -s grj-exch01 -t ITalert@greatrail.com -f alert@greatrail.com -h -a ServerHealth -b " & resultsStr)
'msgbox(resultsStr)
 
Set objShell = Nothing
Set resultsStr = Nothing
 

Open in new window

0
 
LVL 1

Author Comment

by:grjitdept
ID: 20318972
Hi,

Thanks for this - looks good - when I run it for active servers though it always returns afail. It seems to me that you check for the value of err.number on line 16, and it's value is previously set on Line 13, but I don't see how this value would ever change. Doesn't Line 14 want to change the value of err.number?
0
 
LVL 1

Author Comment

by:grjitdept
ID: 20319632
OK ignore that last comment - a little more testing revealed I needed a '\' after the c$ as it's checking for a folder - without the \ it returns an error number of 51, hence it isn't 0 and fails the test... I now understand how the error.number works as well.

Thanks for your help!
0
 

Expert Comment

by:mzatorski
ID: 24227474
Has anyone else gotten this to work?  I tried to get it to run but like grjitdept stated, it always fails.  I tried adding the \c$\  to the script but it still comes up as fail.  
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24255404

Following this from another post.

Null is a reserved word, it returns "illegal assignment" when used in the context above. Change the "null" variable name to something else and it should spring to life.

Chris
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question