[release/v7.6.1] Add verbose message to Get-Service when properties cannot be returned#27250
Conversation
There was a problem hiding this comment.
Pull request overview
Backport to release/v7.6.1 that adds a new verbose message for Get-Service when optional service properties cannot be retrieved (permissions, missing resources, etc.), without changing default output.
Changes:
- Added a new localized resource string (
CouldNotGetServiceProperty) for verbose messaging. - Updated
GetServiceCommandto emit verbose messages when native service configuration queries fail. - Refactored
AddPropertiesto be instance-based to allow callingWriteVerbose.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/Microsoft.PowerShell.Commands.Management/resources/ServiceResources.resx |
Adds the new verbose message resource string (plus some unrelated whitespace-only reformatting). |
src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs |
Emits -Verbose messages when querying service description/delayed autostart/config fails. |
| else | ||
| { | ||
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DESCRIPTIONW)); | ||
| } |
There was a problem hiding this comment.
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.
| } | ||
| else | ||
| { | ||
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO)); |
There was a problem hiding this comment.
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.
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO)); | |
| WriteServicePropertyError(service.ServiceName, "DelayedAutoStart"); |
| } | ||
| else | ||
| { | ||
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.QUERY_SERVICE_CONFIG)); |
There was a problem hiding this comment.
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.
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.QUERY_SERVICE_CONFIG)); | |
| WriteServicePropertyError(service.ServiceName, "BinaryPathName"); | |
| WriteServicePropertyError(service.ServiceName, "UserName"); | |
| WriteServicePropertyError(service.ServiceName, "StartupType"); |
| else | ||
| { | ||
| // handle when OpenServiceW itself fails: | ||
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_QUERY_CONFIG)); |
There was a problem hiding this comment.
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.
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_QUERY_CONFIG)); | |
| WriteServicePropertyError(service.ServiceName, "OpenService"); |
| /// </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) |
There was a problem hiding this comment.
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.
| private void WriteServicePropertyError(string serviceName, string propertyName) | |
| private void WriteServicePropertyVerbose(string serviceName, string propertyName) |
| <!-- | ||
| 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 |
There was a problem hiding this comment.
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.
Backport of #27109 to release/v7.6.1
Triggered by @daxian-dbw on behalf of @reabr
Original CL Label: CL-General
/cc @PowerShell/powershell-maintainers
Impact
REQUIRED: Choose either Tooling Impact or Customer Impact (or both). At least one checkbox must be selected.
Tooling Impact
Customer Impact
Fixes #24272 – adds verbose messages to
Get-Servicewhen service properties (Description, DelayedAutoStart, BinaryPathName, StartupType, UserName) cannot be retrieved due to permissions or other issues. Only visible when using-Verbose, so it is non-intrusive by default.Regression
REQUIRED: Check exactly one box.
This is not a regression.
Testing
Verified locally that verbose messages appear for services with missing MUI resources (e.g. NPSMSvc, WaaSMedicSvc) when running
Get-Service -Verbose. Backport applies cleanly with no conflicts.Risk
REQUIRED: Check exactly one box.
Only adds verbose output when
-Verboseis explicitly used; standardGet-Servicebehavior is completely unchanged.