MitchV85
asked on
Check server availability in VBScript
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
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"
ASKER
Thanks for the quick response sirbounty...I am going to try this now and will let you know the results.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Is there a way to have the script Echo some information when a computer in the text file is not reachable?
Alter this line:
If IsAlive (strComputer) Then OSCheck
to read as this block:
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
ASKER
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?
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?
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...
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...
Open in new window