/// <summary> /// Send a stop command to a Service and wait until the Service is stopped /// </summary> /// <param name="ServiceName"></param> /// <returns>Returns an ArrayList of Services which has stopped because of service dependencies </returns> public ArrayList StopService(string ServiceName) { try { ManagementObjectCollection Dependencies; ManagementObject Service; WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; oProv.mScope.Options.EnablePrivileges = true; Service = oProv.GetObject("Win32_Service.Name='" + ServiceName + "'"); Dependencies = oProv.ExecuteQuery("Associators of {Win32_Service.Name='" + ServiceName + "'} Where AssocClass=Win32_DependentService Role=Antecedent"); ArrayList Result = new ArrayList(); foreach (ManagementObject MO in Dependencies) { if (MO.GetPropertyValue("State").ToString().ToLower() == "running") { Result.AddRange(StopService(MO.GetPropertyValue("Name").ToString())); } } Result.Add(ServiceName); bStopService(Service); return(Result); } catch (Exception ex) { throw (ex); } }
/// <summary> /// Get a DWORD Registry Value as string /// </summary> /// <param name="hDefKey">HKLM = 2147483650</param> /// <param name="sSubKeyName"></param> /// <param name="sValueName"></param> /// <param name="DefaultValue">return string if key or value does not exist</param> /// <returns></returns> public string GetDWord(UInt32 hDefKey, string sSubKeyName, string sValueName, string DefaultValue) { { String result = ""; try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"ROOT\default"; ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("GetDWORDValue"); inParams["hDefKey"] = hDefKey; inParams["sSubKeyName"] = sSubKeyName; inParams["sValueName"] = sValueName; ManagementBaseObject outParams = oProv.ExecuteMethod("StdRegProv", "GetDWORDValue", inParams); if (outParams.GetPropertyValue("ReturnValue").ToString() == "0") { if (outParams.GetPropertyValue("uValue") != null) { result = outParams.GetPropertyValue("uValue").ToString(); } } return(result); } catch { return(DefaultValue); } } }
/// <summary> /// Get an ArrayList of all Subfolders /// </summary> /// <param name="Path"></param> /// <returns></returns> public ArrayList SubFolders(string Path) { lock (oWMIProvider) { ArrayList result = new ArrayList(); try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; ManagementObjectCollection MOC = oProv.ExecuteQuery(@"Associators of {Win32_Directory.Name='" + Path + @"'} where AssocClass=Win32_Subdirectory ResultRole=PartComponent"); foreach (ManagementObject MO in MOC) { try { result.Add(MO.GetPropertyValue("Name").ToString().ToLower()); } catch { } } return(result); } catch { } return(result); } }
/// <summary> /// Delete a Folder with all Subfolders and Files /// </summary> /// <param name="Path"></param> public void DeleteFolder(string Path) { if (!string.IsNullOrEmpty(Path)) { try { ManagementObjectCollection MOC; //Delete all Subfolders foreach (string sSub in SubFolders(Path)) { DeleteFolder(sSub); } WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; string FolderPath = Path.Replace(@"\", @"\\"); //ManagementObjectCollection MOC = oWMIProvider.ExecuteQuery("SELECT * FROM Win32_Directory WHERE Drive like '" + (FolderPath.Split('\\'))[0] + "' and Path like '" + (CachePath.Split(':'))[1] + @"\\' and FileType = 'File Folder'"); MOC = oProv.ExecuteQuery("SELECT * FROM Win32_Directory WHERE Name = '" + FolderPath + "'"); //Delete the root Folder foreach (ManagementObject MO in MOC) { try { ManagementBaseObject inParams = MO.GetMethodParameters("DeleteEx"); ManagementBaseObject result = MO.InvokeMethod("DeleteEx", inParams, null); } catch { } } } catch { } } }
/// <summary> /// Delete multiple Files /// </summary> /// <param name="Drive">Disk Drive like 'c:'</param> /// <param name="Path">Path like '\\windows\\'</param> /// <param name="Filename">Filename like 'kb%'</param> /// <param name="Extension">Extension like 'log'</param> public void DeleteFiles(string Drive, string Path, string Filename, string Extension) { try { ManagementObjectCollection MOC; if (!Path.EndsWith(@"\")) { Path = Path + @"\"; } WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; string FolderPath = Path.Replace(@"\", @"\\"); //ManagementObjectCollection MOC = oWMIProvider.ExecuteQuery("SELECT * FROM Win32_Directory WHERE Drive like '" + (FolderPath.Split('\\'))[0] + "' and Path like '" + (CachePath.Split(':'))[1] + @"\\' and FileType = 'File Folder'"); MOC = oProv.ExecuteQuery(string.Format("SELECT * FROM CIM_DataFile WHERE Drive = '{0}' and Path = '{1}' and Filename like '{2}' and Extension like '{3}'", new object[] { Drive, FolderPath, Filename, Extension })); //Delete the root Folder foreach (ManagementObject MO in MOC) { try { ManagementBaseObject inParams = MO.GetMethodParameters("Delete"); ManagementBaseObject result = MO.InvokeMethod("Delete", inParams, null); } catch { } } } catch { } }
/// <summary> /// Get an ArrayList of all Regisry Values /// </summary> /// <param name="hDefKey">2147483650 = HKLM</param> /// <param name="sSubKeyName"></param> /// <returns>RegistryValues</returns> public List <string> RegValuesList(UInt32 hDefKey, string sSubKeyName) { try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"ROOT\default"; ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("EnumValues"); inParams["hDefKey"] = hDefKey; inParams["sSubKeyName"] = sSubKeyName; ManagementBaseObject outParams = oProv.ExecuteMethod("StdRegProv", "EnumValues", inParams); List <string> result = new List <string>(); if (outParams.GetPropertyValue("ReturnValue").ToString() == "0") { if (outParams.GetPropertyValue("sNames") != null) { result.AddRange(outParams.GetPropertyValue("sNames") as String[]); } } return(result); } catch { throw; } }
/// <summary> /// Create a new process on a remote system. For security reasons this method cannot be used to start an interactive process remotely. /// </summary> /// <param name="CommandLine"></param> /// <param name="CurrentDirectory"></param> /// <param name="ProcessStartupInformation">A Win32_ProcessStartup Object <see href="http://msdn2.microsoft.com/en-us/library/aa394375.aspx"/></param> /// <returns>ProcessID</returns> public int StartProcess(string CommandLine, string CurrentDirectory, ManagementBaseObject ProcessStartupInformation) { if (CurrentDirectory == "") { CurrentDirectory = null; } WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; ManagementClass MC = oProv.GetClass("Win32_Process"); ManagementBaseObject inParams = MC.GetMethodParameters("Create"); inParams["CommandLine"] = CommandLine; inParams["CurrentDirectory"] = CurrentDirectory; inParams["ProcessStartupInformation"] = ProcessStartupInformation; ManagementBaseObject Result = MC.InvokeMethod("Create", inParams, null); switch (int.Parse(Result.GetPropertyValue("ReturnValue").ToString())) { case 0: return(int.Parse(Result.GetPropertyValue("ProcessID").ToString())); case 2: throw new System.Security.SecurityException("Access denied"); case 3: throw new System.Security.SecurityException("Insufficient privilege"); case 9: throw new Exception("Path not found: " + CommandLine); case 21: throw new Exception("Invalid parameter"); default: throw new Exception("Unknown failure"); } }
/// <summary> /// Create a remote process /// </summary> /// <param name="CommandLine"></param> /// <param name="CurrentDirectory"></param> /// <param name="ProcessStartupInformation"></param> /// <returns>Returns ProcessID or 0 if failed</returns> public UInt32 CreateProcess(string CommandLine, string CurrentDirectory, ManagementBaseObject ProcessStartupInformation) { try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; ManagementClass oProcess = oProv.GetClass("Win32_Process"); ManagementBaseObject inParams = oProcess.GetMethodParameters("Create"); inParams["CommandLine"] = CommandLine; inParams["CurrentDirectory"] = CurrentDirectory; inParams["ProcessStartupInformation"] = ProcessStartupInformation; ManagementBaseObject outParams = oProcess.InvokeMethod("Create", inParams, null); if (int.Parse(outParams["ReturnValue"].ToString()) == 0) { return(UInt32.Parse(outParams["ProcessId"].ToString())); } else { return(0); } } catch { throw new Exception("Unable to start command: " + CommandLine); } }
/// <summary> /// local CCM_ComponentClientConfig policy from requested policy /// </summary> /// <param name="ComponentName"></param> /// <returns>ROOT\ccm\Policy\Machine\RequestedConfig:CCM_ComponentClientConfig</returns> public ManagementObjectCollection Component_Requested(string ComponentName) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"ROOT\ccm\Policy\Machine\RequestedConfig"; return(oProv.ExecuteQuery("SELECT * FROM CCM_ComponentClientConfig WHERE ComponentName = '" + ComponentName + "'")); }
/// <summary> /// Get the Win32_Service ManagementObject of a Service /// </summary> /// <param name="ServiceName"></param> /// <returns>Win32_Service ManagementObject</returns> public ManagementObject GetService(String ServiceName) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; return(oProv.GetObject("Win32_Service.Name='" + ServiceName + "'")); }
/// <summary> /// Delete a single File /// </summary> /// <param name="FilePath"></param> public void DeleteFile(string FilePath) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; ManagementObject MO = oProv.GetObject("CIM_DataFile.Name='" + FilePath + "'"); MO.InvokeMethod("Delete", null); }
/// <summary> /// Constructor /// </summary> /// <param name="SMSCli">Instance of smsclictr.automation.SMSClient</param> /// <example> /// C#: /// <code> /// using System; /// using System.Collections.Generic; /// using System.Text; /// using smsclictr.automation; /// /// namespace ConsoleApplication1 /// { /// class Program /// { /// static void Main(string[] args) /// { /// SMSClient oClient = new SMSClient("smsserver"); /// CCMSetup oCCMSetup = new CCMSetup(oClient); /// } /// } /// } /// </code> /// </example> public CCMSetup(SMSClient SMSCli) { if (SMSCli != null) { oSMSClient = SMSCli; oWMIProvider = SMSCli.Connection; sHostname = oWMIProvider.mScope.Path.Server; } }
/// <summary> /// Kill a process by ProcessID /// </summary> /// <param name="ProcessID"></param> /// <returns></returns> public int KillProcess(int ProcessID) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; oProv.mScope.Options.EnablePrivileges = true; ManagementObject MO = oProv.GetObject("Win32_Process.Handle='" + ProcessID.ToString() + "'"); ManagementBaseObject inParams = MO.GetMethodParameters("Terminate"); ManagementBaseObject Res = MO.InvokeMethod("Terminate", inParams, null); return(int.Parse(Res.GetPropertyValue("ReturnValue").ToString())); }
/// <summary> /// Set the Service start mode /// </summary> /// <param name="ServiceName"></param> /// <param name="StartMode"> /// <list type="table"> /// <listheader><term>Value</term><description>Meaning</description></listheader> /// <item><term>Boot</term><description>Device driver started by the operating system loader. This value is valid only for driver services.</description></item> /// <item><term>System</term><description>Device driver started by the operating system initialization process. This value is valid only for driver services.</description></item> /// <item><term>Automatic</term><description>Service to be started automatically by the service control manager during system startup.</description></item> /// <item><term>Manual</term><description>Service to be started by the service control manager when a process calls the StartService method.</description></item> /// <item><term>Disabled</term><description>Service that can no longer be started.</description></item> /// </list> /// </param> /// <returns></returns> public int SetServiceStartMode(string ServiceName, string StartMode) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; ManagementObject Service = oProv.GetObject("Win32_Service.Name='" + ServiceName + "'"); ManagementBaseObject inParams = Service.GetMethodParameters("ChangeStartMode"); inParams["StartMode"] = StartMode; ManagementBaseObject Result = Service.InvokeMethod("ChangeStartMode", inParams, null); return(int.Parse(Result.GetPropertyValue("ReturnValue").ToString())); }
/// <summary> /// CCM_ComponentClientConfig from actual policy /// </summary> /// <param name="ComponentName"></param> /// <returns>ROOT\ccm\Policy\Machine\ActualConfig:CCM_ComponentClientConfig</returns> public ManagementObject Component_Actual(string ComponentName) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"ROOT\ccm\Policy\Machine\ActualConfig"; ManagementObjectCollection MOC = oProv.ExecuteQuery("SELECT * FROM CCM_ComponentClientConfig WHERE ComponentName = '" + ComponentName + "'"); foreach (ManagementObject MO in MOC) { return(MO); } return(null); }
/// <summary> /// Set Registry String Value /// </summary> /// <param name="hDefKey">HKLM = 2147483650</param> /// <param name="sSubKeyName"></param> /// <param name="sValueName"></param> /// <param name="sValue"></param> public void SetStringValue(UInt32 hDefKey, String sSubKeyName, String sValueName, String sValue) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"ROOT\default"; ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("SetStringValue"); inParams["hDefKey"] = hDefKey; inParams["sSubKeyName"] = sSubKeyName; inParams["sValueName"] = sValueName; inParams["sValue"] = sValue; oProv.ExecuteMethod("StdRegProv", "SetStringValue", inParams); }
/// <summary> /// Start a Service /// </summary> /// <param name="ServiceName"></param> /// <returns>Return Value</returns> public int StartService(String ServiceName) { ManagementObject Service; try { ManagementEventWatcher watcher = new ManagementEventWatcher(); lock (oWMIProvider) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; oProv.mScope.Options.EnablePrivileges = true; Service = oProv.GetObject("Win32_Service.Name='" + ServiceName + "'"); WqlEventQuery query = new WqlEventQuery("__InstanceModificationEvent"); query.Condition = "TargetInstance ISA 'Win32_Service' AND TargetInstance.Name='" + ServiceName + "' AND TargetInstance.State='Running'"; query.WithinInterval = new TimeSpan(0, 0, 2); watcher = new ManagementEventWatcher(oProv.mScope, query); watcher.EventArrived += new EventArrivedEventHandler(ServiceEventArrivedHandler); } //watcher.Options.Timeout = new TimeSpan(0, 0, 15); watcher.Start(); Object result = Service.InvokeMethod("StartService", null); int iResult = int.Parse(result.ToString()); if (iResult == 0) { MRE.WaitOne(new TimeSpan(0, 0, 30), true); } watcher.Stop(); watcher.Dispose(); Service.Get(); if (Service["State"].ToString() == "Running") { return(iResult); } else { return(iResult); } } catch (Exception ex) { //0x80080005 wmi stopped throw (ex); } }
/// <summary> /// Delete all instances from a Query Result /// </summary> /// <param name="sNamespace"></param> /// <param name="sQuery"></param> public void DeleteQueryResults(string sNamespace, string sQuery) { try { WMIProvider oProv = new WMIProvider(this.mscope.Clone()); oProv.mScope.Path.NamespacePath = sNamespace; ManagementObjectCollection oResults = oProv.ExecuteQuery(sQuery); foreach (ManagementObject oInst in oResults) { oInst.Delete(); } } catch { } }
/// <summary> /// Install a Windows Installer Package /// </summary> /// <param name="sPath"></param> /// <param name="sOptions"></param> /// <returns>MSI Exit Code</returns> public UInt32 InstallMSI(string sPath, string sOptions) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; ManagementClass MC = oProv.GetClass("Win32_Product"); ManagementBaseObject inParams = MC.GetMethodParameters("Install"); inParams.SetPropertyValue("PackageLocation", sPath); inParams.SetPropertyValue("Options", sOptions); ManagementBaseObject Result = MC.InvokeMethod("Install", inParams, null); return((UInt32)Result.GetPropertyValue("ReturnValue")); }
/// <summary> /// Get DCM Baselines (Class SMS_DesiredConfiguration) /// </summary> /// <returns>Class SMS_DesiredConfiguration</returns> public ManagementObjectCollection CCM_DCMBaselines() { if (oDCMBaselines == null) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\ccm\dcm"; ManagementObjectCollection MOC = oProv.ExecuteQuery("SELECT * FROM SMS_DesiredConfiguration"); oDCMBaselines = MOC; return(MOC); } else { return(oDCMBaselines); } }
/// <summary> /// Uninstall a Windows Installer Package based on the MSI-ID /// </summary> /// <param name="MSIID"></param> /// <returns>MSI Exit Code or 99 if MSIID was not found</returns> public UInt32 UninstallMSI_ID(string MSIID) { ManagementObjectCollection CliAgents; WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; CliAgents = oProv.ExecuteQuery("SELECT * FROM Win32_Product WHERE IdentifyingNumber ='" + MSIID + "'"); foreach (ManagementObject CliAgent in CliAgents) { ManagementBaseObject inParams = CliAgent.GetMethodParameters("Uninstall"); ManagementBaseObject result = CliAgent.InvokeMethod("Uninstall", inParams, null); return(UInt32.Parse(result.GetPropertyValue("ReturnValue").ToString())); } return(99); }
/// <summary> /// Kill a process by ProcessName /// </summary> /// <param name="ProcessName"></param> /// <returns></returns> public int KillProcess(string ProcessName) { ManagementObjectCollection MOC; int Res = 0; WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; oProv.mScope.Options.EnablePrivileges = true; MOC = oProv.ExecuteQuery("SELECT * FROM Win32_Process WHERE Name='" + ProcessName + "'"); foreach (ManagementObject MO in MOC) { ManagementBaseObject inParams = MO.GetMethodParameters("Terminate"); Res = int.Parse((MO.InvokeMethod("Terminate", inParams, null)).GetPropertyValue("ReturnValue").ToString()); } return(Res); }
/// <summary> /// Set a DWORD Registry Value from a UInt32 Value /// </summary> /// <param name="hDefKey"></param> /// <param name="sSubKeyName"></param> /// <param name="sValueName"></param> /// <param name="uValue"></param> public void SetDWord(UInt32 hDefKey, string sSubKeyName, string sValueName, UInt32 uValue) { try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"ROOT\default"; ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("SetDWORDValue"); inParams["hDefKey"] = hDefKey; inParams["sSubKeyName"] = sSubKeyName; inParams["sValueName"] = sValueName; inParams["uValue"] = uValue; ManagementBaseObject outParams = oProv.ExecuteMethod("StdRegProv", "SetDWORDValue", inParams); } catch { throw; } }
/// <summary> /// Check if Directory Exists /// </summary> /// <param name="Path"></param> /// <returns>True = Directory exists</returns> public Boolean DirExist(string Path) { if (!string.IsNullOrEmpty(Path)) { try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); string FolderPath = Path.Replace(@"\", @"\\"); ManagementObjectCollection MOC = oProv.ExecuteQuery("SELECT * FROM Win32_Directory WHERE Name = '" + FolderPath + "'"); foreach (ManagementObject MO in MOC) { return(true); } } catch { } } return(false); }
/// <summary> /// Check if File Exists /// </summary> /// <param name="FilePath"></param> /// <returns>True = File exists</returns> public Boolean FileExist(string FilePath) { if (!string.IsNullOrEmpty(FilePath)) { try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); FilePath = FilePath.Replace(@"\", @"\\"); ManagementObjectCollection MOC = oProv.ExecuteQuery("SELECT * FROM CIM_Datafile WHERE Name = '" + FilePath + "'"); foreach (ManagementObject MO in MOC) { return(true); } } catch { } } return(false); }
/// <summary> /// The cached CCM_SoftwareDistributionClientConfig Class. /// </summary> /// <returns>root\ccm\policy\machine\requestedconfig:CCM_SoftwareDistributionClientConfig</returns> /// <seealso cref="M:smsclictr.automation.SMSComponents.CCM_SoftwareDistributionClientConfig(System.Boolean)"/> public ManagementObject CCM_SoftwareDistributionClientConfig() { if (oCCM_SoftwareDistributionClientConfig == null) { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\ccm\policy\machine\requestedconfig"; ManagementObjectCollection MOC = oProv.ExecuteQuery("SELECT * FROM CCM_SoftwareDistributionClientConfig"); foreach (ManagementObject MO in MOC) { oCCM_SoftwareDistributionClientConfig = MO; return(MO); } return(null); } else { return(oCCM_SoftwareDistributionClientConfig); } }
/// <summary> /// Delete a Registry Key and all Subkeys /// </summary> /// <param name="hDefKey"></param> /// <param name="sSubKeyName"></param> public void DeleteKey(UInt32 hDefKey, String sSubKeyName) { try { //Delete all subkeys ArrayList Subkeys = RegKeys(hDefKey, sSubKeyName); foreach (string skey in Subkeys) { DeleteKey(hDefKey, sSubKeyName + @"\" + skey); } WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"ROOT\default"; ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("DeleteKey"); inParams["hDefKey"] = hDefKey; inParams["sSubKeyName"] = sSubKeyName; oProv.ExecuteMethod("StdRegProv", "DeleteKey", inParams); } catch { throw; } }
private bool bStopService(ManagementObject Service) { try { WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\cimv2"; WqlEventQuery query = new WqlEventQuery("__InstanceModificationEvent"); query.Condition = "TargetInstance ISA 'Win32_Service' AND TargetInstance.Name='" + Service.GetPropertyValue("Name").ToString() + "' AND TargetInstance.State='Stopped'"; query.WithinInterval = new TimeSpan(0, 0, 2); ManagementEventWatcher watcher = new ManagementEventWatcher(oProv.mScope, query); watcher.EventArrived += new EventArrivedEventHandler(ServiceEventArrivedHandler); //watcher.Options.Timeout = new TimeSpan(0, 0, 15); watcher.Start(); Object result = Service.InvokeMethod("StopService", null); if ((UInt32)result == 0) { MRE.WaitOne(new TimeSpan(0, 0, 60), true); } watcher.Stop(); watcher.Dispose(); Service.Get(); if (Service["State"].ToString() == "Stopped") { return(true); } else { return(false); } } catch (Exception ex) { throw (ex); } }
/// <summary> /// Constructor /// </summary> /// <param name="oProvider"></param> public WMIRegistry(WMIProvider oProvider) { oWMIProvider = oProvider; }
/// <summary> /// WMIService Constructor /// </summary> /// <param name="oProvider"></param> public WMIService(WMIProvider oProvider) { oWMIProvider = oProvider; }