Running commands on remote machine

1.7k Views Asked by At

I have written a wmi application which runs a batch file on a remote machine.

This batch file writes the output to the directory-string passed to it as an argument.

When I launch my application, and choose drive C as the output directory, everything works. But, when I choose a network drive, it doesn't work, for some reason.

(This network drive is the company's users drive, and mapped for each user on the network).

If I run the same exact command (with the network drive) manually from within the remote machine, it also works...

ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.Username = sUsername;
connOptions.Password = sPassword;
connOptions.Authority = "NTLMDomain:" + sDomain;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", sHost), connOptions);
manScope.Connect();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

for (int i = 0; i < listOfJobs.Items.Count; i++)
{
   listOfJobs.SetSelected(i, true);
   inParams["CommandLine"] = listOfJobs.SelectedItem.ToString();
   ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
}

Example for job strings:

Working script: C:\Scripts\BatchScript -p C:\Test C:\OutputDir

Failing script: C:\Scripts\BatchScript -p C:\Test U:\OutputDir

Any ideas?

1

There are 1 best solutions below

4
On

Set the ImpersonationLevel to Delegate. Impersonate only allows one network hop, i.e. the server can only access files locally or those back on the client. To access a third machine (the network drive) means another hop hence Delegate is required.