Solved

PSEXEC

Posted on 2009-05-19
12
1,972 Views
Last Modified: 2012-05-07
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?


0
Comment
Question by:VapiSoft
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 1

Accepted Solution

by:
eth4rendil earned 500 total points
ID: 24419851
1. add pause command after first command
2. psexec requies some things on/off. Try to run this batch file on remote pc

:: turn off simple file share
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa" /v forceguest /t REG_DWORD /d 00000000 /f
:: turn on admin$ share XP
rem reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 00000001 /f
:: enable file and print sharing in firewall
netsh firewall set service type = fileandprint mode = enable
add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" /v  AutoShareWks /t REG_DWORD /d 00000001 /f
0
 
LVL 3

Expert Comment

by:goss34
ID: 24419863
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?
0
 
LVL 1

Expert Comment

by:eth4rendil
ID: 24419900
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 86

Expert Comment

by:jkr
ID: 24422633
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

0
 

Author Comment

by:VapiSoft
ID: 24425516
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 24425559
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)
0
 

Author Comment

by:VapiSoft
ID: 24426029
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 24426089
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

0
 

Author Comment

by:VapiSoft
ID: 24426980
It doesn't help, it still inside the while loop.
0
 
LVL 86

Expert Comment

by:jkr
ID: 24427049
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

0
 

Author Closing Comment

by:VapiSoft
ID: 31582906
It works only if I add -u (user-name) to the command.
0
 

Author Comment

by:VapiSoft
ID: 24452481
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?
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
At the beginning of the year, the IT world was taken hostage by the shareholders of LogMeIn. Their free product, which had been free for ten years, all of the sudden became a "pay" product. Now, I am the first person who will say that software maker…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 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