-
Notifications
You must be signed in to change notification settings - Fork 8.3k
[release/v7.6.1] Add verbose message to Get-Service when properties cannot be returned #27250
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||||||
| { | ||||||||||
| 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; | ||||||||||
|
|
||||||||||
|
|
@@ -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
|
||||||||||
|
|
||||||||||
| if (NativeMethods.QueryServiceConfig2( | ||||||||||
| hService, | ||||||||||
|
|
@@ -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)); | ||||||||||
|
||||||||||
| WriteServicePropertyError(service.ServiceName, nameof(NativeMethods.SERVICE_DELAYED_AUTO_START_INFO)); | |
| WriteServicePropertyError(service.ServiceName, "DelayedAutoStart"); |
Copilot
AI
Apr 10, 2026
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.
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"); |
Copilot
AI
Apr 10, 2026
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.
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"); |
| 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
|
||
| associated with the data types. | ||
|
|
||
| Example: | ||
|
|
||
| ... ado.net/XML headers & schema ... | ||
| <resheader name="resmimetype">text/microsoft-resx</resheader> | ||
| <resheader name="version">2.0</resheader> | ||
|
|
@@ -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. | ||
| --> | ||
|
|
@@ -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> | ||
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.
This helper writes to the verbose stream, but the name
WriteServicePropertyErrorsuggests it writes an error record. Renaming it to reflect the verbose-only behavior (e.g.,WriteServicePropertyVerbose/WriteCouldNotGetServicePropertyVerbose) would reduce confusion for future maintainers.