Link to home
Start Free TrialLog in
Avatar of VapiSoft
VapiSoft

asked on

PSEXEC

Hi,

I am trying to run a program in a remote PC from my program.
I am using "psexec" to do it.
he command is:  "C:\Program Files\Supervisor\psexec.exe" \\OFFICE -d -e \\OFFICE\C\Temp2\Temp2.exe

When I do it from a CMD window it works OK (it opens a dialog, but when I do it from ShellExecute command
   like that
   ShellExecute(NULL,"open","C:\Program Files\Supervisor\psexec.exe","\\OFFICE -d -e \\OFFICE\C\Temp2\Temp2.exe",NULL,SW_SHOW), it opens the CMD for split second (so I cannot see the error).
Questions:
1. How can I tell the CMD not to close itself, so I can see the errors?
2. When I try to run the command (from the CMD window) on another PC (e.g PC2) I get the following error: Couldn't access PC2. I can write on PC2 from my computer. How can I enable the psexec?


ASKER CERTIFIED SOLUTION
Avatar of eth4rendil
eth4rendil

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
Avatar of goss34
goss34

Hi VapiSoft,

If you save your command statement as a batch file and add "pause" on the line underneath that should pause the window on the screen so you can see whats happening.

Does the PC that works have any different permissions? Logged on as the same user etc?
Avatar of jkr
Your 'ShellExecute()' statement is incorrect, that should be

ShellExecute(NULL,"open","C:\\Program Files\\Supervisor\\psexec.exe","\\\\OFFICE -d -e \\\\OFFICE\\C\\Temp2\\Temp2.exe",NULL,SW_SHOW)

Apart from that, try running PsExec using the following funciot using

ExecuteAndWaitForCompletion("C:\\Program Files\\Supervisor\\psexec.exe \\\\OFFICE -d -e \\\\OFFICE\\C\\Temp2\\Temp2.exe",FALSE)
DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd, BOOL bShow)
{
	STARTUPINFO         si;
	PROCESS_INFORMATION pi;
 
	BOOL                bRes;
 
	DWORD               dwCode  =   0;
 
	MSG				   msg;
 
	ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));
 
	si.cb           =   sizeof  (   STARTUPINFO);
	si.dwFlags      =   STARTF_USESHOWWINDOW;
	si.wShowWindow  =   bShow ? SW_SHOWNORMAL : SW_HIDE;
 
	bRes    =   CreateProcess   (   NULL,
								   pszCmd,
								   NULL,
								   NULL,
								   TRUE,
								   NORMAL_PRIORITY_CLASS,
								   NULL,
								   NULL,
								   &si,
								   &pi
							   );
 
	while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
																   &pi.hProcess,
																   FALSE,
																   INFINITE,
																   QS_ALLINPUT
															   )
		   )
		   {
			   while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
					   {
						   DispatchMessage     (   &msg);
					   }
		   }
 
	GetExitCodeProcess  (   pi.hProcess,    &dwCode);
 
	CloseHandle (   pi.hProcess);
	CloseHandle (   pi.hThread);
 
	return  (   dwCode);
}

Open in new window

Avatar of VapiSoft

ASKER

About the ShellExec it works now (I made a mistake in the syntax), but I still don't know how to "tell" the CMD window not to close (I cannot ad a "pause" to the command because it is not a batch file).
The problem is still activating a command on a remote computer.
I changed the registry as described by "eth4rendil" but I still get the error: Can't acccess PC2

If you use 'ExecuteAndWaitForComp()' from above, you can use a shell to redirect the output to check for errors or even keep the window open with '/k', e.g.

ExecuteAndWaitForCompletion("cmd.exe /k C:\\Program Files\\Supervisor\\psexec.exe \\\\OFFICE -d -e \\\\OFFICE\\C\\Temp2\\Temp2.exe >> err.log",FALSE)
OK, I used 'ExecuteAndWaitForComp with the exac command line as you wrote.
1. It never returns from the function ('ExecuteAndWaitForComp) it stays in the "while (PeekMessage
forever, and the err.log is empty.
Ooops, correction here, your paths contains spaces which need to be quoted - try
ExecuteAndWaitForCompletion("cmd.exe /k \"C:\\Program Files\\Supervisor\\psexec.exe\" \\\\OFFICE -d -e \\\\OFFICE\\C\\Temp2\\Temp2.exe >> err.log",TRUE);

Open in new window

It doesn't help, it still inside the while loop.
Well, that only means that the command hasn't finished - if you want that to happen, use '/c' instead of '/k', e.g.
ExecuteAndWaitForCompletion("cmd.exe /c \"C:\\Program Files\\Supervisor\\psexec.exe\" \\\\OFFICE -d -e \\\\OFFICE\\C\\Temp2\\Temp2.exe >> err.log",TRUE);

Open in new window

It works only if I add -u (user-name) to the command.
To: eth4rendil,
I don't know what happend (probably I restarted the computer) and now it does not work, and I get the "Access Denied" again.
Do you have any idea what happend?