【问题标题】:Invoke Application (Custom application which has arguments) on Remote System using WMI使用 WMI 在远程系统上调用应用程序(具有参数的自定义应用程序)
【发布时间】:2014-03-12 05:33:18
【问题描述】:

我有一个安装或其他系统的自定义应用程序,我想使用 WMI C# 从这个系统调用它,而不需要任何批处理文件。

此外,该应用程序具有要运行的命令参数。那么,谁能指导我我想编码的内容?

我已经尝试了一些我在此处粘贴的内容(代码 sn-p)供您参考,在运行 Notepad.exe 或 Calc.exe 的情况下可以正常工作。 事实上,它也适用于我的自定义应用程序,没有参数但没有参数。当我通过参数传递时,它会在 2 秒后启动并终止。这意味着它不会以正确/正确的格式传递参数。

private static uint CreateProcess(ManagementScope connectionScope, string exeWithPathAndArguments)
    {
        try
        {
            var objectGetOptions = new ObjectGetOptions();
            ManagementPath processPath = new ManagementPath("Win32_Process");

            using (var processTask = new ManagementClass(connectionScope, processPath, objectGetOptions))
            {
                using (var methodInParams = processTask.GetMethodParameters("Create"))
                {
                    methodInParams["CommandLine"] = exeWithPathAndArguments;
                    using (var methodOutParams = processTask.InvokeMethod("Create", methodInParams, null))
                    {
                        var err = (uint)methodOutParams["returnValue"];
                        if (err != 0)
                        {
                            var info = "see http://msdn.microsoft.com/en-us/library/windows/desktop/aa389388(v=vs.85).aspx";
                            switch (err)
                            {
                                case 2: info = "Access Denied"; break;
                                case 3: info = "Insufficient Privilege"; break;
                                case 8: info = "Unknown failure"; break;
                                case 9: info = "Path Not Found"; break;
                                case 21: info = "Invalid Parameter"; break;
                                default: info = "Unknown(Code)"; break;
                            }

                            var msg = "Failed to Start the Process, error = " + methodOutParams["returnValue"] + " (" + info + ")";
                            throw new Exception(msg);
                        }

                        return (uint)methodOutParams["processId"];
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我已经知道 PSExec 但我不想使用它。同时我不想使用批处理文件来运行我的命令。只想使用直接命令传递的方式来运行应用程序。 我的应用程序位置不在 PATH 目录中。所以,显然我需要提供一个完整的路径......

CreateProcess(connectionScope, exeWithPathAndArguments.Trim()); 其中 exeWithPathAndArguments 将是 "/"C:\Program files (x86)\Company\Application Folder\app.exe/" -argsName argvalue"

【问题讨论】:

    标签: c# wmi remote-access


    【解决方案1】:

    添加methodInParams["CurrentDirectory"] = wheretostartapp;并删除-argsName

    所以你的exeWithPathAndArguments 将是:

    \"C:\\\\Program files (x86)\\\\Company\\\\Application Folder\\\\app.exe\" argvalue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2022-12-21
      • 2023-03-22
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      相关资源
      最近更新 更多