リモート マシンでサービスを再起動したいのですが、ServiceController を使用したくありません。そのマシンですべてのサービスを取得するプロセスに 21 秒かかり、次の ManagementObject が 2 秒未満で返されたからです。
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + ConfigurationManager.AppSettings["remoteMachine"] + "\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("Select * from Win32_Service where DisplayName LIKE '%" + ConfigurationManager.AppSettings["likeSerices"] + "%'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
List<ServiceObj> outList = new List<ServiceObj>();
foreach (ManagementObject m in queryCollection)
{
ServiceObj thisObject = new ServiceObj();
thisObject.DisplayName = m["DisplayName"].ToString();
thisObject.Name = m["Name"].ToString();
thisObject.Status = m["State"].ToString();
thisObject.StartMode = m["StartMode"].ToString();
outList.Add(thisObject);
}
私は今試しました: m.InvokeMethod("StopService", null); foreach ブロックで成功しませんでした。私は何をしているのですか?
ありがとうジャック