Solved

Running VBScript Remotely

Posted on 2011-02-15
17
1,054 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:Bianchi928
  • 8
  • 5
  • 3
  • +1
17 Comments
 
LVL 4

Expert Comment

by:IceCode
ID: 34901316
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?
0
 

Author Comment

by:Bianchi928
ID: 34901336
No we aren't on the same network, but i have admin rights to all networks
0
 
LVL 11

Expert Comment

by:TheGorby
ID: 34901445
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
0
 

Author Comment

by:Bianchi928
ID: 34901730
Any suggestion of different methods that can be used to achieve this task ?
0
 
LVL 4

Expert Comment

by:IceCode
ID: 34901749
0
 

Author Comment

by:Bianchi928
ID: 34902115
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


0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34902688
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.
0
 
LVL 4

Expert Comment

by:IceCode
ID: 34902826
Are you just trying to run z.vbs on the remote machine but using another script to start it?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Bianchi928
ID: 34903547
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34903634
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

0
 

Author Comment

by:Bianchi928
ID: 34903725
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
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 34903788
Try this.  Try putting the script on a network share instead of directly on the remote PC.

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")
strScriptPath = "\\server\share\scripts\z.vbs"
Do while not objFile.AtEndOfStream
	currentpc = objFile.Readline
	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
			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

0
 

Author Comment

by:Bianchi928
ID: 34904811

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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34905217
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.
0
 

Author Comment

by:Bianchi928
ID: 34911677
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34911713
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.
0
 

Author Comment

by:Bianchi928
ID: 34911967
That's working now. Thanks a lot..Cheers. I have a little problem with another script which I will post in 2 secs.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA clear all check boxes 14 42
If not exist vbs error 4 28
VBS Script not working correctly. 1 21
Sending Attachment via CDO 3 17
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now