I am transitioning from DOS to vbscript and ran into a problem that has got me stumped. The issue is that the command does not execute in the subroutine Turnoff, but yet if I ran similiar script (See TurnOff.vbs) it runs just fine. My hunch is that it does not like the multiple Set objShell = CreateObject("WScript.Shell") and set WshShell = WScript.CreateObject("WScript.Shell") in the same script. Could somebody take a look at it. I have verified that it does pass Computername just fine, I have outputted strCommand and it looks fine, it just wont execute the command within the subroutine.
Here is the problem script, below this is the script that runs fine.
This Script runs fine, very similiar:
Here is the problem script, below this is the script that runs fine.
Code:
INPUT_FILE_NAME = "C:\Computers.txt"
Const FOR_READING = 1
Dim WshShell, strCommand, objFSO, systems
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
Set objShell = CreateObject("WScript.Shell")
On Error Resume Next
For Each strComputer In arrComputers
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
WScript.Echo strTarget & " responded to ping."
turnoff(strComputer)
Else
WScript.Echo strTarget & " did not respond to ping."
End If
Next
sub turnoff(Computername)
set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "c:\RunProgram.vbs " & chr(34) & "shutdown -f" & chr(34) & " /username:local\admin /password:ytgyur /computer:" & Computername
WshShell.Run strCommand, 0
msgbox strCommand
end sub
This Script runs fine, very similiar:
Code:
INPUT_FILE_NAME = "\\198.196.23.39\tech\PublicMaintScript\LoopThroughSystems\computers.txt"
Const FOR_READING = 1
Dim WshShell, strCommand, objFSO, systems
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
strCommand = "\\198.196.23.39\tech\PublicMaintScript\LoopThroughSystems\RunProgram.vbs " & chr(34) & "shutdown -f" & chr(34) & " /username:local\admin /password:yutgr67 /computer:" & strComputer
WshShell.Run strCommand, 0
Next