简体   繁体   English

C#WMI远程程序无法正确执行

[英]C# WMI remote program doesn't execute correctly

I am trying to execute a program on a remote server using WMI. 我正在尝试使用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. 另外,我检查从WMI报告的结果,我得到一个0的代码,我相信这表明没有错误。

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. 如果我的代码中的某些内容没有明显错误,那么我将非常感谢有关如何解决此问题的建议,因为我是WMI流程的新手。

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. 您可以在远程计算机的任务管理器中看到进程意味着WMI执行成功。 Now the issue that the files are not being created is because the remote process create method runs the process in session 0 . 现在,未创建文件的问题是因为远程进程create方法在会话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: 您需要在连接选项中明确指定它以启用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. 我正在回答这个问题,所以我可以将它标记为MethodMan的答案。 He was right, our server team says it is turned off. 他是对的,我们的服务器团队表示已关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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