protected internal static ServiceInfo CreateServiceInfo(ManagementObject managementObject) { if (managementObject == null) return null; ServiceInfo rtn = null; try { rtn = new ServiceInfo { _managementObject = managementObject, InteractWithDesktop = false, State = Helpers.ToEnum<ServiceState>(managementObject["State"]), Status = (string)managementObject["Status"], Name = (string)managementObject["Name"], DisplayName = (string)managementObject["DisplayName"], PathName = (string)managementObject["PathName"], ProcessId = (uint)managementObject["ProcessId"], Started = (bool)managementObject["Started"], StartMode = Helpers.ToEnum<StartMode>(managementObject["StartMode"]), ServiceType = Helpers.ToEnum<ServiceType>(managementObject["ServiceType"]), InstallDate = (string)managementObject["InstallDate"], Description = (string)managementObject["Description"], Caption = (string)managementObject["Caption"], Username = (string)managementObject["StartName"] }; } catch (Exception ex) { Console.WriteLine(ex); } return rtn; }
/// <summary> /// Gets the state of the service. /// </summary> /// <param name="svcInfo">The service information object. The Name property is used.</param> /// <returns></returns> public ServiceState GetServiceState(ServiceInfo svcInfo) { return GetServiceInfo(svcInfo).State; }
/// <summary> /// Gets a service by name. /// </summary> /// <param name="svcInfo">The service information object. The Name property is used</param> /// <returns></returns> public ServiceInfo GetServiceInfo(ServiceInfo svcInfo) { return GetServiceInfo(svcInfo.Name); }
/// <summary> /// Waits for the service to be in a given state. /// </summary> /// <param name="service">The service information object.</param> /// <param name="state">The state to wait for.</param> /// <param name="timeoutMs">The timeout in milliseconds.</param> /// <returns>True when service is in given state. /// False if service is not in given state by the end of the timeout period</returns> public ReturnValue WaitForState(ServiceInfo service, ServiceState state, int timeoutMs = 5000) { DateTime endDate = DateTime.Now.AddMilliseconds(timeoutMs); do { service = GetServiceInfo(service); if (service.State == state) return ReturnValue.Success; Thread.Sleep(500); } while (endDate > DateTime.Now); return ReturnValue.ServiceRequestTimeout; }
/// <summary> /// Stops the service. /// </summary> /// <param name="service">The service information object. Name property is used</param> /// <returns></returns> public ReturnValue StopService(ServiceInfo service) { ManagementObject svc = service.ManagementObject() ?? GetService(service.Name); return InvokeMethod(svc, "StopService"); }
/// <summary> /// Uninstalls the service from the system. /// </summary> /// <param name="svcInfo">The service information object. Name property is used</param> /// <returns></returns> public ReturnValue UninstallService(ServiceInfo service) { ManagementObject svc = service.ManagementObject() ?? GetService(service.Name); return InvokeMethod(svc, "delete"); }
/// <summary> /// Changes the specified service username, password or path. /// </summary> /// <param name="service">The service to update.</param> /// <returns></returns> public ReturnValue Change(ServiceInfo service) { if (!string.IsNullOrWhiteSpace(service.Password) && !Security.IsValidLogin(service.Username, service.Password, Server)) return ReturnValue.ServiceLogonFailure; //ensure user account has logon as service rights Security.SetLogonAsAService(service.Username, Server); ManagementObject svc = service.ManagementObject() ?? GetService(service.Name); ManagementClass mc = ScopedClass("Win32_Service"); mc.Scope = Scope(); ManagementBaseObject inParams = mc.GetMethodParameters("Change"); if (!string.IsNullOrWhiteSpace(service.Password)) { inParams["StartName"] = service.Username; inParams["StartPassword"] = service.Password; } inParams["PathName"] = service.PathName; return InvokeMethod(svc, "Change", inParams); }
/// <summary> /// Determines whether a service is installed by name. /// </summary> /// <param name="svcInfo">The service information object. Name property is used</param> /// <returns>True if installed</returns> public bool IsServiceInstalled(ServiceInfo svcInfo) { return IsServiceInstalled(svcInfo.Name); }
/// <summary> /// Installs a windows service. Ensures user has Logon as a service right by calling <see cref="Security.SetLogonAsAService"/> /// </summary> /// <param name="service">The service information object. <see cref="ServiceInfo"/></param> /// <returns></returns> public ReturnValue InstallService(ServiceInfo service) { if (!Security.IsValidLogin(service.Username, service.Password, Server)) return ReturnValue.ServiceLogonFailure; //ensure user account has logon as service rights Security.SetLogonAsAService(service.Username, Server); ManagementClass mc = ScopedClass("Win32_Service"); ManagementBaseObject inParams = mc.GetMethodParameters("create"); inParams["Name"] = service.Name; inParams["DisplayName"] = string.IsNullOrWhiteSpace(service.DisplayName) ? service.Name : service.DisplayName; inParams["PathName"] = service.PathName; inParams["ServiceType"] = service.ServiceType; inParams["ErrorControl"] = service.ErrorHandle; inParams["StartMode"] = service.StartMode == StartMode.Auto ? "Automatic" : service.StartMode.ToString(); inParams["DesktopInteract"] = service.InteractWithDesktop; inParams["StartName"] = service.Username; inParams["StartPassword"] = service.Password; inParams["LoadOrderGroup"] = service.LoadOrderGroup; inParams["LoadOrderGroupDependencies"] = service.LoadOrderGroupDependencies; inParams["ServiceDependencies"] = service.Dependencies; var rtn = InvokeMethod(mc, "Create", inParams); SetDescription(service.Name, service.Description); return rtn; }
/// <summary> /// Installs a windows service. Ensures user has Logon as a service right by calling <see cref="Security.SetLogonAsAService"/> /// </summary> /// <param name="svcName">Name of the service.</param> /// <param name="svcDispName">Display name of the service</param> /// <param name="svcPath">The service file path.</param> /// <param name="description">The service description.</param> /// <param name="username">The username to run the service.</param> /// <param name="password">The password of the user running the service.</param> /// <param name="svcType">Type of the service.</param> /// <param name="errHandle">The error handle type.</param> /// <param name="svcStartMode">The service start mode.</param> /// <param name="interactWithDesktop">if set to true service can interact with desktop.</param> /// <param name="loadOrderGroup">The load order group.</param> /// <param name="loadOrderGroupDependencies">The load order group dependencies.</param> /// <param name="svcDependencies">Any service dependencies.</param> /// <returns><see cref="WMI.ReturnValue"/></returns> public ReturnValue InstallService(string svcName, string svcDispName, string svcPath, string description, string username = null, string password = null, ServiceType svcType = ServiceType.OwnProcess, OnError errHandle = OnError.UserIsNotified, StartMode svcStartMode = StartMode.Auto, bool interactWithDesktop = false, string loadOrderGroup = null, string[] loadOrderGroupDependencies = null, string[] svcDependencies = null) { var service = new ServiceInfo { Name = svcName, DisplayName = svcDispName, Description = description, PathName = svcPath, ServiceType = svcType, ErrorHandle = errHandle, StartMode = svcStartMode, InteractWithDesktop = interactWithDesktop, LoadOrderGroup = loadOrderGroup, LoadOrderGroupDependencies = loadOrderGroupDependencies, Dependencies = svcDependencies, Username = ComputerManager.EnsureDomain(username), Password = password }; return InstallService(service); }