【问题标题】:C# WMI Unparsable queryC# WMI 不可解析查询
【发布时间】:2013-09-25 21:00:10
【问题描述】:

我的目标:

  • 使用参数在远程计算机上启动进程(安装应用程序)
  • 等待进程完成并在完成时引发事件。

我在此行收到错误“Unparseable query”:
var watcher = manWatch.WaitForNextEvent();

我有一个包含大约 2xx 个不同应用程序的数据库,我可以使用这种方法安装。并非每个应用程序在安装过程中都会给我这个错误。有些是成功的,有些则不是。我确实相信这是我参加活动的方式。有什么想法吗?

    private void StartAppAction(string PCName, string Command)
    {

        string Params = @"\\" + PCName + @"\C$\SoftwareInstall\" + Command;
        ConnectionOptions conOpt = new ConnectionOptions();
        conOpt.Impersonation = ImpersonationLevel.Impersonate;
        conOpt.Authentication = AuthenticationLevel.Default;
        conOpt.EnablePrivileges = true;

        ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", PCName), conOpt);
        manScope.Connect();

        ObjectGetOptions objGetOpt = new ObjectGetOptions();
        ManagementPath manPath = new ManagementPath("Win32_Process");
        ManagementClass manClass = new ManagementClass(manScope, manPath, objGetOpt);

        ManagementBaseObject inParams = manClass.GetMethodParameters("Create");
        inParams["CommandLine"] = Params;
        ManagementBaseObject outParams = manClass.InvokeMethod("Create", inParams, null);

        string queryString = "SELECT * From WIN32_ProcessStopTrace WHERE ProcessID= outParams['ProcessID']";
        WqlEventQuery wqlQuery = new WqlEventQuery(queryString);
        ManagementEventWatcher manWatch = new ManagementEventWatcher(@"\\" + PCName + @"\root\CIMV2", "SELECT * From WIN32_ProcessStopTrace WHERE ProcessID=" + outParams["ProcessID"]);

        var watcher = manWatch.WaitForNextEvent();

        if (watcher["ExitStatus"].ToString() == "0")
        {
            MessageBox.Show("Remote Exection Finished Succesfully with ExitCode 0");
        }
        else
        {
            MessageBox.Show("Remote Exection exited with the code of " + watcher["ExitStatus"].ToString());
        }

    }

【问题讨论】:

    标签: c# wmi wmi-query


    【解决方案1】:

    您的 WQL 语句中有错字 替换这一行

    string queryString = "SELECT * From WIN32_ProcessStopTrace WHERE ProcessID= outParams['ProcessID']";
    

    这个

    string queryString = String.Format("SELECT * From WIN32_ProcessStopTrace WHERE ProcessID={0}",outParams['ProcessID']);
    

    【讨论】:

    • 感谢@RRUZ 的回复。刚刚用你的代码替换了我的代码,我仍然遇到同样的问题。我还必须在 outParams["ProcessID"] 部分添加双引号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2017-10-13
    相关资源
    最近更新 更多