C# 如何从WMI远程进程获取退出代码
我正在通过WMI(Win32_process Create)远程执行进程,但无法确定如何确定进程何时完成执行。当我第一次发出命令时,有一个退出代码(0表示成功),但它告诉我进程已经成功生成C# 如何从WMI远程进程获取退出代码,c#,wmi,C#,Wmi,我正在通过WMI(Win32_process Create)远程执行进程,但无法确定如何确定进程何时完成执行。当我第一次发出命令时,有一个退出代码(0表示成功),但它告诉我进程已经成功生成 有没有办法让我知道这个过程何时结束?谢谢 下面是一个在.NET对象之上创建的示例,但它是在Powershell中编写的,很容易将其转换为C# 面对同样的问题,编写了一个简单的VMI包装: var exitStatus = WmiOperations.Run("notepad.exe", wait:10);
有没有办法让我知道这个过程何时结束?谢谢 下面是一个在.NET对象之上创建的示例,但它是在Powershell中编写的,很容易将其转换为C#
面对同样的问题,编写了一个简单的VMI包装:
var exitStatus = WmiOperations.Run("notepad.exe", wait:10);
运行的概要是:
int Run(string command, // Required
string commandline = null, // (default=none)
string machine = null, // (default=local)
string domain = null, // (default=current user domain)
string username = null, // (default=current user login)
string password = null, // (default=current user password)
SecureString securePassword = null, // (default=current user password)
double wait = double.PositiveInfinity); // (default=wait til command ends);
源代码可以从下载
给凯撒应得的,代码的灵感来自。简单地说:
- 将对象重构为静态类
- 增加了对远程处理参数的更多控制
- 重新设计事件观察程序以抑制不具吸引力的检查过程
注意:您可能必须修改WMI安全设置,以允许指定用户运行命令(请参阅)这在Windows XP/2003上有效吗?从这一点来看,ExitCode在那些OSs上似乎不可用。我正在查询Win32\u ProcessStopTrace
中的ExitStatus
属性,因此,实际上,它可能不在那些OSs上。根据您的需要,使用可能是一种合理的选择(实际上,这就是我最后用来避免更改WMI安全设置的方法)。
int Run(string command, // Required
string commandline = null, // (default=none)
string machine = null, // (default=local)
string domain = null, // (default=current user domain)
string username = null, // (default=current user login)
string password = null, // (default=current user password)
SecureString securePassword = null, // (default=current user password)
double wait = double.PositiveInfinity); // (default=wait til command ends);