Why does my WMI InvokeMethod call return 0 (success) but fail to do anything remotely?

StackOverflow https://stackoverflow.com/questions/15981499

  •  03-04-2022
  •  | 
  •  

Вопрос

I have a very simple WMI method that connects to a remote server to kick off a batch file.

The return code from this execution is 0, telling me it succeeded. If I change the path to the batch file, it returns a 9 so I know it's finding the command file.

Problem is, when I look on the server, I can see that nothing has happened. The first thing this batch file does is delete the GIT repo from the drive.

My code is:

public object[] ProcessToRun;

    public void StartBuild()
    {
        ProcessToRun = BuildServerInfo.SelectedBranch == BuildServerBranch.Branch.Development ? new object[] { "c:\\src\\Build\\Batch1_Development.cmd" } : new object[] { "c:\\src\\Build\\Batch2_Release_Candidate.cmd" };
        var connection = new ConnectionOptions();

        connection.Impersonation = ImpersonationLevel.Impersonate;
        connection.Authentication = AuthenticationLevel.Packet;
        connection.EnablePrivileges = true;
        connection.Timeout = new TimeSpan(0,0,15);

        var mp = new ManagementPath("\\\\" + BuildServerInfo.BuildServer + "\\root" + "\\cimv2:Win32_LogicalDisk.DeviceID=\"C:\"");
        var ms = new ManagementScope(mp, connection);

        var mprocess = new ManagementClass(ms, new ManagementPath("Win32_Process"), new ObjectGetOptions());
        var results = mprocess.InvokeMethod("Create", ProcessToRun);
    }

Is this a problem with the code, or is there something I need to do with my server? The server is Windows 2008 R2.

Thanks.

Edit: My account has full admin privileges and the WMI users under CIMV2\Security have full access.

Это было полезно?

Решение

It appears I needed to set the working directory in order for the execution to work. I created inParams and changed my Invoke to use them as such:

var inParams = mprocess.GetMethodParameters("Create");
        inParams["CommandLine"] = @"c:\src\Build\Batch1_Development.cmd";
        inParams["CurrentDirectory"] = @"c:\src\Build";

        var outParams = mprocess.InvokeMethod("Create", inParams, null);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top