From 30f70ee772e70278290ba5c7e6a4bb46a59fd184 Mon Sep 17 00:00:00 2001 From: reabr <91016230+reabr@users.noreply.github.com> Date: Tue, 31 Mar 2026 21:52:55 +0330 Subject: [PATCH] Add verbose message to `Get-Service` when properties cannot be returned (#27109) --- .../commands/management/Service.cs | 36 +++++++++++- .../resources/ServiceResources.resx | 57 ++++++++++--------- 2 files changed, 65 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs index 739baa15ba..a00f9583d6 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs @@ -674,13 +674,30 @@ namespace Microsoft.PowerShell.Commands #endregion Overrides #nullable enable + + /// + /// Writes a verbose message when a service property query fails. + /// + /// Name of the service. + /// Name of the property that failed to be queried. + private void WriteServicePropertyError(string serviceName, string propertyName) + { + Win32Exception e = new(Marshal.GetLastWin32Error()); + WriteVerbose( + StringUtil.Format( + ServiceResources.CouldNotGetServiceProperty, + serviceName, + propertyName, + e.Message)); + } + /// /// Adds UserName, Description, BinaryPathName, DelayedAutoStart and StartupType to a ServiceController object. /// /// Handle to the local SCManager instance. /// /// ServiceController as PSObject with UserName, Description and StartupType added. - private static PSObject AddProperties(nint scManagerHandle, ServiceController service) + private PSObject AddProperties(nint scManagerHandle, ServiceController service) { NakedWin32Handle hService = nint.Zero; @@ -709,6 +726,10 @@ namespace Microsoft.PowerShell.Commands { description = descriptionInfo.lpDescription; } + else + { + WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DESCRIPTIONW)); + } if (NativeMethods.QueryServiceConfig2( hService, @@ -717,6 +738,10 @@ namespace Microsoft.PowerShell.Commands { isDelayedAutoStart = autostartInfo.fDelayedAutostart; } + else + { + WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO)); + } if (NativeMethods.QueryServiceConfig( hService, @@ -731,6 +756,15 @@ namespace Microsoft.PowerShell.Commands isDelayedAutoStart.Value); } } + else + { + WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.QUERY_SERVICE_CONFIG)); + } + } + else + { + // handle when OpenServiceW itself fails: + WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_QUERY_CONFIG)); } } finally diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/ServiceResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/ServiceResources.resx index e1213c3fb0..c19f753529 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/ServiceResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/ServiceResources.resx @@ -1,17 +1,17 @@ - @@ -213,4 +213,7 @@ The startup type '{0}' is not supported by {1}. + + Could not retrieve property '{1}' for service '{0}': {2} +