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}
+