如何使用 WMI/CimSession 停止远程计算机 windows 服务?
How can I stop a remote computers windows service using WMI/CimSession?
我正在尝试创建一个应用程序,我可以在其中停止和启动远程计算机上的特定 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);
感谢H.G。 Sandhagen 的评论正确的方法是使用CimSession.InvokeMethod。请参阅有问题的更新以了解它的外观。
我正在尝试创建一个应用程序,我可以在其中停止和启动远程计算机上的特定 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);
感谢H.G。 Sandhagen 的评论正确的方法是使用CimSession.InvokeMethod。请参阅有问题的更新以了解它的外观。