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

x
?
Solved

Check server availability in VBScript

Posted on 2009-04-16
8
Medium Priority
?
736 Views
Last Modified: 2012-05-06
Hello all, I am having a pretty difficult time getting a script to work the way that I would like. My goal is to have the script first open a text file containing the names of all servers the script will run on. For each server in the text file, the script should first ping to see if the server is available. If it is available, it will then check the OS version of the server, copy files to the server based on its respective OS, and then move on to the next server listed in the text file.

The part that is giving me the most difficulty is the part where I have the script ping the server to check availability. I have left this part of the script empty for this posting as I have not been able to get anything to work as of yet; and because I am open to any suggestion.

If you need any more information on this script just let me know.

Thanks in advance for any help!

-Mitch
On Error Resume Next
 
Const ForReading = 1
Const OverwriteExisting = TRUE
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objTextFile = objFSO.OpenTextFile("C:\ServerList.txt", ForReading)
 
strComputer = objTextFile.Readline
 
Do Until objTextFile.AtEndOfStream
 
'*******************************************
'*Check to see if server is up and running.*
'*******************************************
 
 
 
'*******************************************
'*Check the OS version on the server.*******
'*******************************************
Sub OSCheck
 
	Set ColOSes = objWMIService.ExecQuery("Select Caption from Win32_OperatingSystem")
		For Each objOS in ColOSes
			strOSVersion = objOS.Caption
		Next
 
		If Instr(strOSVersion, "2000") Then
			WScript.Echo strComputer & " is running Windows 2000 Server."
			Call Server2000FileCopy
		Else
			WScript.Echo strComputer & " is running Windows Server 2003."
			Call Server2003FileCopy
		End If
 
End Sub
 
'*******************************************
'*Copy files to a 2000 Server.**************
'*******************************************
Sub Server2000FileCopy
 
	If objFSO.FileExists("\\" & strcomputer & "\C$\WINNT\rsc\exports") Then
		WScript.Echo "Now copying files to the server."
		Set objExportsFile = objFSO.GetFile("\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports")
		objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports" , "\\" & strcomputer & "\C$\WINNT\rsc\"
		objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users" , "\\" & strcomputer & "\C$\WINNT\rsc\"
		objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users.local" , "\\" & strcomputer & "\C$\WINNT\rsc\"
	End If
 
End Sub
 
'*******************************************
'*Copy files to a 2003 Server.**************
'*******************************************
Sub Server2003FileCopy
 
	If objFSO.FileExists("\\" & strcomputer & "\C$\Windows\rsc\exports") Then
		WScript.Echo "Now copying files to the server."
		Set objExportsFile = objFSO.GetFile("\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports")
		objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports" , "\\" & strcomputer & "\C$\Windows\rsc\"
		objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users" , "\\" & strcomputer & "\C$\Windows\rsc\"
		objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users.local" , "\\" & strcomputer & "\C$\Windows\rsc\"
	End If
 
End Sub
 
Loop
 
objTextFile.Close
 
WScript.Echo "Done"

Open in new window

0
Comment
Question by:MitchV85
  • 4
  • 4
8 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24157857
Try this...
On Error Resume Next
 
Const ForReading = 1
Const OverwriteExisting = TRUE
 
Dim objWMIService
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objTextFile = objFSO.OpenTextFile("C:\ServerList.txt", ForReading)
 
strComputer = objTextFile.Readline
 
Do Until objTextFile.AtEndOfStream
 
'*******************************************
'*Check to see if server is up and running.*
'*******************************************
  strDevice = objTextFile.ReadLine
  If IsAlive (strDevice) Then OSCheck 
 
Loop
 
objTextFile.Close
 
WScript.Echo "Done"
 
'*******************************************
'*Check the OS version on the server.*******
'*******************************************
Sub OSCheck
        Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strDevice & "\root\cimv2")
        Set ColOSes = objWMIService.ExecQuery("Select Caption from Win32_OperatingSystem")
                For Each objOS in ColOSes
                        strOSVersion = objOS.Caption
                Next
 
                If Instr(strOSVersion, "2000") Then
                        WScript.Echo strComputer & " is running Windows 2000 Server."
                        Call Server2000FileCopy
                Else
                        WScript.Echo strComputer & " is running Windows Server 2003."
                        Call Server2003FileCopy
                End If
 
End Sub
 
'*******************************************
'*Copy files to a 2000 Server.**************
'*******************************************
Sub Server2000FileCopy
 
        If objFSO.FileExists("\\" & strcomputer & "\C$\WINNT\rsc\exports") Then
                WScript.Echo "Now copying files to the server."
                Set objExportsFile = objFSO.GetFile("\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports")
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports" , "\\" & strcomputer & "\C$\WINNT\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users" , "\\" & strcomputer & "\C$\WINNT\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users.local" , "\\" & strcomputer & "\C$\WINNT\rsc\"
        End If
 
End Sub
 
'*******************************************
'*Copy files to a 2003 Server.**************
'*******************************************
Sub Server2003FileCopy
 
        If objFSO.FileExists("\\" & strcomputer & "\C$\Windows\rsc\exports") Then
                WScript.Echo "Now copying files to the server."
                Set objExportsFile = objFSO.GetFile("\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports")
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports" , "\\" & strcomputer & "\C$\Windows\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users" , "\\" & strcomputer & "\C$\Windows\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users.local" , "\\" & strcomputer & "\C$\Windows\rsc\"
        End If
 
End Sub
 
 
Function IsAlive (host)
  Dim colResult : 
Set colResult = GetObject("winmgmts:{impersonationLevel=impersonate}//./root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & "WHERE Address = '" & host & "'")
 
  For Each Response In colResult
      If Response.StatusCode = 0 Then
        IsAlive = True
      Else
        IsAlive = False
      End If
  Next
 
End Function

Open in new window

0
 
LVL 5

Author Comment

by:MitchV85
ID: 24157879
Thanks for the quick response sirbounty...I am going to try this now and will let you know the results.
0
 
LVL 5

Author Comment

by:MitchV85
ID: 24158404
sirbounty, I tried the script that you had provided me and did not have success. It reported that it was "Done." as soon as I had launched it.

I also tried modifying it quite a bit, hence the delayed response, without success. Quick question though, why do you define strDevice as objTextFile.Readline when strComputer is already set to this definition?

Thanks again for the quick response you provided. Definitely a step in the right direction.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 67

Accepted Solution

by:
sirbounty earned 1420 total points
ID: 24158521
I didn't see it since it was outside the loop...try this version - if it continues to fail - remove the first line (on error) and try again...
On Error Resume Next
 
Const ForReading = 1
Const OverwriteExisting = TRUE
 
Dim objWMIService
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objTextFile = objFSO.OpenTextFile("C:\ServerList.txt", ForReading)
  
Do Until objTextFile.AtEndOfStream
 
'*******************************************
'*Check to see if server is up and running.*
'*******************************************
  strComputer = objTextFile.Readline
  If IsAlive (strComputer) Then OSCheck 
 
Loop
 
objTextFile.Close
 
WScript.Echo "Done"
 
'*******************************************
'*Check the OS version on the server.*******
'*******************************************
Sub OSCheck
        Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set ColOSes = objWMIService.ExecQuery("Select Caption from Win32_OperatingSystem")
                For Each objOS in ColOSes
                        strOSVersion = objOS.Caption
                Next
 
                If Instr(strOSVersion, "2000") Then
                        WScript.Echo strComputer & " is running Windows 2000 Server."
                        Call Server2000FileCopy
                Else
                        WScript.Echo strComputer & " is running Windows Server 2003."
                        Call Server2003FileCopy
                End If
 
End Sub
 
'*******************************************
'*Copy files to a 2000 Server.**************
'*******************************************
Sub Server2000FileCopy
 
        If objFSO.FileExists("\\" & strcomputer & "\C$\WINNT\rsc\exports") Then
                WScript.Echo "Now copying files to the server."
                Set objExportsFile = objFSO.GetFile("\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports")
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports" , "\\" & strcomputer & "\C$\WINNT\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users" , "\\" & strcomputer & "\C$\WINNT\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users.local" , "\\" & strcomputer & "\C$\WINNT\rsc\"
        End If
 
End Sub
 
'*******************************************
'*Copy files to a 2003 Server.**************
'*******************************************
Sub Server2003FileCopy
 
        If objFSO.FileExists("\\" & strcomputer & "\C$\Windows\rsc\exports") Then
                WScript.Echo "Now copying files to the server."
                Set objExportsFile = objFSO.GetFile("\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports")
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\exports" , "\\" & strcomputer & "\C$\Windows\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users" , "\\" & strcomputer & "\C$\Windows\rsc\"
                objFSO.CopyFile "\\csc06radqvqa02s\softdoc$\Bladelogic\ACL\Windows\users.local" , "\\" & strcomputer & "\C$\Windows\rsc\"
        End If
 
End Sub
 
 
Function IsAlive (host)
 Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}\\.\root\cimv2")
 Set colResult = objWMIService.ExecQuery ("Select * from Win32_PingStatus " & "Where Address = '" & host & "'")
 
  For Each Response In colResult
      wscript.echo Response.StatusCode
      If Response.StatusCode = 0 Then
        IsAlive = True
      Else
        IsAlive = False
      End If
  Next
 
End Function

Open in new window

0
 
LVL 5

Author Comment

by:MitchV85
ID: 24158618
Great, That worked! When the script runs on a server in the text file that responds to a ping it copies the files to their respective location based on the OS.

Is there a way to have the script Echo some information when a computer in the text file is not reachable?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24158643
Alter this line:
If IsAlive (strComputer) Then OSCheck
to read as this block:



If IsAlive (strComputer) Then 
  OSCheck 
Else
  wscript.echo strComputer & " could not be contacted."
End If

Open in new window

0
 
LVL 5

Author Comment

by:MitchV85
ID: 24160809
That did it. Thanks for the help sirbounty!

A few quick questions though...why is it that you defined the objWMIService variable in the OSCheck Sub Procedure instead of leaving it at the beginning of the script? Also, why did you redefine the objWMIService variable in the IsAlive function?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24161026
I actually declared it at the beginning, thinking I could just use/reuse the same reference.
However, in your OSCheck routine, you're querying wmi against a target device (strComputer) whereas the ping query is initiated against the local machine (.).  You could use the same variable and just redefine it the way it's declared, but I simply neglected to do so in the code above...

Ordinarily, were I to write this from scratch, I think I'd pass the value of strComputer 'to' OSCheck and let it deal with it that way, rather than relying on a global variable, but that's just me...

In the two wmi references, you could pass the local computer in, if you wanted, but in wmi, the period, will refer to the local machine anyway...

0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

867 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