SilthDraeth

Platinum Member
Oct 28, 2003
2,635
0
71
I found this handy script.

Basically, I have a batch file that reads computers from a text file and runs the shutdown command on them.

The problem arrises though, if the machine is not powered on the script waits 30 seconds until it moves on to the next computer in the list.

So I found this handy VBS that pings a list of computers, and then outputs the results in a text file.

The problem with it however, is the list of computers to ping is hard coded into the script. I need it to ping the list of computers from a text file named "computers.txt" then I need the text file it creates to be named after the computer running the VBS script.

for example

server1 executes ping.vbs and then writes server1.txt to a dfs share \\domain\dfs\ping_results\server1.txt

Thank you for any help.

Here is the script I am working with.

strMachines = "computer1;computer2"
aMachines = split(strMachines, ";")

Set objRF = CreateObject("Scripting.FileSystemObject")
Set objPrintOut = objRF.CreateTextFile("Computer Name.txt")

For Each machine in aMachines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& machine & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objPrintOut.WriteLine()

else objPrintOut.WriteLine(""& machine &"")

End If
Next
Next

ObjPrintOut.Close
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Here is a sample script to read lines from a text file:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("c:\scripts\servers and services.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "Server name: " & arrServiceList(0)
For i = 1 to Ubound(arrServiceList)
Wscript.Echo "Service: " & arrServiceList(i)
Next
Loop

you can read the computer names from computers.txt using this script


 

SilthDraeth

Platinum Member
Oct 28, 2003
2,635
0
71
KB thanks. After a lot more digging around, this is what I found, and pieced together.

On Error Resume Next

Set objOU = GetObject("LDAP://OU=,OU=,OU=,DC=,DC=com") '
objOU.Filter = Array("Computer")

Set objRF = CreateObject("Scripting.FileSystemObject")
Set objPrintOut = objRF.CreateTextFile("results3.txt")

objPrintOut.Writeline Now()
For Each objComputer in objOU
strComputer = objComputer.CN



Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select * from Win32_PingStatus " & _
"Where Address = '" & strComputer & "'")
For Each objItem in colItems

If IsNull(objItem.StatusCode) or objItem.StatusCode<>0 Then
objPrintOut.WriteLine(""& strComputer &"") & " Host Unreachable"

else objPrintOut.WriteLine(""& strComputer &"") & " Ping Succesfull Shutdown Sent"

Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c shutdown -s -f -m \\" & strComputer & ""

Set objExecObject = objShell.Exec(strCommand)


End If
Next
Next