【问题标题】:How can I stop a remote computers windows service using WMI/CimSession?如何使用 WMI/CimSession 停止远程计算机 Windows 服务?
【发布时间】:2019-01-16 18:40:42
【问题描述】:

我正在尝试创建一个应用程序,我可以在其中停止和启动远程计算机上的特定 Windows 服务。我正在使用 Microsoft.Management.Infrastructure 在 C# 中远程连接到 WMI。现在我可以检索服务,但是如何使用我的 QueryInstance 调用 StopService 或 StartService 方法?

CimCredential cimCredential = new CimCredential(PasswordAuthenticationMechanism.Default, ".", "<userName>", secureString);
        WSManSessionOptions wSManSessionOptions = new WSManSessionOptions();
        wSManSessionOptions.AddDestinationCredentials(cimCredential);
        CimSession cimSession = CimSession.Create("<IPAddress>", wSManSessionOptions);

        IEnumerable < CimInstance> queryInstance = cimSession.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_Service WHERE Name = '<ServiceName>'");

更新:CimSession.InvokeMethod 是如何完成的。 下面是停止和启动的代码

List<CimInstance> serviceList = queryInstance.ToList();
CimInstance service = serviceList[0];
cimSession.InvokeMethod(service, "StopService", null);
Thread.Sleep(15000);
cimSession.InvokeMethod(service, "StartService", null);

【问题讨论】:

标签: c# windows-services wmi rpc


【解决方案1】:

感谢 H.G. Sandhagen 的评论,正确的方法是使用 CimSession.InvokeMethod。查看相关更新以了解其外观。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多