| |
| |
|
|
| #if !UNIX // Not built on Unix |
|
|
| using System; |
| using System.Collections.Generic; |
| using System.ComponentModel; |
| using System.Diagnostics.CodeAnalysis; |
| using System.Management.Automation; |
| using System.Management.Automation.Internal; |
| using System.Runtime.InteropServices; |
| using System.Runtime.Serialization; |
| using System.Security.AccessControl; |
| using System.ServiceProcess; |
| using Dbg = System.Management.Automation.Diagnostics; |
| using DWORD = System.UInt32; |
| using NakedWin32Handle = System.IntPtr; |
|
|
| namespace Microsoft.PowerShell.Commands |
| { |
| #region ServiceBaseCommand |
|
|
| |
| |
| |
| public abstract class ServiceBaseCommand : Cmdlet |
| { |
| #region Internal |
|
|
| |
| |
| |
| |
| |
| protected bool ShouldProcessServiceOperation(ServiceController service) |
| { |
| return ShouldProcessServiceOperation( |
| service.DisplayName, |
| service.ServiceName); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected bool ShouldProcessServiceOperation( |
| string displayName, string serviceName) |
| { |
| string name = StringUtil.Format(ServiceResources.ServiceNameForConfirmation, |
| displayName, |
| serviceName); |
|
|
| return ShouldProcess(name); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| internal void WriteNonTerminatingError( |
| ServiceController service, |
| Exception innerException, |
| string errorId, |
| string errorMessage, |
| ErrorCategory category) |
| { |
| WriteNonTerminatingError( |
| service.ServiceName, |
| service.DisplayName, |
| service, |
| innerException, |
| errorId, |
| errorMessage, |
| category); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal void WriteNonTerminatingError( |
| string serviceName, |
| string displayName, |
| object targetObject, |
| Exception innerException, |
| string errorId, |
| string errorMessage, |
| ErrorCategory category) |
| { |
| string message = StringUtil.Format(errorMessage, |
| serviceName, |
| displayName, |
| (innerException == null) ? category.ToString() : innerException.Message); |
|
|
| var exception = new ServiceCommandException(message, innerException); |
| exception.ServiceName = serviceName; |
|
|
| WriteError(new ErrorRecord(exception, errorId, category, targetObject)); |
| } |
|
|
| internal void SetServiceSecurityDescriptor( |
| ServiceController service, |
| string securityDescriptorSddl, |
| NakedWin32Handle hService) |
| { |
| var rawSecurityDescriptor = new RawSecurityDescriptor(securityDescriptorSddl); |
| RawAcl rawDiscretionaryAcl = rawSecurityDescriptor.DiscretionaryAcl; |
| var discretionaryAcl = new DiscretionaryAcl(false, false, rawDiscretionaryAcl); |
|
|
| byte[] rawDacl = new byte[discretionaryAcl.BinaryLength]; |
| discretionaryAcl.GetBinaryForm(rawDacl, 0); |
| rawSecurityDescriptor.DiscretionaryAcl = new RawAcl(rawDacl, 0); |
| byte[] securityDescriptorByte = new byte[rawSecurityDescriptor.BinaryLength]; |
| rawSecurityDescriptor.GetBinaryForm(securityDescriptorByte, 0); |
|
|
| bool status = NativeMethods.SetServiceObjectSecurity( |
| hService, |
| SecurityInfos.DiscretionaryAcl, |
| securityDescriptorByte); |
|
|
| if (!status) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| bool accessDenied = exception.NativeErrorCode == NativeMethods.ERROR_ACCESS_DENIED; |
| WriteNonTerminatingError( |
| service, |
| exception, |
| nameof(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl), |
| StringUtil.Format(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl, service.ServiceName, exception.Message), |
| accessDenied ? ErrorCategory.PermissionDenied : ErrorCategory.InvalidOperation); |
| } |
| } |
| #endregion Internal |
| } |
| #endregion ServiceBaseCommand |
|
|
| #region MultipleServiceCommandBase |
|
|
| |
| |
| |
| |
| public abstract class MultipleServiceCommandBase : ServiceBaseCommand |
| { |
| #region Parameters |
|
|
| |
| |
| |
| internal enum SelectionMode |
| { |
| |
| |
| |
| Default = 0, |
| |
| |
| |
| DisplayName = 1, |
| |
| |
| |
| InputObject = 2, |
| |
| |
| |
| ServiceName = 3 |
| } |
| |
| |
| |
| internal SelectionMode selectionMode; |
|
|
| |
| |
| |
| |
| internal string[] serviceNames = null; |
|
|
| |
| |
| |
| [Parameter(ParameterSetName = "DisplayName", Mandatory = true)] |
| public string[] DisplayName |
| { |
| get |
| { |
| return displayNames; |
| } |
|
|
| set |
| { |
| displayNames = value; |
| selectionMode = SelectionMode.DisplayName; |
| } |
| } |
|
|
| internal string[] displayNames = null; |
|
|
| |
| |
| |
| |
| |
| |
| [Parameter] |
| [ValidateNotNullOrEmpty] |
| public string[] Include |
| { |
| get |
| { |
| return include; |
| } |
|
|
| set |
| { |
| include = value; |
| } |
| } |
|
|
| internal string[] include = null; |
|
|
| |
| |
| |
| |
| |
| |
| [Parameter] |
| [ValidateNotNullOrEmpty] |
| public string[] Exclude |
| { |
| get |
| { |
| return exclude; |
| } |
|
|
| set |
| { |
| exclude = value; |
| } |
| } |
|
|
| internal string[] exclude = null; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [Parameter(ParameterSetName = "InputObject", ValueFromPipeline = true)] |
| [ValidateNotNullOrEmpty] |
| [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] |
| public ServiceController[] InputObject |
| { |
| get |
| { |
| return _inputObject; |
| } |
|
|
| set |
| { |
| _inputObject = value; |
| selectionMode = SelectionMode.InputObject; |
| } |
| } |
|
|
| private ServiceController[] _inputObject = null; |
| #endregion Parameters |
|
|
| #region Internal |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal ServiceController[] AllServices => _allServices ??= ServiceController.GetServices(); |
|
|
| private ServiceController[] _allServices; |
|
|
| internal ServiceController GetOneService(string nameOfService) |
| { |
| Dbg.Assert(!WildcardPattern.ContainsWildcardCharacters(nameOfService), "Caller should verify that nameOfService doesn't contain wildcard characters"); |
|
|
| try |
| { |
| var sc = new ServiceController(nameOfService); |
| |
| var unused = sc.Status; |
|
|
| |
| return sc; |
| } |
| catch (InvalidOperationException) { } |
| catch (ArgumentException) { } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| internal List<ServiceController> MatchingServices() |
| { |
| List<ServiceController> matchingServices; |
| switch (selectionMode) |
| { |
| case SelectionMode.DisplayName: |
| matchingServices = MatchingServicesByDisplayName(); |
| break; |
| case SelectionMode.InputObject: |
| matchingServices = MatchingServicesByInput(); |
| break; |
| default: |
| matchingServices = MatchingServicesByServiceName(); |
| break; |
| } |
| |
| |
| matchingServices.Sort(ServiceComparison); |
| return matchingServices; |
| } |
|
|
| |
| private static int ServiceComparison(ServiceController x, ServiceController y) |
| { |
| return string.Compare(x.ServiceName, y.ServiceName, StringComparison.OrdinalIgnoreCase); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private List<ServiceController> MatchingServicesByServiceName() |
| { |
| List<ServiceController> matchingServices = new(); |
|
|
| if (serviceNames == null) |
| { |
| foreach (ServiceController service in AllServices) |
| { |
| IncludeExcludeAdd(matchingServices, service, false); |
| } |
|
|
| return matchingServices; |
| } |
|
|
| foreach (string pattern in serviceNames) |
| { |
| bool found = false; |
|
|
| if (WildcardPattern.ContainsWildcardCharacters(pattern)) |
| { |
| WildcardPattern wildcard = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase); |
| foreach (ServiceController service in AllServices) |
| { |
| if (!wildcard.IsMatch(service.ServiceName)) |
| continue; |
| found = true; |
| IncludeExcludeAdd(matchingServices, service, true); |
| } |
| } |
| else |
| { |
| ServiceController service = GetOneService(pattern); |
| if (service != null) |
| { |
| found = true; |
| IncludeExcludeAdd(matchingServices, service, true); |
| } |
| } |
|
|
| if (!found && !WildcardPattern.ContainsWildcardCharacters(pattern)) |
| { |
| WriteNonTerminatingError( |
| pattern, |
| string.Empty, |
| pattern, |
| null, |
| "NoServiceFoundForGivenName", |
| ServiceResources.NoServiceFoundForGivenName, |
| ErrorCategory.ObjectNotFound); |
| } |
| } |
|
|
| return matchingServices; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private List<ServiceController> MatchingServicesByDisplayName() |
| { |
| List<ServiceController> matchingServices = new(); |
| if (DisplayName == null) |
| { |
| Diagnostics.Assert(false, "null DisplayName"); |
| throw PSTraceSource.NewInvalidOperationException(); |
| } |
|
|
| foreach (string pattern in DisplayName) |
| { |
| WildcardPattern wildcard = |
| WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase); |
| bool found = false; |
| foreach (ServiceController service in AllServices) |
| { |
| if (!wildcard.IsMatch(service.DisplayName)) |
| continue; |
| found = true; |
| IncludeExcludeAdd(matchingServices, service, true); |
| } |
|
|
| if (!found && !WildcardPattern.ContainsWildcardCharacters(pattern)) |
| { |
| WriteNonTerminatingError( |
| string.Empty, |
| pattern, |
| pattern, |
| null, |
| "NoServiceFoundForGivenDisplayName", |
| ServiceResources.NoServiceFoundForGivenDisplayName, |
| ErrorCategory.ObjectNotFound); |
| } |
| } |
|
|
| return matchingServices; |
| } |
|
|
| |
| |
| |
| |
| |
| private List<ServiceController> MatchingServicesByInput() |
| { |
| List<ServiceController> matchingServices = new(); |
| if (InputObject == null) |
| { |
| Diagnostics.Assert(false, "null InputObject"); |
| throw PSTraceSource.NewInvalidOperationException(); |
| } |
|
|
| foreach (ServiceController service in InputObject) |
| { |
| service.Refresh(); |
| IncludeExcludeAdd(matchingServices, service, false); |
| } |
|
|
| return matchingServices; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private void IncludeExcludeAdd( |
| List<ServiceController> list, |
| ServiceController service, |
| bool checkDuplicates) |
| { |
| if (include != null && !Matches(service, include)) |
| return; |
| if (exclude != null && Matches(service, exclude)) |
| return; |
| if (checkDuplicates) |
| { |
| foreach (ServiceController sc in list) |
| { |
| if (sc.ServiceName == service.ServiceName && |
| sc.MachineName == service.MachineName) |
| { |
| return; |
| } |
| } |
| } |
|
|
| list.Add(service); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private bool Matches(ServiceController service, string[] matchList) |
| { |
| if (matchList == null) |
| throw PSTraceSource.NewArgumentNullException(nameof(matchList)); |
| string serviceID = (selectionMode == SelectionMode.DisplayName) |
| ? service.DisplayName |
| : service.ServiceName; |
| foreach (string pattern in matchList) |
| { |
| WildcardPattern wildcard = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase); |
| if (wildcard.IsMatch(serviceID)) |
| return true; |
| } |
|
|
| return false; |
| } |
| #endregion Internal |
|
|
| } |
| #endregion MultipleServiceCommandBase |
|
|
| #region GetServiceCommand |
| |
| |
| |
| [Cmdlet(VerbsCommon.Get, "Service", DefaultParameterSetName = "Default", |
| HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096496", RemotingCapability = RemotingCapability.SupportedByCommand)] |
| [OutputType(typeof(ServiceController))] |
| public sealed class GetServiceCommand : MultipleServiceCommandBase |
| { |
| #region Parameters |
| |
| |
| |
| |
| |
| |
| |
| [Parameter(Position = 0, ParameterSetName = "Default", ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)] |
| [ValidateNotNullOrEmpty()] |
| [Alias("ServiceName")] |
| public string[] Name |
| { |
| get |
| { |
| return serviceNames; |
| } |
|
|
| set |
| { |
| serviceNames = value; |
| selectionMode = SelectionMode.ServiceName; |
| } |
| } |
|
|
| |
| |
| |
| [Parameter] |
| [Alias("DS")] |
| public SwitchParameter DependentServices { get; set; } |
|
|
| |
| |
| |
| [Parameter] |
| [Alias("SDO", "ServicesDependedOn")] |
| public SwitchParameter RequiredServices { get; set; } |
|
|
| #endregion Parameters |
|
|
| #region Overrides |
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| nint scManagerHandle = nint.Zero; |
| if (!DependentServices && !RequiredServices) |
| { |
| |
| |
| scManagerHandle = NativeMethods.OpenSCManagerW( |
| lpMachineName: null, |
| lpDatabaseName: null, |
| dwDesiredAccess: NativeMethods.SC_MANAGER_CONNECT); |
| if (scManagerHandle == nint.Zero) |
| { |
| Win32Exception exception = new(); |
| string message = StringUtil.Format(ServiceResources.FailToOpenServiceControlManager, exception.Message); |
| ServiceCommandException serviceException = new ServiceCommandException(message, exception); |
| ErrorRecord err = new ErrorRecord( |
| serviceException, |
| "FailToOpenServiceControlManager", |
| ErrorCategory.PermissionDenied, |
| null); |
| ThrowTerminatingError(err); |
| } |
| } |
|
|
| try |
| { |
| foreach (ServiceController service in MatchingServices()) |
| { |
| if (!DependentServices.IsPresent && !RequiredServices.IsPresent) |
| { |
| WriteObject(AddProperties(scManagerHandle, service)); |
| } |
| else |
| { |
| if (DependentServices.IsPresent) |
| { |
| foreach (ServiceController dependantserv in service.DependentServices) |
| { |
| WriteObject(dependantserv); |
| } |
| } |
|
|
| if (RequiredServices.IsPresent) |
| { |
| foreach (ServiceController servicedependedon in service.ServicesDependedOn) |
| { |
| WriteObject(servicedependedon); |
| } |
| } |
| } |
| } |
| } |
| finally |
| { |
| if (scManagerHandle != nint.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(scManagerHandle); |
| Diagnostics.Assert(succeeded, "SCManager handle close failed"); |
| } |
| } |
| } |
|
|
| #endregion Overrides |
|
|
| #nullable enable |
|
|
| |
| |
| |
| |
| |
| private void WriteServicePropertyError(string serviceName, string propertyName) |
| { |
| Win32Exception e = new(Marshal.GetLastWin32Error()); |
| WriteVerbose( |
| StringUtil.Format( |
| ServiceResources.CouldNotGetServiceProperty, |
| serviceName, |
| propertyName, |
| e.Message)); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private PSObject AddProperties(nint scManagerHandle, ServiceController service) |
| { |
| NakedWin32Handle hService = nint.Zero; |
|
|
| |
| |
| bool? isDelayedAutoStart = null; |
| string? binPath = null; |
| string? description = null; |
| string? startName = null; |
| ServiceStartupType startupType = ServiceStartupType.InvalidValue; |
| try |
| { |
| |
| |
| hService = NativeMethods.OpenServiceW( |
| scManagerHandle, |
| service.ServiceName, |
| NativeMethods.SERVICE_QUERY_CONFIG |
| ); |
| if (hService != nint.Zero) |
| { |
| if (NativeMethods.QueryServiceConfig2( |
| hService, |
| NativeMethods.SERVICE_CONFIG_DESCRIPTION, |
| out NativeMethods.SERVICE_DESCRIPTIONW descriptionInfo)) |
| { |
| description = descriptionInfo.lpDescription; |
| } |
| else |
| { |
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DESCRIPTIONW)); |
| } |
|
|
| if (NativeMethods.QueryServiceConfig2( |
| hService, |
| NativeMethods.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, |
| out NativeMethods.SERVICE_DELAYED_AUTO_START_INFO autostartInfo)) |
| { |
| isDelayedAutoStart = autostartInfo.fDelayedAutostart; |
| } |
| else |
| { |
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO)); |
| } |
|
|
| if (NativeMethods.QueryServiceConfig( |
| hService, |
| out NativeMethods.QUERY_SERVICE_CONFIG serviceInfo)) |
| { |
| binPath = serviceInfo.lpBinaryPathName; |
| startName = serviceInfo.lpServiceStartName; |
| if (isDelayedAutoStart.HasValue) |
| { |
| startupType = NativeMethods.GetServiceStartupType( |
| (ServiceStartMode)serviceInfo.dwStartType, |
| isDelayedAutoStart.Value); |
| } |
| } |
| else |
| { |
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.QUERY_SERVICE_CONFIG)); |
| } |
| } |
| else |
| { |
| |
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_QUERY_CONFIG)); |
| } |
| } |
| finally |
| { |
| if (hService != IntPtr.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(hService); |
| Diagnostics.Assert(succeeded, "Failed to close service handle"); |
| } |
| } |
|
|
| PSObject serviceAsPSObj = PSObject.AsPSObject(service); |
| PSNoteProperty noteProperty = new("UserName", startName); |
| serviceAsPSObj.Properties.Add(noteProperty, true); |
| serviceAsPSObj.TypeNames.Insert(0, "System.Service.ServiceController#UserName"); |
|
|
| noteProperty = new PSNoteProperty("Description", description); |
| serviceAsPSObj.Properties.Add(noteProperty, true); |
| serviceAsPSObj.TypeNames.Insert(0, "System.Service.ServiceController#Description"); |
|
|
| noteProperty = new PSNoteProperty("DelayedAutoStart", isDelayedAutoStart); |
| serviceAsPSObj.Properties.Add(noteProperty, true); |
| serviceAsPSObj.TypeNames.Insert(0, "System.Service.ServiceController#DelayedAutoStart"); |
|
|
| noteProperty = new PSNoteProperty("BinaryPathName", binPath); |
| serviceAsPSObj.Properties.Add(noteProperty, true); |
| serviceAsPSObj.TypeNames.Insert(0, "System.Service.ServiceController#BinaryPathName"); |
|
|
| noteProperty = new PSNoteProperty("StartupType", startupType); |
| serviceAsPSObj.Properties.Add(noteProperty, true); |
| serviceAsPSObj.TypeNames.Insert(0, "System.Service.ServiceController#StartupType"); |
|
|
| return serviceAsPSObj; |
| } |
| } |
| #nullable disable |
| #endregion GetServiceCommand |
|
|
| #region ServiceOperationBaseCommand |
| |
| |
| |
| |
| public abstract class ServiceOperationBaseCommand : MultipleServiceCommandBase |
| { |
| #region Parameters |
| |
| |
| |
| |
| |
| |
| |
| [Parameter(Position = 0, ParameterSetName = "Default", Mandatory = true, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)] |
| [Alias("ServiceName")] |
| public string[] Name |
| { |
| get |
| { |
| return serviceNames; |
| } |
|
|
| set |
| { |
| serviceNames = value; |
| selectionMode = SelectionMode.ServiceName; |
| } |
| } |
|
|
| |
| |
| |
| [Parameter(Position = 0, Mandatory = true, ParameterSetName = "InputObject", ValueFromPipeline = true)] |
| [ValidateNotNullOrEmpty] |
| [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] |
| public new ServiceController[] InputObject |
| { |
| get |
| { |
| return base.InputObject; |
| } |
|
|
| set |
| { |
| base.InputObject = value; |
| } |
| } |
|
|
| |
| |
| |
| |
| [Parameter] |
| public SwitchParameter PassThru { get; set; } |
|
|
| #endregion Parameters |
|
|
| #region Internal |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal bool DoWaitForStatus( |
| ServiceController serviceController, |
| ServiceControllerStatus targetStatus, |
| ServiceControllerStatus pendingStatus, |
| string resourceIdPending, |
| string errorId, |
| string errorMessage) |
| { |
| while (true) |
| { |
| try |
| { |
| |
| |
| serviceController.WaitForStatus( |
| targetStatus, |
| new TimeSpan(20000000) |
| ); |
| return true; |
| } |
| catch (System.ServiceProcess.TimeoutException) |
| { |
| if (serviceController.Status != pendingStatus |
| |
| |
| |
| && serviceController.Status != targetStatus) |
| { |
| WriteNonTerminatingError(serviceController, null, |
| errorId, |
| errorMessage, |
| ErrorCategory.OpenError); |
| return false; |
| } |
|
|
| string message = StringUtil.Format(resourceIdPending, |
| serviceController.ServiceName, |
| serviceController.DisplayName |
| ); |
| |
| WriteWarning(message); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| internal bool DoStartService(ServiceController serviceController) |
| { |
| Exception exception = null; |
| try |
| { |
| serviceController.Start(); |
| } |
| catch (Win32Exception e) |
| { |
| if (e.NativeErrorCode != NativeMethods.ERROR_SERVICE_ALREADY_RUNNING) |
| exception = e; |
| } |
| catch (InvalidOperationException e) |
| { |
| if (e.InnerException is not Win32Exception eInner |
| || eInner.NativeErrorCode != NativeMethods.ERROR_SERVICE_ALREADY_RUNNING) |
| { |
| exception = e; |
| } |
| } |
|
|
| if (exception != null) |
| { |
| |
| |
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotStartService", |
| ServiceResources.CouldNotStartService, |
| ErrorCategory.OpenError); |
| return false; |
| } |
|
|
| |
| |
| if (!DoWaitForStatus( |
| serviceController, |
| ServiceControllerStatus.Running, |
| ServiceControllerStatus.StartPending, |
| ServiceResources.StartingService, |
| "StartServiceFailed", |
| ServiceResources.StartServiceFailed)) |
| { |
| return false; |
| } |
|
|
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal List<ServiceController> DoStopService(ServiceController serviceController, bool force, bool waitForServiceToStop) |
| { |
| |
| |
|
|
| List<ServiceController> stoppedServices = new(); |
| ServiceController[] dependentServices = null; |
|
|
| try |
| { |
| dependentServices = serviceController.DependentServices; |
| } |
| catch (Win32Exception e) |
| { |
| WriteNonTerminatingError(serviceController, e, |
| "CouldNotAccessDependentServices", |
| ServiceResources.CouldNotAccessDependentServices, |
| ErrorCategory.InvalidOperation); |
| } |
| catch (InvalidOperationException e) |
| { |
| WriteNonTerminatingError(serviceController, e, |
| "CouldNotAccessDependentServices", |
| ServiceResources.CouldNotAccessDependentServices, |
| ErrorCategory.InvalidOperation); |
| } |
|
|
| if (!force) |
| { |
| if ((dependentServices != null) |
| && (dependentServices.Length > 0)) |
| { |
| |
| if (!HaveAllDependentServicesStopped(dependentServices)) |
| { |
| |
| |
| |
| WriteNonTerminatingError(serviceController, |
| null, |
| "ServiceHasDependentServices", |
| ServiceResources.ServiceHasDependentServices, |
| ErrorCategory.InvalidOperation); |
|
|
| return stoppedServices; |
| } |
| } |
| } |
|
|
| if (dependentServices != null) |
| { |
| foreach (ServiceController service in dependentServices) |
| { |
| if ((service.Status == ServiceControllerStatus.Running || |
| service.Status == ServiceControllerStatus.StartPending) && |
| service.CanStop) |
| { |
| stoppedServices.Add(service); |
| } |
| } |
| } |
|
|
| Exception exception = null; |
| try |
| { |
| serviceController.Stop(); |
| } |
| catch (Win32Exception e) |
| { |
| if (e.NativeErrorCode != NativeMethods.ERROR_SERVICE_NOT_ACTIVE) |
| exception = e; |
| } |
| catch (InvalidOperationException e) |
| { |
| if (e.InnerException is not Win32Exception eInner |
| || eInner.NativeErrorCode != NativeMethods.ERROR_SERVICE_NOT_ACTIVE) |
| { |
| exception = e; |
| } |
| } |
|
|
| if (exception != null) |
| { |
| |
| |
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotStopService", |
| ServiceResources.CouldNotStopService, |
| ErrorCategory.CloseError); |
| RemoveNotStoppedServices(stoppedServices); |
| return stoppedServices; |
| } |
|
|
| |
| |
| if (waitForServiceToStop) |
| { |
| if (!DoWaitForStatus( |
| serviceController, |
| ServiceControllerStatus.Stopped, |
| ServiceControllerStatus.StopPending, |
| ServiceResources.StoppingService, |
| "StopServiceFailed", |
| ServiceResources.StopServiceFailed)) |
| { |
| RemoveNotStoppedServices(stoppedServices); |
| return stoppedServices; |
| } |
| } |
|
|
| RemoveNotStoppedServices(stoppedServices); |
| if ((serviceController.Status.Equals(ServiceControllerStatus.Stopped)) || (serviceController.Status.Equals(ServiceControllerStatus.StopPending))) |
| { |
| stoppedServices.Add(serviceController); |
| } |
|
|
| return stoppedServices; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private static bool HaveAllDependentServicesStopped(ServiceController[] dependentServices) |
| { |
| return Array.TrueForAll(dependentServices, static service => service.Status == ServiceControllerStatus.Stopped); |
| } |
|
|
| |
| |
| |
| |
| internal void RemoveNotStoppedServices(List<ServiceController> services) |
| { |
| |
| services.RemoveAll(service => |
| service.Status != ServiceControllerStatus.Stopped && |
| service.Status != ServiceControllerStatus.StopPending); |
| } |
|
|
| |
| |
| |
| |
| |
| internal bool DoPauseService(ServiceController serviceController) |
| { |
| Exception exception = null; |
| bool serviceNotRunning = false; |
| try |
| { |
| serviceController.Pause(); |
| } |
| catch (Win32Exception e) |
| { |
| if (e.NativeErrorCode == NativeMethods.ERROR_SERVICE_NOT_ACTIVE) |
| { |
| serviceNotRunning = true; |
| } |
|
|
| exception = e; |
| } |
| catch (InvalidOperationException e) |
| { |
| if (e.InnerException is Win32Exception eInner |
| && eInner.NativeErrorCode == NativeMethods.ERROR_SERVICE_NOT_ACTIVE) |
| { |
| serviceNotRunning = true; |
| } |
|
|
| exception = e; |
| } |
|
|
| if (exception != null) |
| { |
| |
| |
| string resourceIdAndErrorId = ServiceResources.CouldNotSuspendService; |
| if (serviceNotRunning) |
| { |
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotSuspendServiceNotRunning", |
| ServiceResources.CouldNotSuspendServiceNotRunning, |
| ErrorCategory.CloseError); |
| } |
| else if (!serviceController.CanPauseAndContinue) |
| { |
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotSuspendServiceNotSupported", |
| ServiceResources.CouldNotSuspendServiceNotSupported, |
| ErrorCategory.CloseError); |
| } |
|
|
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotSuspendService", |
| ServiceResources.CouldNotSuspendService, |
| ErrorCategory.CloseError); |
|
|
| return false; |
| } |
|
|
| |
| |
| if (!DoWaitForStatus( |
| serviceController, |
| ServiceControllerStatus.Paused, |
| ServiceControllerStatus.PausePending, |
| ServiceResources.SuspendingService, |
| "SuspendServiceFailed", |
| ServiceResources.SuspendServiceFailed)) |
| { |
| return false; |
| } |
|
|
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| internal bool DoResumeService(ServiceController serviceController) |
| { |
| Exception exception = null; |
| bool serviceNotRunning = false; |
| try |
| { |
| serviceController.Continue(); |
| } |
| catch (Win32Exception e) |
| { |
| if (e.NativeErrorCode == NativeMethods.ERROR_SERVICE_NOT_ACTIVE) |
| { |
| serviceNotRunning = true; |
| } |
|
|
| exception = e; |
| } |
| catch (InvalidOperationException e) |
| { |
| if (e.InnerException is Win32Exception eInner |
| && eInner.NativeErrorCode == NativeMethods.ERROR_SERVICE_NOT_ACTIVE) |
| { |
| serviceNotRunning = true; |
| } |
|
|
| exception = e; |
| } |
|
|
| if (exception != null) |
| { |
| |
| |
| if (serviceNotRunning) |
| { |
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotResumeServiceNotRunning", |
| ServiceResources.CouldNotResumeServiceNotRunning, |
| ErrorCategory.CloseError); |
| } |
| else if (!serviceController.CanPauseAndContinue) |
| { |
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotResumeServiceNotSupported", |
| ServiceResources.CouldNotResumeServiceNotSupported, |
| ErrorCategory.CloseError); |
| } |
|
|
| WriteNonTerminatingError(serviceController, |
| exception, |
| "CouldNotResumeService", |
| ServiceResources.CouldNotResumeService, |
| ErrorCategory.CloseError); |
|
|
| return false; |
| } |
|
|
| |
| |
| if (!DoWaitForStatus( |
| serviceController, |
| ServiceControllerStatus.Running, |
| ServiceControllerStatus.ContinuePending, |
| ServiceResources.ResumingService, |
| "ResumeServiceFailed", |
| ServiceResources.ResumeServiceFailed)) |
| { |
| return false; |
| } |
|
|
| return true; |
| } |
| #endregion Internal |
| } |
| #endregion ServiceOperationBaseCommand |
|
|
| #region StopServiceCommand |
|
|
| |
| |
| |
| |
| |
| |
| |
| [Cmdlet(VerbsLifecycle.Stop, "Service", DefaultParameterSetName = "InputObject", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097052")] |
| [OutputType(typeof(ServiceController))] |
| public sealed class StopServiceCommand : ServiceOperationBaseCommand |
| { |
| |
| |
| |
| |
| [Parameter] |
| public SwitchParameter Force { get; set; } |
|
|
| |
| |
| |
| [Parameter] |
| public SwitchParameter NoWait { get; set; } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| foreach (ServiceController serviceController in MatchingServices()) |
| { |
| |
| |
| if (!ShouldProcessServiceOperation(serviceController)) |
| { |
| continue; |
| } |
|
|
| List<ServiceController> stoppedServices = DoStopService(serviceController, Force, !NoWait); |
|
|
| if (PassThru && stoppedServices.Count > 0) |
| { |
| foreach (ServiceController service in stoppedServices) |
| { |
| WriteObject(service); |
| } |
| } |
| } |
| } |
| } |
| #endregion StopServiceCommand |
|
|
| #region StartServiceCommand |
| |
| |
| |
| [Cmdlet(VerbsLifecycle.Start, "Service", DefaultParameterSetName = "InputObject", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097051")] |
| [OutputType(typeof(ServiceController))] |
| public sealed class StartServiceCommand : ServiceOperationBaseCommand |
| { |
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| foreach (ServiceController serviceController in MatchingServices()) |
| { |
| |
| |
| if (!ShouldProcessServiceOperation(serviceController)) |
| { |
| continue; |
| } |
|
|
| if (DoStartService(serviceController)) |
| { |
| if (PassThru) |
| WriteObject(serviceController); |
| } |
| } |
| } |
| } |
| #endregion StartServiceCommand |
|
|
| #region SuspendServiceCommand |
| |
| |
| |
| [Cmdlet(VerbsLifecycle.Suspend, "Service", DefaultParameterSetName = "InputObject", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097053")] |
| [OutputType(typeof(ServiceController))] |
| public sealed class SuspendServiceCommand : ServiceOperationBaseCommand |
| { |
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| foreach (ServiceController serviceController in MatchingServices()) |
| { |
| |
| |
| if (!ShouldProcessServiceOperation(serviceController)) |
| { |
| continue; |
| } |
|
|
| if (DoPauseService(serviceController)) |
| { |
| if (PassThru) |
| WriteObject(serviceController); |
| } |
| } |
| } |
| } |
| #endregion SuspendServiceCommand |
|
|
| #region ResumeServiceCommand |
| |
| |
| |
| [Cmdlet(VerbsLifecycle.Resume, "Service", DefaultParameterSetName = "InputObject", SupportsShouldProcess = true, |
| HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097150")] |
| [OutputType(typeof(ServiceController))] |
| public sealed class ResumeServiceCommand : ServiceOperationBaseCommand |
| { |
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| foreach (ServiceController serviceController in MatchingServices()) |
| { |
| |
| |
| if (!ShouldProcessServiceOperation(serviceController)) |
| { |
| continue; |
| } |
|
|
| if (DoResumeService(serviceController)) |
| { |
| if (PassThru) |
| WriteObject(serviceController); |
| } |
| } |
| } |
| } |
| #endregion ResumeServiceCommand |
|
|
| #region RestartServiceCommand |
|
|
| |
| |
| |
| [Cmdlet(VerbsLifecycle.Restart, "Service", DefaultParameterSetName = "InputObject", SupportsShouldProcess = true, |
| HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097059")] |
| [OutputType(typeof(ServiceController))] |
| public sealed class RestartServiceCommand : ServiceOperationBaseCommand |
| { |
| |
| |
| |
| |
| [Parameter] |
| public SwitchParameter Force { get; set; } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| foreach (ServiceController serviceController in MatchingServices()) |
| { |
| |
| |
| if (!ShouldProcessServiceOperation(serviceController)) |
| { |
| continue; |
| } |
|
|
| |
| List<ServiceController> stoppedServices = DoStopService(serviceController, Force, true); |
|
|
| if (stoppedServices.Count > 0) |
| { |
| foreach (ServiceController service in stoppedServices) |
| { |
| if (DoStartService(service)) |
| { |
| if (PassThru) |
| WriteObject(service); |
| } |
| } |
| } |
| } |
| } |
| } |
| #endregion RestartServiceCommand |
|
|
| #region SetServiceCommand |
|
|
| |
| |
| |
| [Cmdlet(VerbsCommon.Set, "Service", SupportsShouldProcess = true, DefaultParameterSetName = "Name", |
| HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097148", RemotingCapability = RemotingCapability.SupportedByCommand)] |
| [OutputType(typeof(ServiceController))] |
| public class SetServiceCommand : ServiceOperationBaseCommand |
| { |
| #region Parameters |
|
|
| |
| |
| |
| |
| [Parameter(Mandatory = true, ParameterSetName = "Name", Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] |
| [Alias("ServiceName", "SN")] |
| public new string Name |
| { |
| get |
| { |
| return serviceName; |
| } |
|
|
| set |
| { |
| serviceName = value; |
| } |
| } |
|
|
| internal string serviceName = null; |
|
|
| |
| |
| |
| |
| |
| |
| [Parameter(Mandatory = true, ParameterSetName = "InputObject", Position = 0, ValueFromPipeline = true)] |
| public new ServiceController InputObject { get; set; } |
|
|
| |
| |
| |
| |
| [Parameter] |
| [ValidateNotNullOrEmpty] |
| [Alias("DN")] |
| public new string DisplayName |
| { |
| get |
| { |
| return displayName; |
| } |
|
|
| set |
| { |
| displayName = value; |
| } |
| } |
|
|
| internal string displayName = null; |
|
|
| |
| |
| |
| |
| [Parameter] |
| [Credential()] |
| public PSCredential Credential { get; set; } |
|
|
| |
| |
| |
| |
| |
| |
| |
| [Parameter] |
| [ValidateNotNullOrEmpty] |
| public string Description |
| { |
| get |
| { |
| return description; |
| } |
|
|
| set |
| { |
| description = value; |
| } |
| } |
|
|
| internal string description = null; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| [Parameter] |
| [Alias("StartMode", "SM", "ST", "StartType")] |
| [ValidateNotNullOrEmpty] |
| public ServiceStartupType StartupType |
| { |
| get |
| { |
| return startupType; |
| } |
|
|
| set |
| { |
| startupType = value; |
| } |
| } |
|
|
| |
| |
| internal ServiceStartupType startupType = ServiceStartupType.InvalidValue; |
|
|
| |
| |
| |
| [Parameter] |
| [Alias("sd")] |
| [ValidateNotNullOrEmpty] |
| public string SecurityDescriptorSddl |
| { |
| get; |
| set; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| [Parameter] |
| [ValidateSetAttribute(new string[] { "Running", "Stopped", "Paused" })] |
| public string Status |
| { |
| get |
| { |
| return serviceStatus; |
| } |
|
|
| set |
| { |
| serviceStatus = value; |
| } |
| } |
|
|
| internal string serviceStatus = null; |
|
|
| |
| |
| |
| |
| |
| |
| [Parameter] |
| public SwitchParameter Force { get; set; } |
|
|
| |
| |
| |
| |
| [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] |
| public new string[] Include |
| { |
| get |
| { |
| return include; |
| } |
|
|
| set |
| { |
| include = null; |
| } |
| } |
|
|
| internal new string[] include = null; |
|
|
| |
| |
| |
| |
| [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] |
| public new string[] Exclude |
| { |
| get |
| { |
| return exclude; |
| } |
|
|
| set |
| { |
| exclude = null; |
| } |
| } |
|
|
| internal new string[] exclude = null; |
| #endregion Parameters |
|
|
| #region Overrides |
| |
| |
| protected override void ProcessRecord() |
| { |
| ServiceController service = null; |
| IntPtr password = IntPtr.Zero; |
| bool objServiceShouldBeDisposed = false; |
|
|
| try |
| { |
| if (InputObject != null) |
| { |
| service = InputObject; |
| Name = service.ServiceName; |
| objServiceShouldBeDisposed = false; |
| } |
| else |
| { |
| service = new ServiceController(serviceName); |
| objServiceShouldBeDisposed = true; |
| } |
|
|
| Diagnostics.Assert(!string.IsNullOrEmpty(Name), "null ServiceName"); |
|
|
| |
| |
| |
| string unusedByDesign = service.DisplayName; |
| } |
| catch (ArgumentException ex) |
| { |
| |
| ErrorRecord er = new(ex, "ArgumentException", ErrorCategory.ObjectNotFound, Name); |
| WriteError(er); |
| return; |
| } |
| catch (InvalidOperationException ex) |
| { |
| |
| ErrorRecord er = new(ex, "InvalidOperationException", ErrorCategory.ObjectNotFound, Name); |
| WriteError(er); |
| return; |
| } |
|
|
| try |
| { |
| |
| |
| if (!ShouldProcessServiceOperation(service)) |
| { |
| return; |
| } |
|
|
| NakedWin32Handle hScManager = IntPtr.Zero; |
| NakedWin32Handle hService = IntPtr.Zero; |
| IntPtr delayedAutoStartInfoBuffer = IntPtr.Zero; |
| try |
| { |
| hScManager = NativeMethods.OpenSCManagerW( |
| string.Empty, |
| null, |
| NativeMethods.SC_MANAGER_CONNECT |
| ); |
|
|
| if (hScManager == IntPtr.Zero) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "FailToOpenServiceControlManager", |
| ServiceResources.FailToOpenServiceControlManager, |
| ErrorCategory.PermissionDenied); |
| return; |
| } |
|
|
| var access = NativeMethods.SERVICE_CHANGE_CONFIG; |
| if (!string.IsNullOrEmpty(SecurityDescriptorSddl)) |
| access |= NativeMethods.WRITE_DAC | NativeMethods.WRITE_OWNER; |
|
|
| hService = NativeMethods.OpenServiceW( |
| hScManager, |
| Name, |
| access |
| ); |
|
|
| if (hService == IntPtr.Zero) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "CouldNotSetService", |
| ServiceResources.CouldNotSetService, |
| ErrorCategory.PermissionDenied); |
| return; |
| } |
| |
| if (!string.IsNullOrEmpty(DisplayName) |
| || StartupType != ServiceStartupType.InvalidValue || Credential != null) |
| { |
| DWORD dwStartType = NativeMethods.SERVICE_NO_CHANGE; |
| if (!NativeMethods.TryGetNativeStartupType(StartupType, out dwStartType)) |
| { |
| WriteNonTerminatingError(StartupType.ToString(), "Set-Service", Name, |
| new ArgumentException(), "CouldNotSetService", |
| ServiceResources.UnsupportedStartupType, |
| ErrorCategory.InvalidArgument); |
| return; |
| } |
|
|
| string username = null; |
| if (Credential != null) |
| { |
| username = Credential.UserName; |
| password = Marshal.SecureStringToCoTaskMemUnicode(Credential.Password); |
| } |
|
|
| bool succeeded = NativeMethods.ChangeServiceConfigW( |
| hService, |
| NativeMethods.SERVICE_NO_CHANGE, |
| dwStartType, |
| NativeMethods.SERVICE_NO_CHANGE, |
| null, |
| null, |
| IntPtr.Zero, |
| null, |
| username, |
| password, |
| DisplayName |
| ); |
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "CouldNotSetService", |
| ServiceResources.CouldNotSetService, |
| ErrorCategory.PermissionDenied); |
| return; |
| } |
| } |
|
|
| NativeMethods.SERVICE_DESCRIPTIONW sd = new(); |
| sd.lpDescription = Description; |
| int size = Marshal.SizeOf(sd); |
| IntPtr buffer = Marshal.AllocCoTaskMem(size); |
| Marshal.StructureToPtr(sd, buffer, false); |
|
|
| bool status = NativeMethods.ChangeServiceConfig2W( |
| hService, |
| NativeMethods.SERVICE_CONFIG_DESCRIPTION, |
| buffer); |
|
|
| if (!status) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "CouldNotSetServiceDescription", |
| ServiceResources.CouldNotSetServiceDescription, |
| ErrorCategory.PermissionDenied); |
| } |
|
|
| |
| NativeMethods.SERVICE_DELAYED_AUTO_START_INFO ds = new(); |
| ds.fDelayedAutostart = StartupType == ServiceStartupType.AutomaticDelayedStart; |
| size = Marshal.SizeOf(ds); |
| delayedAutoStartInfoBuffer = Marshal.AllocCoTaskMem(size); |
| Marshal.StructureToPtr(ds, delayedAutoStartInfoBuffer, false); |
|
|
| status = NativeMethods.ChangeServiceConfig2W( |
| hService, |
| NativeMethods.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, |
| delayedAutoStartInfoBuffer); |
|
|
| if (!status) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| Name, |
| DisplayName, |
| Name, |
| exception, |
| "CouldNotSetServiceDelayedAutoStart", |
| ServiceResources.CouldNotSetServiceDelayedAutoStart, |
| ErrorCategory.PermissionDenied); |
| } |
|
|
| |
| if (!string.IsNullOrEmpty(Status)) |
| { |
| if (Status.Equals("Running", StringComparison.OrdinalIgnoreCase)) |
| { |
| if (!service.Status.Equals(ServiceControllerStatus.Running)) |
| { |
| if (service.Status.Equals(ServiceControllerStatus.Paused)) |
| |
| DoResumeService(service); |
| else |
| |
| DoStartService(service); |
| } |
| } |
| else if (Status.Equals("Stopped", StringComparison.OrdinalIgnoreCase)) |
| { |
| if (!service.Status.Equals(ServiceControllerStatus.Stopped)) |
| { |
| |
| ServiceController[] dependentServices = service.DependentServices; |
|
|
| if ((!Force) && (dependentServices != null) && (dependentServices.Length > 0)) |
| { |
| WriteNonTerminatingError(service, null, "ServiceHasDependentServicesNoForce", ServiceResources.ServiceHasDependentServicesNoForce, ErrorCategory.InvalidOperation); |
| return; |
| } |
|
|
| |
| DoStopService(service, Force, waitForServiceToStop: true); |
| } |
| } |
| else if (Status.Equals("Paused", StringComparison.OrdinalIgnoreCase)) |
| { |
| if (!service.Status.Equals(ServiceControllerStatus.Paused)) |
| { |
| DoPauseService(service); |
| } |
| } |
| } |
|
|
| if (!string.IsNullOrEmpty(SecurityDescriptorSddl)) |
| { |
| SetServiceSecurityDescriptor(service, SecurityDescriptorSddl, hService); |
| } |
|
|
| if (PassThru.IsPresent) |
| { |
| |
| ServiceController displayservice = new(Name); |
| WriteObject(displayservice); |
| } |
| } |
| finally |
| { |
| if (delayedAutoStartInfoBuffer != IntPtr.Zero) |
| { |
| Marshal.FreeCoTaskMem(delayedAutoStartInfoBuffer); |
| } |
|
|
| if (hService != IntPtr.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(hService); |
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "CouldNotSetServiceDescription", |
| ServiceResources.CouldNotSetServiceDescription, |
| ErrorCategory.PermissionDenied); |
| } |
| } |
|
|
| if (hScManager != IntPtr.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(hScManager); |
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "CouldNotSetServiceDescription", |
| ServiceResources.CouldNotSetServiceDescription, |
| ErrorCategory.PermissionDenied); |
| } |
| } |
| } |
| } |
| finally |
| { |
| if (password != IntPtr.Zero) |
| { |
| Marshal.ZeroFreeCoTaskMemUnicode(password); |
| } |
|
|
| if (objServiceShouldBeDisposed) |
| { |
| service.Dispose(); |
| } |
| } |
| } |
| #endregion Overrides |
|
|
| } |
| #endregion SetServiceCommand |
|
|
| #region NewServiceCommand |
| |
| |
| |
| [Cmdlet(VerbsCommon.New, "Service", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096905")] |
| [OutputType(typeof(ServiceController))] |
| public class NewServiceCommand : ServiceBaseCommand |
| { |
| #region Parameters |
| |
| |
| |
| |
| [Parameter(Position = 0, Mandatory = true)] |
| [Alias("ServiceName")] |
| public string Name |
| { |
| get { return serviceName; } |
|
|
| set { serviceName = value; } |
| } |
|
|
| internal string serviceName = null; |
|
|
| |
| |
| |
| |
| [Parameter(Position = 1, Mandatory = true)] |
| [Alias("Path")] |
| public string BinaryPathName |
| { |
| get { return binaryPathName; } |
|
|
| set { binaryPathName = value; } |
| } |
|
|
| internal string binaryPathName = null; |
|
|
| |
| |
| |
| |
| [Parameter] |
| [ValidateNotNullOrEmpty] |
| public string DisplayName |
| { |
| get { return displayName; } |
|
|
| set { displayName = value; } |
| } |
|
|
| internal string displayName = null; |
|
|
| |
| |
| |
| |
| [Parameter] |
| [ValidateNotNullOrEmpty] |
| public string Description |
| { |
| get { return description; } |
|
|
| set { description = value; } |
| } |
|
|
| internal string description = null; |
|
|
| |
| |
| |
| |
| [Parameter] |
| public ServiceStartupType StartupType |
| { |
| get { return startupType; } |
|
|
| set { startupType = value; } |
| } |
|
|
| internal ServiceStartupType startupType = ServiceStartupType.Automatic; |
|
|
| |
| |
| |
| |
| [Parameter] |
| [Credential()] |
| public PSCredential Credential |
| { |
| get { return credential; } |
|
|
| set { credential = value; } |
| } |
|
|
| internal PSCredential credential = null; |
|
|
| |
| |
| |
| [Parameter] |
| [Alias("sd")] |
| [ValidateNotNullOrEmpty] |
| public string SecurityDescriptorSddl |
| { |
| get; |
| set; |
| } |
|
|
| |
| |
| |
| |
| [Parameter] |
| public string[] DependsOn |
| { |
| get { return dependsOn; } |
|
|
| set { dependsOn = value; } |
| } |
|
|
| internal string[] dependsOn = null; |
| #endregion Parameters |
|
|
| #region Overrides |
| |
| |
| |
| protected override void BeginProcessing() |
| { |
| ServiceController service = null; |
| Diagnostics.Assert(!string.IsNullOrEmpty(Name), |
| "null ServiceName"); |
| Diagnostics.Assert(!string.IsNullOrEmpty(BinaryPathName), |
| "null BinaryPathName"); |
|
|
| |
| |
| if (!ShouldProcessServiceOperation(DisplayName ?? string.Empty, Name)) |
| { |
| return; |
| } |
|
|
| |
| NakedWin32Handle hScManager = IntPtr.Zero; |
| NakedWin32Handle hService = IntPtr.Zero; |
| IntPtr password = IntPtr.Zero; |
| IntPtr delayedAutoStartInfoBuffer = IntPtr.Zero; |
| try |
| { |
| hScManager = NativeMethods.OpenSCManagerW( |
| null, |
| null, |
| NativeMethods.SC_MANAGER_CONNECT | NativeMethods.SC_MANAGER_CREATE_SERVICE |
| ); |
| if (hScManager == IntPtr.Zero) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| Name, |
| DisplayName, |
| Name, |
| exception, |
| "CouldNotNewService", |
| ServiceResources.CouldNotNewService, |
| ErrorCategory.PermissionDenied); |
| return; |
| } |
|
|
| if (!NativeMethods.TryGetNativeStartupType(StartupType, out DWORD dwStartType)) |
| { |
| WriteNonTerminatingError(StartupType.ToString(), "New-Service", Name, |
| new ArgumentException(), "CouldNotNewService", |
| ServiceResources.UnsupportedStartupType, |
| ErrorCategory.InvalidArgument); |
| return; |
| } |
| |
| IntPtr lpDependencies = IntPtr.Zero; |
| if (DependsOn != null) |
| { |
| int numchars = 1; |
| foreach (string dependedOn in DependsOn) |
| { |
| numchars += dependedOn.Length + 1; |
| } |
|
|
| char[] doubleNullArray = new char[numchars]; |
| int pos = 0; |
| foreach (string dependedOn in DependsOn) |
| { |
| Array.Copy( |
| dependedOn.ToCharArray(), 0, |
| doubleNullArray, pos, |
| dependedOn.Length |
| ); |
| pos += dependedOn.Length; |
| doubleNullArray[pos++] = (char)0; |
| } |
|
|
| doubleNullArray[pos++] = (char)0; |
| Diagnostics.Assert(pos == numchars, "lpDependencies build error"); |
| lpDependencies = Marshal.AllocHGlobal( |
| numchars * Marshal.SystemDefaultCharSize); |
| Marshal.Copy(doubleNullArray, 0, lpDependencies, numchars); |
| } |
|
|
| |
| string username = null; |
| if (Credential != null) |
| { |
| username = Credential.UserName; |
| password = Marshal.SecureStringToCoTaskMemUnicode(Credential.Password); |
| } |
|
|
| |
| hService = NativeMethods.CreateServiceW( |
| hScManager, |
| Name, |
| DisplayName, |
| NativeMethods.SERVICE_CHANGE_CONFIG | NativeMethods.WRITE_DAC | NativeMethods.WRITE_OWNER, |
| NativeMethods.SERVICE_WIN32_OWN_PROCESS, |
| dwStartType, |
| NativeMethods.SERVICE_ERROR_NORMAL, |
| BinaryPathName, |
| null, |
| null, |
| lpDependencies, |
| username, |
| password |
| ); |
| if (hService == IntPtr.Zero) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| Name, |
| DisplayName, |
| Name, |
| exception, |
| "CouldNotNewService", |
| ServiceResources.CouldNotNewService, |
| ErrorCategory.PermissionDenied); |
| return; |
| } |
|
|
| |
| NativeMethods.SERVICE_DESCRIPTIONW sd = new(); |
| sd.lpDescription = Description; |
| int size = Marshal.SizeOf(sd); |
| IntPtr buffer = Marshal.AllocCoTaskMem(size); |
| Marshal.StructureToPtr(sd, buffer, false); |
|
|
| bool succeeded = NativeMethods.ChangeServiceConfig2W( |
| hService, |
| NativeMethods.SERVICE_CONFIG_DESCRIPTION, |
| buffer); |
|
|
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| Name, |
| DisplayName, |
| Name, |
| exception, |
| "CouldNotNewServiceDescription", |
| ServiceResources.CouldNotNewServiceDescription, |
| ErrorCategory.PermissionDenied); |
| } |
|
|
| |
| if (StartupType == ServiceStartupType.AutomaticDelayedStart) |
| { |
| NativeMethods.SERVICE_DELAYED_AUTO_START_INFO ds = new(); |
| ds.fDelayedAutostart = true; |
| size = Marshal.SizeOf(ds); |
| delayedAutoStartInfoBuffer = Marshal.AllocCoTaskMem(size); |
| Marshal.StructureToPtr(ds, delayedAutoStartInfoBuffer, false); |
|
|
| succeeded = NativeMethods.ChangeServiceConfig2W( |
| hService, |
| NativeMethods.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, |
| delayedAutoStartInfoBuffer); |
|
|
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| Name, |
| DisplayName, |
| Name, |
| exception, |
| "CouldNotNewServiceDelayedAutoStart", |
| ServiceResources.CouldNotNewServiceDelayedAutoStart, |
| ErrorCategory.PermissionDenied); |
| } |
| } |
|
|
| |
| service = new ServiceController(Name); |
|
|
| if (!string.IsNullOrEmpty(SecurityDescriptorSddl)) |
| { |
| SetServiceSecurityDescriptor(service, SecurityDescriptorSddl, hService); |
| } |
|
|
| WriteObject(service); |
| } |
| finally |
| { |
| if (delayedAutoStartInfoBuffer != IntPtr.Zero) |
| { |
| Marshal.FreeCoTaskMem(delayedAutoStartInfoBuffer); |
| } |
|
|
| if (password != IntPtr.Zero) |
| { |
| Marshal.ZeroFreeCoTaskMemUnicode(password); |
| } |
|
|
| if (hService != IntPtr.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(hService); |
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| Name, |
| DisplayName, |
| Name, |
| exception, |
| "CouldNotNewServiceDescription", |
| ServiceResources.CouldNotNewServiceDescription, |
| ErrorCategory.PermissionDenied); |
| } |
| } |
|
|
| if (hScManager != IntPtr.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(hScManager); |
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| Name, |
| DisplayName, |
| Name, |
| exception, |
| "CouldNotNewServiceDescription", |
| ServiceResources.CouldNotNewServiceDescription, |
| ErrorCategory.PermissionDenied); |
| } |
| } |
| } |
| } |
| #endregion Overrides |
| } |
| #endregion NewServiceCommand |
|
|
| #region RemoveServiceCommand |
| |
| |
| |
| [Cmdlet(VerbsCommon.Remove, "Service", SupportsShouldProcess = true, DefaultParameterSetName = "Name", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2248980")] |
| public class RemoveServiceCommand : ServiceBaseCommand |
| { |
| #region Parameters |
|
|
| |
| |
| |
| [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Name")] |
| [Alias("ServiceName", "SN")] |
| public string Name { get; set; } |
|
|
| |
| |
| |
| |
| |
| |
| [Parameter(ValueFromPipeline = true, ParameterSetName = "InputObject")] |
| public ServiceController InputObject { get; set; } |
|
|
| #endregion Parameters |
|
|
| #region Overrides |
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| ServiceController service = null; |
| bool objServiceShouldBeDisposed = false; |
| try |
| { |
| if (InputObject != null) |
| { |
| service = InputObject; |
| Name = service.ServiceName; |
| objServiceShouldBeDisposed = false; |
| } |
| else |
| { |
| service = new ServiceController(Name); |
| objServiceShouldBeDisposed = true; |
| } |
|
|
| Diagnostics.Assert(!string.IsNullOrEmpty(Name), "null ServiceName"); |
|
|
| |
| |
| string unusedByDesign = service.DisplayName; |
| } |
| catch (ArgumentException ex) |
| { |
| |
| ErrorRecord er = new(ex, "ArgumentException", ErrorCategory.ObjectNotFound, Name); |
| WriteError(er); |
| return; |
| } |
| catch (InvalidOperationException ex) |
| { |
| |
| ErrorRecord er = new(ex, "InvalidOperationException", ErrorCategory.ObjectNotFound, Name); |
| WriteError(er); |
| return; |
| } |
|
|
| try |
| { |
| |
| |
| if (!ShouldProcessServiceOperation(service)) |
| { |
| return; |
| } |
|
|
| NakedWin32Handle hScManager = IntPtr.Zero; |
| NakedWin32Handle hService = IntPtr.Zero; |
| try |
| { |
| hScManager = NativeMethods.OpenSCManagerW( |
| lpMachineName: string.Empty, |
| lpDatabaseName: null, |
| dwDesiredAccess: NativeMethods.SC_MANAGER_ALL_ACCESS |
| ); |
| if (hScManager == IntPtr.Zero) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteObject(exception); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "FailToOpenServiceControlManager", |
| ServiceResources.FailToOpenServiceControlManager, |
| ErrorCategory.PermissionDenied); |
| return; |
| } |
|
|
| hService = NativeMethods.OpenServiceW( |
| hScManager, |
| Name, |
| NativeMethods.SERVICE_DELETE |
| ); |
| if (hService == IntPtr.Zero) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "CouldNotRemoveService", |
| ServiceResources.CouldNotRemoveService, |
| ErrorCategory.PermissionDenied); |
| return; |
| } |
|
|
| bool status = NativeMethods.DeleteService(hService); |
|
|
| if (!status) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Win32Exception exception = new(lastError); |
| WriteNonTerminatingError( |
| service, |
| exception, |
| "CouldNotRemoveService", |
| ServiceResources.CouldNotRemoveService, |
| ErrorCategory.PermissionDenied); |
| } |
| } |
| finally |
| { |
| if (hService != IntPtr.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(hService); |
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Diagnostics.Assert(lastError != 0, "ErrorCode not success"); |
| } |
| } |
|
|
| if (hScManager != IntPtr.Zero) |
| { |
| bool succeeded = NativeMethods.CloseServiceHandle(hScManager); |
| if (!succeeded) |
| { |
| int lastError = Marshal.GetLastWin32Error(); |
| Diagnostics.Assert(lastError != 0, "ErrorCode not success"); |
| } |
| } |
| } |
| } |
| finally |
| { |
| if (objServiceShouldBeDisposed) |
| { |
| service.Dispose(); |
| } |
| } |
| } |
| #endregion Overrides |
| } |
| #endregion RemoveServiceCommand |
|
|
| #region ServiceCommandException |
| |
| |
| |
| public class ServiceCommandException : SystemException |
| { |
| #region ctors |
| |
| |
| |
| |
| public ServiceCommandException() |
| : base() |
| { |
| throw new NotImplementedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| public ServiceCommandException(string message) |
| : base(message) |
| { |
| } |
|
|
| |
| |
| |
| |
| |
| public ServiceCommandException(string message, Exception innerException) |
| : base(message, innerException) |
| { |
| } |
| #endregion ctors |
|
|
| #region Serialization |
| |
| |
| |
| |
| |
| |
| [Obsolete("Legacy serialization support is deprecated since .NET 8, hence this method is now marked as obsolete", DiagnosticId = "SYSLIB0051")] |
| protected ServiceCommandException(SerializationInfo info, StreamingContext context) |
| { |
| throw new NotSupportedException(); |
| } |
|
|
| #endregion Serialization |
|
|
| #region Properties |
| |
| |
| |
| |
| public string ServiceName |
| { |
| get { return _serviceName; } |
|
|
| set { _serviceName = value; } |
| } |
|
|
| private string _serviceName = string.Empty; |
| #endregion Properties |
| } |
| #endregion ServiceCommandException |
|
|
| #region NativeMethods |
| internal static class NativeMethods |
| { |
| |
| internal const int ERROR_SERVICE_ALREADY_RUNNING = 1056; |
| internal const int ERROR_SERVICE_NOT_ACTIVE = 1062; |
| internal const int ERROR_INSUFFICIENT_BUFFER = 122; |
| internal const DWORD ERROR_ACCESS_DENIED = 0x5; |
| internal const DWORD SC_MANAGER_CONNECT = 1; |
| internal const DWORD SC_MANAGER_CREATE_SERVICE = 2; |
| internal const DWORD SC_MANAGER_ALL_ACCESS = 0xf003f; |
| internal const DWORD SERVICE_QUERY_CONFIG = 1; |
| internal const DWORD SERVICE_CHANGE_CONFIG = 2; |
| internal const DWORD SERVICE_DELETE = 0x10000; |
| internal const DWORD SERVICE_NO_CHANGE = 0xffffffff; |
| internal const DWORD SERVICE_AUTO_START = 0x2; |
| internal const DWORD SERVICE_DEMAND_START = 0x3; |
| internal const DWORD SERVICE_DISABLED = 0x4; |
| internal const DWORD SERVICE_CONFIG_DESCRIPTION = 1; |
| internal const DWORD SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 3; |
| internal const DWORD SERVICE_CONFIG_SERVICE_SID_INFO = 5; |
| internal const DWORD WRITE_DAC = 262144; |
| internal const DWORD WRITE_OWNER = 524288; |
| internal const DWORD SERVICE_WIN32_OWN_PROCESS = 0x10; |
| internal const DWORD SERVICE_ERROR_NORMAL = 1; |
|
|
| |
| [DllImport(PinvokeDllNames.OpenSCManagerWDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| NakedWin32Handle OpenSCManagerW( |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpMachineName, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpDatabaseName, |
| DWORD dwDesiredAccess |
| ); |
|
|
| [DllImport(PinvokeDllNames.OpenServiceWDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| NakedWin32Handle OpenServiceW( |
| NakedWin32Handle hSCManager, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpServiceName, |
| DWORD dwDesiredAccess |
| ); |
|
|
| [DllImport(PinvokeDllNames.QueryServiceConfigDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| bool QueryServiceConfigW( |
| NakedWin32Handle hSCManager, |
| IntPtr lpServiceConfig, |
| DWORD cbBufSize, |
| out DWORD pcbBytesNeeded |
| ); |
|
|
| [DllImport(PinvokeDllNames.QueryServiceConfig2DllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| bool QueryServiceConfig2W( |
| NakedWin32Handle hService, |
| DWORD dwInfoLevel, |
| IntPtr lpBuffer, |
| DWORD cbBufSize, |
| out DWORD pcbBytesNeeded |
| ); |
|
|
| [DllImport(PinvokeDllNames.CloseServiceHandleDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| bool CloseServiceHandle( |
| NakedWin32Handle hSCManagerOrService |
| ); |
|
|
| [DllImport(PinvokeDllNames.DeleteServiceDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| bool DeleteService( |
| NakedWin32Handle hService |
| ); |
|
|
| [DllImport(PinvokeDllNames.ChangeServiceConfigWDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| bool ChangeServiceConfigW( |
| NakedWin32Handle hService, |
| DWORD dwServiceType, |
| DWORD dwStartType, |
| DWORD dwErrorControl, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpBinaryPathName, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpLoadOrderGroup, |
| IntPtr lpdwTagId, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpDependencies, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpServiceStartName, |
| [In] IntPtr lpPassword, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpDisplayName |
| ); |
|
|
| [DllImport(PinvokeDllNames.ChangeServiceConfig2WDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| bool ChangeServiceConfig2W( |
| NakedWin32Handle hService, |
| DWORD dwInfoLevel, |
| IntPtr lpInfo |
| ); |
|
|
| [StructLayout(LayoutKind.Sequential)] |
| internal struct SERVICE_DESCRIPTIONW |
| { |
| [MarshalAs(UnmanagedType.LPWStr)] |
| internal string lpDescription; |
| } |
|
|
| [StructLayout(LayoutKind.Sequential)] |
| internal struct QUERY_SERVICE_CONFIG |
| { |
| internal uint dwServiceType; |
| internal uint dwStartType; |
| internal uint dwErrorControl; |
| [MarshalAs(UnmanagedType.LPWStr)] internal string lpBinaryPathName; |
| [MarshalAs(UnmanagedType.LPWStr)] internal string lpLoadOrderGroup; |
| internal uint dwTagId; |
| [MarshalAs(UnmanagedType.LPWStr)] internal string lpDependencies; |
| [MarshalAs(UnmanagedType.LPWStr)] internal string lpServiceStartName; |
| [MarshalAs(UnmanagedType.LPWStr)] internal string lpDisplayName; |
| } |
|
|
| [StructLayout(LayoutKind.Sequential)] |
| internal struct SERVICE_DELAYED_AUTO_START_INFO |
| { |
| internal bool fDelayedAutostart; |
| } |
|
|
| [DllImport(PinvokeDllNames.CreateServiceWDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| internal static extern |
| NakedWin32Handle CreateServiceW( |
| NakedWin32Handle hSCManager, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpServiceName, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpDisplayName, |
| DWORD dwDesiredAccess, |
| DWORD dwServiceType, |
| DWORD dwStartType, |
| DWORD dwErrorControl, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpBinaryPathName, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpLoadOrderGroup, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpdwTagId, |
| [In] IntPtr lpDependencies, |
| [In, MarshalAs(UnmanagedType.LPWStr)] string lpServiceStartName, |
| [In] IntPtr lpPassword |
| ); |
|
|
| [DllImport(PinvokeDllNames.SetServiceObjectSecurityDllName, CharSet = CharSet.Unicode, SetLastError = true)] |
| [return: MarshalAs(UnmanagedType.Bool)] |
| internal static extern |
| bool SetServiceObjectSecurity( |
| NakedWin32Handle hSCManager, |
| System.Security.AccessControl.SecurityInfos dwSecurityInformation, |
| byte[] lpSecurityDescriptor |
| ); |
|
|
| internal static bool QueryServiceConfig(NakedWin32Handle hService, out NativeMethods.QUERY_SERVICE_CONFIG configStructure) |
| { |
| IntPtr lpBuffer = IntPtr.Zero; |
| configStructure = default(NativeMethods.QUERY_SERVICE_CONFIG); |
| DWORD bufferSize, bufferSizeNeeded = 0; |
| bool status = NativeMethods.QueryServiceConfigW( |
| hSCManager: hService, |
| lpServiceConfig: lpBuffer, |
| cbBufSize: 0, |
| pcbBytesNeeded: out bufferSizeNeeded); |
|
|
| if (!status && Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) |
| { |
| return status; |
| } |
|
|
| try |
| { |
| lpBuffer = Marshal.AllocCoTaskMem((int)bufferSizeNeeded); |
| bufferSize = bufferSizeNeeded; |
|
|
| status = NativeMethods.QueryServiceConfigW( |
| hService, |
| lpBuffer, |
| bufferSize, |
| out bufferSizeNeeded); |
| configStructure = (NativeMethods.QUERY_SERVICE_CONFIG)Marshal.PtrToStructure(lpBuffer, typeof(NativeMethods.QUERY_SERVICE_CONFIG)); |
| } |
| finally |
| { |
| Marshal.FreeCoTaskMem(lpBuffer); |
| } |
|
|
| return status; |
| } |
|
|
| internal static bool QueryServiceConfig2<T>(NakedWin32Handle hService, DWORD infolevel, out T configStructure) |
| { |
| IntPtr lpBuffer = IntPtr.Zero; |
| configStructure = default(T); |
| DWORD bufferSize, bufferSizeNeeded = 0; |
|
|
| bool status = NativeMethods.QueryServiceConfig2W( |
| hService: hService, |
| dwInfoLevel: infolevel, |
| lpBuffer: lpBuffer, |
| cbBufSize: 0, |
| pcbBytesNeeded: out bufferSizeNeeded); |
|
|
| if (!status && Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) |
| { |
| return status; |
| } |
|
|
| try |
| { |
| lpBuffer = Marshal.AllocCoTaskMem((int)bufferSizeNeeded); |
| bufferSize = bufferSizeNeeded; |
|
|
| status = NativeMethods.QueryServiceConfig2W( |
| hService, |
| infolevel, |
| lpBuffer, |
| bufferSize, |
| out bufferSizeNeeded); |
| configStructure = (T)Marshal.PtrToStructure(lpBuffer, typeof(T)); |
| } |
| finally |
| { |
| Marshal.FreeCoTaskMem(lpBuffer); |
| } |
|
|
| return status; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static bool TryGetNativeStartupType(ServiceStartupType StartupType, out DWORD dwStartType) |
| { |
| bool success = true; |
| dwStartType = NativeMethods.SERVICE_NO_CHANGE; |
| switch (StartupType) |
| { |
| case ServiceStartupType.Automatic: |
| case ServiceStartupType.AutomaticDelayedStart: |
| dwStartType = NativeMethods.SERVICE_AUTO_START; |
| break; |
| case ServiceStartupType.Manual: |
| dwStartType = NativeMethods.SERVICE_DEMAND_START; |
| break; |
| case ServiceStartupType.Disabled: |
| dwStartType = NativeMethods.SERVICE_DISABLED; |
| break; |
| case ServiceStartupType.InvalidValue: |
| dwStartType = NativeMethods.SERVICE_NO_CHANGE; |
| break; |
| default: |
| success = false; |
| break; |
| } |
|
|
| return success; |
| } |
|
|
| internal static ServiceStartupType GetServiceStartupType(ServiceStartMode startMode, bool delayedAutoStart) |
| { |
| ServiceStartupType result = ServiceStartupType.Disabled; |
| switch (startMode) |
| { |
| case ServiceStartMode.Automatic: |
| result = delayedAutoStart ? ServiceStartupType.AutomaticDelayedStart : ServiceStartupType.Automatic; |
| break; |
| case ServiceStartMode.Manual: |
| result = ServiceStartupType.Manual; |
| break; |
| case ServiceStartMode.Disabled: |
| result = ServiceStartupType.Disabled; |
| break; |
| } |
|
|
| return result; |
| } |
| } |
| #endregion NativeMethods |
|
|
| #region ServiceStartupType |
| |
| |
| |
| public enum ServiceStartupType |
| { |
| |
| InvalidValue = -1, |
| |
| Automatic = 2, |
| |
| Manual = 3, |
| |
| Disabled = 4, |
| |
| AutomaticDelayedStart = 10 |
| } |
| #endregion ServiceStartupType |
| } |
|
|
| #endif // Not built on Unix |
|
|