![](/img/trans.png)
[英]How do I start/stop/restart a windows service on a remote machine using .net
[英]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);
感謝HG Sandhagen的評論,正確的方法是使用CimSession.InvokeMethod。 請參閱有問題的更新,以了解其外觀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.