How to execute a command in a remote computer?

127,229

Solution 1

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.

Solution 2

You could use SysInternal's PsExec.

Solution 3

IMO, in your case you can try this:

  1. Map the shared folder to a drive or folder on your machine. (here's how)
  2. Access the mapped drive/folder as you normally would local files.

Nothing needs to be installed. No services need to be running except those that enable folder sharing.

If you can access the shared folder and maps it on your machine, most things should work just like local files, including command prompts and all explorer-enhancement tools.

This is different from using PsExec (or RDP-ing in) in that you do not need to have administrative rights and/or remote desktop/terminal services connection rights on the remote server, you just need to be able to access those shared folders.

Also make sure you have all the necessary security permissions to run whatever commands/tools you want to run on those shared folders as well.


If, however you wish the processing to be done on the target machine, then you can try PsExec as @divo and @recursive pointed out, something alongs:

PsExec \\yourServerName -u yourUserName cmd.exe

Which will brings gives you a command prompt at the remote machine. And from there you can execute whatever you want.

I am not sure but I think you need either the Server (lanmanserver) or the Terminal Services (TermService) service to be running (which should have already be running).

Share:
127,229

Related videos on Youtube

user53179
Author by

user53179

Updated on August 21, 2020

Comments

  • user53179
    user53179 over 3 years

    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.

    • GEOCHET
      GEOCHET over 15 years
      Do you want the command to run on the remote machine or the local machine?
    • Anderson Green
      Anderson Green over 11 years
      Which operating systems are you working with? Is this only about Windows-to-Windows communication?
  • B Bulfin
    B Bulfin over 15 years
    If you run executables via a shared folder, they don't run on the sharing server. They run on the browsing client.
  • chakrit
    chakrit over 15 years
    If some files is not a lot of files, then I don't think that is a problem.
  • Nicolas Fall
    Nicolas Fall over 11 years
    so far all my attempts to wrap PsExec to call appcmd.exe have failed, or gotten stuck after reading a success length > 1046
  • unruledboy
    unruledboy about 11 years
    this solution can run an application, but without interaction (no interface)
  • Contango
    Contango over 10 years
    Pity, that website seems to be down - does anyone know if there is a mirror?
  • spy
    spy over 4 years
    what's the method for command line arguments. is it just appended to the executable name?
  • user122222
    user122222 almost 4 years
    is there alternative for .net core?