Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems executing command from Subroutine

Status
Not open for further replies.

sromine

Technical User
Apr 21, 2006
38
0
0
US
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.

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
 
Try removing your "On Error Resume Next" and see what your error is. Though it shouldn't be an issue, you don't need to create two instances of wscript.shell.

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top