Link to home
Start Free TrialLog in
Avatar of Bianchi928
Bianchi928

asked on

Running VBScript Remotely

Can someone tell me what can be wrong with this little vbscript. I've attached teh error message. Or maybe suggest a different way to run a VBScript remotely. I can ping the IP Address successfully.

strComputer = "10.24.???.???"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
Error = objWMIService.Create("c:\scripts\z.vbs", null, null, intProcessID)
If Error = 0 Then
    Wscript.Echo "z.vbs was started with a process ID of " & intProcessID
Else
    Wscript.Echo "z.vbs could not be started due to error " & Error
End If



error.bmp
Avatar of IceCode
IceCode
Flag of United States of America image

Unfortunately ping does not equal access to the WMI namespace.  Are you on the same network as the machine you want to run this against?
Avatar of Bianchi928
Bianchi928

ASKER

No we aren't on the same network, but i have admin rights to all networks
I think this means you can't do what you need to:

"For security reasons the Win32_Process.Create method cannot be used to start an interactive process remotely."

http://msdn.microsoft.com/en-us/library/aa389388(v=vs.85).aspx
Any suggestion of different methods that can be used to achieve this task ?
I've tried using psexec and I still have a problem.

eg

psexec \\mycomputer c:\scripts\z.vbs

Error is : PsExec could not start c:\scripts\z.vbs on myipaddress
The system cannot file the file specified

Very strange because the file is there

Cheers


Avatar of RobSampson
Hi,

Is C:\Scripts\z.vbs on the remote machine, or on your machine?

If it's on the remote machine, it should work, but it's probably easier to put the script on a file share, and use the following:

psexec -accepteula -u domain\admin -p password \\REMOTEPC wscript.exe \\server\share\z.vbs

Regards,

Rob.
Are you just trying to run z.vbs on the remote machine but using another script to start it?
Hi Rob

You got me on the right track as usual...The following code is working fine and I've included you example in there. How can I modify it to write to log of the ones that failed ?

Thanks
Cheers


set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("Wscript.Shell")
set objFile = objFSO.OpenTextFile("C:\documents and settings\cis0794\desktop\inputlist5.txt")

Do while not objFile.AtEndOfStream
      currentpc = objFile.Readline
      objShell.Run "d:\psexec \\" & currentpc & " -i -d -u retail\username -p password wscript.exe \\" & currentpc & "\c$\scripts\z.vbs"
Loop
Hi, try this.  We can catch the return value from PSExec and report on that.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objFile = objFSO.OpenTextFile("C:\documents and settings\cis0794\desktop\inputlist5.txt")
Set objLog = objFSO.CreateTextFile("C:\Documents and settings\cis794\desktop\runscriptlog.txt")
Do while not objFile.AtEndOfStream
	currentpc = objFile.Readline
	If Ping(currentpc) = True Then
		intReturn = objShell.Run("d:\psexec \\" & currentpc & " -i -d -u retail\username -p password wscript.exe \\" & currentpc & "\c$\scripts\z.vbs", 0, True)
		If intReturn = 0 Then
			objLog.WriteLine "Script started successfully on " & currentpc
		Else
			objLog.WriteLine "Script start failed on " & currentpc & " with return code " & intReturn
		End If
	Else
		objLog.WriteLine currentpc & " is offline"
	End If
Loop
objLog.Close
objFile.Close

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

I fixed a few little mistakes (strcomputer with currentpc). I ran it and it's hung


Any idea ?
Cheers

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objFile = objFSO.OpenTextFile("C:\documents and settings\cis0794\desktop\inputlist5.txt")
Set objLog = objFSO.CreateTextFile("C:\Documents and settings\cis0794\desktop\runscriptlog.txt")
Do while not objFile.AtEndOfStream
      currentpc = objFile.Readline
      If Ping(currentpc) = True Then
            intReturn = objShell.Run("d:\psexec \\" & currentpc & " -i -d -u retail\cis0794 -p cis0794 wscript.exe \\" & currentpc & "\c$\scripts\z.vbs", 0, True)
            If intReturn = 0 Then
                  objLog.WriteLine "Script started successfully on " & currentpc
            Else
                  objLog.WriteLine "Script start failed on " & currentpc & " with return code " & intReturn
            End If
      Else
            objLog.WriteLine currentpc & " is offline"
      End If
Loop
objLog.Close
objFile.Close

Function Ping(currentpc)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & currentpc, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

intReturn = objShell.Run("d:\psexec \\" & currentpc & " -i -d -u retail\cis0794 -p cis0794 wscript.exe b]\\" & currentpc & "\c$\scripts\z.vbs", 0, True)

strScriptPath = "\\server\share\scripts\z.vbs"

Isn't that the same as

\\" & currentpc & "\c$\scripts\z.vbs"  in the top line
Not really.

If you tell a computer to run \\<remotepc>\c$\scripts\z.vbs then C:\Scripts\z.vbs must exist *on that remote machine*.  Whereas, if you tell a computer to run \\fileserver\share\scripts\z.vbs then all computers can have access to the same script at that share.

Rob.
It looks that it's hung on the Ping. When I run this little program, I get a "0" and "Ok". But, if i modify your script to reflect that , it hangs.

Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolcode = objShell.Run("Ping u101c8 " , 0, True)
        wscript.echo boolcode
        If boolCode = 0 Then
                wscript.echo "Ok"
            Ping = True
      Else
                wscript.echo "Failed"
            Ping = False
      End If
That's very strange. I've never had that problem....maybe it's the PSExec that's actually hanging....

Change these lines:

 
If Ping(currentpc) = True Then
		intReturn = objShell.Run("d:\psexec -i -u retail\username -p password \\" & currentpc & " wscript.exe " & strScriptPath, 0, True)
		If intReturn = 0 Then

Open in new window


to this:

 
If Ping(currentpc) = True Then
		strCommand = "d:\psexec -accepteula -i -u retail\username -p password \\" & currentpc & " wscript.exe " & strScriptPath
		InputBox "About to run:", "Command", strCommand
		intReturn = objShell.Run(strCommand, 0, True)
		If intReturn = 0 Then

Open in new window


and then you'll get an InputBox from which you can copy and paste the PSExec command to test it at a DOS prompt.

Also, make sure you're using the latest version of PSExec.exe.  I use 1.96 or 1.98 most often.

Regards,

Rob.
That's working now. Thanks a lot..Cheers. I have a little problem with another script which I will post in 2 secs.