Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a command in a remote computer?

Tags:

I have a shared folder in a server and I need to remotely execute a command on some files. How do I do that?

What services need to be running on the server to make that work?

Some details: Only C# can be used. Nothing can be installed in the server.

like image 884
user53179 Avatar asked Jan 09 '09 14:01

user53179


People also ask

How do you remotely use Command Prompt on another computer?

Step 1. Open Command Prompt, then type in “mstsc” and press Enter to evoke the Windows Remote Desktop. Step 2. Enter the Computer and User name of the remote computer to remotely control it.

Can you Run ipconfig on a remote computer?

The remote system's MAC address is the last entry in the output from the command. An alternative to the above is to use the psexec tool from Mark Russinovich's Windows Sysinternals suite of tools. Using psexec, you can run ipconfig /all remotely on the remote machine and return the result to your local computer.


2 Answers

Another solution is to use WMI.NET or Windows Management Instrumentation.

Using the .NET Framework namespace System.Management, you can automate administrative tasks using Windows Management Instrumentation (WMI).

Code Sample

using System.Management; ... var processToRun = new[] { "notepad.exe" }; var connection = new ConnectionOptions(); connection.Username = "username"; connection.Password = "password"; var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", REMOTE_COMPUTER_NAME), connection); var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()); wmiProcess.InvokeMethod("Create", processToRun); 

If you have trouble with authentication, then check the DCOM configuration.

  1. On the target machine, run dcomcnfg from the command prompt.
  2. Expand Component Services\Computers\My Computer\DCOM Config
  3. Find Windows Management Instruction, identified with GUID 8BC3F05E-D86B-11D0-A075-00C04FB68820 (you can see this in the details view).
  4. Edit the properties and then add the username you are trying to login with under the permissions tab.
  5. You may need to reboot the service or the entire machine.

NOTE: All paths used for the remote process need to be local to the target machine.

like image 87
Dennis Avatar answered Nov 26 '22 13:11

Dennis


You could use SysInternal's PsExec.

like image 42
Dirk Vollmar Avatar answered Nov 26 '22 12:11

Dirk Vollmar