Solved

PSEXEC

Posted on 2009-05-19
12
2,004 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

636 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