Skip to content

Get-Service Ignore common errors #24245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2025
Merged

Conversation

jborean93
Copy link
Collaborator

@jborean93 jborean93 commented Sep 4, 2024

PR Summary

Ignore common errors when attempting to retrieve some properties for a service like the Description, UserName, StartupType. These properties are not critical for using a service object or to retrieve other information like the name and whether a service exists or not and by emitting an error record it could fail a script if it did not expect an error.

Instead if the service failed to retrieve the property due to permissions or other environmental problems, it will set the affected properties to $null.

PR Context

The more common cause was doing Get-Service to enumerate all services or to get a particular service where the description points to a dll to extract the MUI entry. If that dll didn't exist or was set to an invalid index it would fail to get the description and then fail with a generic permission error message from PowerShell. Another less common problem was if the caller didn't have the SERVICE_QUERY_CONFIG access to retrieve the extra info requested.

As per the conversation in #10371 (comment) the WG choice was to not emit an error and set the properties to $null.

Fixes: #10371

PR Checklist

@jborean93
Copy link
Collaborator Author

I’m not the biggest fan of throwing away errors as it makes it harder for people to figure out why a value is not set or not what they expect. I understand using an error is not ideal and a warning might be too noisy as well but could we potentially emit the errors as:

  • a verbose/debug message, or
  • an information record with a specific tag

The latter option could potentially turn into a common pattern used by other cmdlets for similar scenarios.

iSazonov
iSazonov previously approved these changes Sep 5, 2024
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Instead of adding verbose/debug messages (which may be in too many numbers - several for each service, i.e. hundreds in total), we could simply add to the documentation that if the property is empty, then the reason is most likely the lack of rights.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Sep 5, 2024
@jborean93
Copy link
Collaborator Author

While I do agree that using the error or even warning stream will be too noisy for scenarios like this I do think that errors should not be ignored and there should be a way to view them for troubleshooting purposes. For example without actually stepping through the code it’s not apparent if the properties are empty due to a permission issue, missing MUI/environmental setting, or even incorrect code calls. Being able to give end users a way to trace that info to provide in issue reports would be quite invaluable, at least from a triage perspective.

I see that as the whole point of the debug or verbose streams. Especially since debug is no longer inquire by default I think we should be taking advantage of them to log more info. Even if we wanted to keep that clear for backwards compatibility I think there is some utility on using the information stream and a common tag like PSTRACE to log such events.

I don’t think that needs to be answered here but thought I should bring it up.


// As these are optional values, a failure due to permissions or
// other problem is ignored and the properties are set to null.
bool isDelayedAutoStart = false;
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to have the default value for isDelayedAutoStart to be null too? In case we failed to retrieve the actual value for this property, we probably should not use false as the default value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can make it nullable, it just means that we would need to decide how GetServiceStartupType treats the value as null. We could either

  • Treated as false
  • Treated the whole start type as invalid (or when Automatic)

The former is simpler but it may not be true if the service is truly automatic but delayed and we couldn't get the correct result.

https://github.com/PowerShell/PowerShell/blob/27e8cb3c9212d1d3e9a14198cbe822e7f7aec9dc/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs#L2915-L2931

Copy link
Member

@daxian-dbw daxian-dbw Sep 9, 2024

Choose a reason for hiding this comment

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

Given that startupType depends on the real value of autostartInfo.fDelayedAutostart, if we cannot get the real value of isDelayedAutoStart I think we may have to treat the startupType as invalid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Has been updated to keep it as InvalidValue (what it was if the config couldn't be retrieved) if we couldn't get the automatic delayed start value.

Copy link
Member

Choose a reason for hiding this comment

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

isDelayedAutoStart is not updated to have a null value when it cannot be retrieved.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@daxian-dbw
Copy link
Member

daxian-dbw commented Sep 9, 2024

I see that as the whole point of the debug or verbose streams. Especially since debug is no longer inquire by default I think we should be taking advantage of them to log more info. Even if we wanted to keep that clear for backwards compatibility I think there is some utility on using the information stream and a common tag like PSTRACE to log such events.

I agree we would want to capture the error messages in another stream. Would the debug/verbose stream be better as they are displayed with highlighted color? Information records are displayed in plain text color by default I guess. Also, we may not want to use 'PSTRACE' as the tag if going with the information record, because we have Disable/Enable-PSTrace commands from PSDiagnostics module and a user may be confused about what it is for.

Can you please open an issue for this?

@jborean93
Copy link
Collaborator Author

Can you please open an issue for this?

If I get time to do it I will but if I am being honest I mostly avoid opening issues here because they either don't get seen or do months ahead in time.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Sep 17, 2024
@SteveL-MSFT SteveL-MSFT added WG-Cmdlets general cmdlet issues WG-NeedsReview Needs a review by the labeled Working Group labels Sep 18, 2024
@SteveL-MSFT
Copy link
Member

@PowerShell/wg-powershell-cmdlets reviewed this and agree with the new behavior.

@SteveL-MSFT SteveL-MSFT added WG-Reviewed A Working Group has reviewed this and made a recommendation and removed WG-NeedsReview Needs a review by the labeled Working Group labels Sep 18, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Sep 18, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Sep 26, 2024
Ignore common errors when attempting to retrieve some properties for a
service like the Description, UserName, StartupType. These properties
are not critical for using a service object or to retrieve other
information like the name and whether a service exists or not and by
emitting an error record it could fail a script if it did not expect an
error.

Instead if the service failed to retrieve the property due to
permissions or other environmental problems, it will set the affected
properties to $null.
@iSazonov
Copy link
Collaborator

iSazonov commented Oct 4, 2024

@daxian-dbw Can you take another look?

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Oct 4, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Oct 11, 2024
@iSazonov iSazonov self-requested a review February 20, 2025 04:43
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

@jborean93 Sorry for my long delay :(. I'm happy to take over this PR if you are no longer interested in making changes.


// As these are optional values, a failure due to permissions or
// other problem is ignored and the properties are set to null.
bool isDelayedAutoStart = false;
Copy link
Member

Choose a reason for hiding this comment

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

isDelayedAutoStart is not updated to have a null value when it cannot be retrieved.

@microsoft-github-policy-service microsoft-github-policy-service bot added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Apr 1, 2025
@daxian-dbw daxian-dbw self-assigned this Apr 1, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Apr 1, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Apr 9, 2025
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

LGTM!

@daxian-dbw daxian-dbw merged commit 352fd38 into PowerShell:master Apr 21, 2025
35 checks passed
@github-project-automation github-project-automation bot moved this from PR In Progress to Done in PowerShell Team Reviews/Investigations Apr 21, 2025
@daxian-dbw daxian-dbw removed the Review - Needed The PR is being reviewed label Apr 21, 2025
@jborean93 jborean93 deleted the service-error branch April 21, 2025 21:20
@jborean93
Copy link
Collaborator Author

Thanks for the review and merge @daxian-dbw

Sysoiev-Yurii pushed a commit to Sysoiev-Yurii/PowerShell that referenced this pull request May 12, 2025
@eabase
Copy link

eabase commented Jun 30, 2025

Still an issue!
#20188

@jborean93
Copy link
Collaborator Author

@daxian-dbw can you add this to the 7.4 and 7.5 backport process? I think it’s a bugfix that should meet the backport requirements so people don’t have to wait for 7.6.

@eabase
Copy link

eabase commented Jul 1, 2025

@jborean93

I'm not a Windows OS coder, so it's not clear to me if this will fix the errors, or just hide them? I.e. will Get-Service truly show all the kernel drivers?

@jborean93
Copy link
Collaborator Author

It'll hide the errors like Windows PowerShell did when retrieving service info for services that cannot do things like resolve the resource names. It does not change any logic when it comes to drivers, they still need to be queried manually.

So after this change

Get-Service | Where-Object {$_.ServiceType -eq "KernelDriver"}

Will no longer show the error records just like it was in Windows PowerShell 5.1 but it will not show the kernel drivers as Get-Service only returns those service types if you explicitly request them by name.

@jborean93
Copy link
Collaborator Author

See this screenshot, top is 7.5.2, middle is this repo built with all the current changes (including this PR fix), and the bottom is 5.1

image

All 3 do not enumerate KernelDriver service types if you just did Get-Service. But now instead of showing the unrelated error records for unrelated services it doesn't do that anymore bringing the behaviour back to how 5.1 works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log WG-Cmdlets general cmdlet issues WG-Reviewed A Working Group has reviewed this and made a recommendation
Projects
Development

Successfully merging this pull request may close these issues.

Some drivers throw an exception when fetched with Get-Service, but still return the object anyway
5 participants