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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot get PsExec command to work from within vb6 1

Status
Not open for further replies.

MakeItSo

Programmer
Oct 21, 2003
3,316
DE
Hi friends,

I must be doing something wrong but I just don't know what!
I have downloaded PsTools and put the PsExec in my project folder as well as in System32.

I am trying to execute a DOS command on a remote machine and I'm trying like this:
Code:
Dim Serv As String
Dim checker As String
Dim theCommand As String

Serv = "\\[remote machine] "
checker = "netstat"
theCommand = "psexec " & VNCServ & checker
Shell Chr(34) & theCommand & Chr(34)
DoEvents
Here I 've already shortened the command to no parameters, yet I'll always get "file not found" at the Shell line.
It seems it cannot find PsExec.
I've also tried with:
Code:
theCommand = App.Path & \psexec.exe " & ...
but to no avail. Always "file not found".

the command itself works fine if executed from a command line.
ponder

By the way, this is what I actually want to achieve, in case anyone knows how to do this with WMI or whatever:

I am trying to query the VNC status of that remote machine with a simple
Code:
netstat -an | find ":5900" >M:\temp\temp.txt
where M is a mapped drive that is reachable as "M" from any computer in our domain.
After that I want to query the resulting text file to know
a) whether any client is connected and
b) which clients are connected.

With this info, I would like to create some sort of "VNC traffic lights". That is why I do this from VB6; I need a form to display further information.

Any idea what's up here?

Thanks & greetz!
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Firstly

>Shell Chr(34) & theCommand & Chr(34)

should be

Shell theCommand

However, that still won't work as expected, as it'll run against your local machine because:

>[red]Serv[/red] = "\\[remote machine] "
>checker = "netstat"
>theCommand = "psexec " & [red]VNCServ[/red] & checker

Might I suggest that you ensure Option Explicit is set , if you have not already, as it catches this sort of error.

 
Hi strongm

Thanks for that hint with the quotes. Sometimes I'm just plain *dunce*

The second is not a thing though. I had manually renamed it here in the post while posting and forgot the second one. In my code it's both correct.

The quotes have fixed it - a bit, as in no more "file not found" error. Still no output though. I've re-inserted the output parameter.
The code runs without error but it does not produce the text file:
Code:
VNCServ = "\\appserv1 "
checker = "netstat >M:\temp\temp.txt"
theCommand = "psexec " & VNCServ & checker
Shell theCommand
DoEvents

The code takes about a second to finish so it's clearly doing something. Alas: no text file.
I've also modified it to ">C:\temp.txt" - no difference.

One more thing: the remote computer is running XP SP3 but the computer executing the script is running Windows 7 Pro, UAC is deactivated.

Thanks!
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
No idea what the [tt]DoEvents[/tt] is doing in there.

You are trying to pass a cmd.exe command string with piping and output redirection. I'm fairly sure that doesn't work.

PsExec expects a program to run and the arguments to pass to it. You are running NetStat and passing junk to it that it does not understand, [tt]>M:\temp\temp.txt[/tt] in your most recent example.

Instead you need to run cmd.exe at the remote system and pass the stuff to that to make it run the programs you want. Be sure to add the switch to make it exit after carrying out the commands.
 
Got it!
Finally found something and with dilettante's explanation is makes sense.
1.) Need to add "cmd /c" before psexec.
2.) if user name and password are not specified, there won't be access to network drives.

@dilettante: sometimes I just feel like DoEvents. It's like yelling at the darned code especially if it isn't doing it! [tongue]
So, here's the code that will produce the correct output, i.e. port information for port 5900 to check VNC status:
Code:
VNCServ = "\\[remote computer] "
checker = "netstat -an | find "":5900"" >M:\temp\temp.txt"
theCommand = "cmd /c psexec -u [user] -p [password] " & VNCServ & checker
Shell theCommand

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
I consider [tt]DoEvents[/tt] so bad that the IDE should transfer $10 of your funds to a charity every time you use one. It often leads to hard to diagnose problems, and most of the time it is a sign you are doing things wrong.

Good catch on the need for a logon with network access rights. I had missed that you were using a mapped drive.


I normally handle this differently, using my ShellPipe control to control one or more copies of PsExec. This means I don't need the overhead of cmd.exe at the remote system, and I can get the output back directly rather than dumping it to some file (then twiddling my thumbs waiting for completion, then opening and reading the file, then...).

But I won't go into detail, you've been through enough.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top