Solved

PSEXEC

Posted on 2009-05-19
12
1,966 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
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.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now