Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,30 @@ protected override void ProcessRecord()
#endregion Overrides

#nullable enable

/// <summary>
/// Writes a verbose message when a service property query fails.
/// </summary>
/// <param name="serviceName">Name of the service.</param>
/// <param name="propertyName">Name of the property that failed to be queried.</param>
private void WriteServicePropertyError(string serviceName, string propertyName)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper writes to the verbose stream, but the name WriteServicePropertyError suggests it writes an error record. Renaming it to reflect the verbose-only behavior (e.g., WriteServicePropertyVerbose/WriteCouldNotGetServicePropertyVerbose) would reduce confusion for future maintainers.

Suggested change
private void WriteServicePropertyError(string serviceName, string propertyName)
private void WriteServicePropertyVerbose(string serviceName, string propertyName)

Copilot uses AI. Check for mistakes.
{
Win32Exception e = new(Marshal.GetLastWin32Error());
WriteVerbose(
StringUtil.Format(
ServiceResources.CouldNotGetServiceProperty,
serviceName,
propertyName,
e.Message));
}

/// <summary>
/// Adds UserName, Description, BinaryPathName, DelayedAutoStart and StartupType to a ServiceController object.
/// </summary>
/// <param name="scManagerHandle">Handle to the local SCManager instance.</param>
/// <param name="service"></param>
/// <returns>ServiceController as PSObject with UserName, Description and StartupType added.</returns>
private static PSObject AddProperties(nint scManagerHandle, ServiceController service)
private PSObject AddProperties(nint scManagerHandle, ServiceController service)
{
NakedWin32Handle hService = nint.Zero;

Expand Down Expand Up @@ -709,6 +726,10 @@ private static PSObject AddProperties(nint scManagerHandle, ServiceController se
{
description = descriptionInfo.lpDescription;
}
else
{
WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DESCRIPTIONW));
}
Comment on lines +729 to +732
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The verbose message currently uses nameof(NativeMethods.SERVICE_DESCRIPTIONW) as the “property” name, which will surface a native struct identifier (e.g., SERVICE_DESCRIPTIONW) rather than the user-facing PowerShell property (Description). Consider passing the actual PS property name so Get-Service -Verbose output is actionable.

Copilot uses AI. Check for mistakes.

if (NativeMethods.QueryServiceConfig2(
hService,
Expand All @@ -717,6 +738,10 @@ private static PSObject AddProperties(nint scManagerHandle, ServiceController se
{
isDelayedAutoStart = autostartInfo.fDelayedAutostart;
}
else
{
WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO));
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here: using nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO) will emit a native type name in verbose output. Use the corresponding PowerShell property name (DelayedAutoStart) so the message matches what users see on the returned objects.

Suggested change
WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO));
WriteServicePropertyError(service.ServiceName, "DelayedAutoStart");

Copilot uses AI. Check for mistakes.
}

if (NativeMethods.QueryServiceConfig(
hService,
Expand All @@ -731,6 +756,15 @@ private static PSObject AddProperties(nint scManagerHandle, ServiceController se
isDelayedAutoStart.Value);
}
}
else
{
WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.QUERY_SERVICE_CONFIG));
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nameof(NativeMethods.QUERY_SERVICE_CONFIG) will show the native struct name in the verbose message, but the failing retrieval affects multiple PS properties (BinaryPathName, UserName, StartupType). Consider emitting the relevant PowerShell property names (potentially one message per derived property) so users know what data is missing.

Suggested change
WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.QUERY_SERVICE_CONFIG));
WriteServicePropertyError(service.ServiceName, "BinaryPathName");
WriteServicePropertyError(service.ServiceName, "UserName");
WriteServicePropertyError(service.ServiceName, "StartupType");

Copilot uses AI. Check for mistakes.
}
}
else
{
// handle when OpenServiceW itself fails:
WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_QUERY_CONFIG));
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On OpenServiceW failure, the “property” name is currently SERVICE_QUERY_CONFIG (an access right), which reads oddly in the verbose stream. Consider using a clearer label like OpenService/ServiceConfig or the affected PowerShell properties so the message communicates what could not be retrieved.

Suggested change
WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_QUERY_CONFIG));
WriteServicePropertyError(service.ServiceName, "OpenService");

Copilot uses AI. Check for mistakes.
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
Comment on lines +3 to +10
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hunk reformats the ResX schema comment block (including adding trailing whitespace). Since it’s unrelated to the new resource entry, consider reverting these whitespace-only changes to avoid noisy diffs and potential style/whitespace lint issues in future merges.

Copilot uses AI. Check for mistakes.
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -213,4 +213,7 @@
<data name="UnsupportedStartupType" xml:space="preserve">
<value>The startup type '{0}' is not supported by {1}.</value>
</data>
<data name="CouldNotGetServiceProperty" xml:space="preserve">
<value>Could not retrieve property '{1}' for service '{0}': {2}</value>
</data>
</root>
Loading