简体   繁体   中英

C# WMI remote program doesn't execute correctly

I am trying to execute a program on a remote server using WMI.

When I execute my code, I can see the executable appear in the Process tab of Task Manager on the remote server, but it never actually performs its function. There should be some files in a specific folder when it runs correctly but there are none when I run it from my program.

Also, I check the result reported back from WMI and I get a code of 0, which I believe indicates there were no errors.

I have posted the code below. If something in my code is not obviously wrong then I would appreciate advice on how to troubleshoot this as I am new to the WMI process.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Management;

namespace ExecuteNetworkResources
{
    class Program
    {
        static void Main(string[] args)
        {
            var processToRun = new[] { "C:\\Program Files\\TestApplication\\mscombine.exe -c -cwa" };
            var connection = new ConnectionOptions();
            connection.Username = "domain\\user1;
            connection.Password = "password";
            var wmiScope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", "Server1"), connection);
            var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
            object result = wmiProcess.InvokeMethod("Create", processToRun);
            Console.WriteLine("Returned: " + result);
            Console.ReadLine();

        }
    }
}

You are able to see the process in task manager in remote machine means WMI execution was successful. Now the issue that the files are not being created is because the remote process create method runs the process in session 0 . Which is a non interactive desktop session.

This might be limiting the access/privilege of process to access the files system. Which is why you're not getting any error but the actions from the process are simply being ignored. You need to explicitly specify it in connection options to enable previleges:

        ConnectionOptions connection = new ConnectionOptions() {
            EnablePrivileges = true
        };

This will give the access to file system if user which is being used for connection have enough rights.

I am answering this so I Can mark it as the answer for MethodMan. He was right, our server team says it is turned off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM