Solved

PSEXEC

Posted on 2009-05-19
12
1,955 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Like many organizations, your foray into cloud computing may have started with an ancillary or security service, like email spam and virus protection. For some, the first or second step into the cloud was moving email off-premise. For others, a clou…
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…
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…

706 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

21 Experts available now in Live!

Get 1:1 Help Now