-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Conversation
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:
The latter option could potentially turn into a common pattern used by other cmdlets for similar scenarios. |
There was a problem hiding this 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.
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 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs
Show resolved
Hide resolved
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 Can you please open an issue for this? |
27e8cb3
to
68133f5
Compare
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. |
@PowerShell/wg-powershell-cmdlets reviewed this and agree with the new behavior. |
68133f5
to
3a5b829
Compare
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.
3a5b829
to
2687612
Compare
@daxian-dbw Can you take another look? |
There was a problem hiding this 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.
src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs
Outdated
Show resolved
Hide resolved
|
||
// 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; |
There was a problem hiding this comment.
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.
src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thanks for the review and merge @daxian-dbw |
…ical properties for a service (PowerShell#24245)
Still an issue! |
@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. |
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 |
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 |
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 theSERVICE_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
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.- [ ] Issue filed:
(which runs in a different PS Host).