Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs (C#)

Code before:
                    WriteNonTerminatingError(
                        service,
                        exception,
                        "FailToOpenServiceControlManager",
                        ServiceResources.FailToOpenServiceControlManager,
                        ErrorCategory.PermissionDenied);
                }

                hService = NativeMethods.OpenServiceW(
                    hScManager,
                    service.ServiceName,
                    NativeMethods.SERVICE_QUERY_CONFIG
                );
                if (hService == IntPtr.Zero)
                {
                    lastError = Marshal.GetLastWin32Error();
                    Win32Exception exception = new(lastError);
                    WriteNonTerminatingError(
                        service,
                        exception,
                        "CouldNotGetServiceInfo",
                        ServiceResources.CouldNotGetServiceInfo,
                        ErrorCategory.PermissionDenied);
                }

                NativeMethods.SERVICE_DESCRIPTIONW description = new();
                bool querySuccessful = NativeMethods.QueryServiceConfig2<NativeMethods.SERVICE_DESCRIPTIONW>(hService, NativeMethods.SERVICE_CONFIG_DESCRIPTION, out description);

                NativeMethods.SERVICE_DELAYED_AUTO_START_INFO autostartInfo = new();
                querySuccessful = querySuccessful && NativeMethods.QueryServiceConfig2<NativeMethods.SERVICE_DELAYED_AUTO_START_INFO>(hService, NativeMethods.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, out autostartInfo);

                NativeMethods.QUERY_SERVICE_CONFIG serviceInfo = new();
                querySuccessful = querySuccessful && NativeMethods.QueryServiceConfig(hService, out serviceInfo);

                if (!querySuccessful)
                {
                    WriteNonTerminatingError(
                        service: service,
                        innerException: null,
                        errorId: "CouldNotGetServiceInfo",
                        errorMessage: ServiceResources.CouldNotGetServiceInfo,
                        category: ErrorCategory.PermissionDenied
                        );
                }

                PSProperty noteProperty = new("UserName", serviceInfo.lpServiceStartName);
                serviceAsPSObj.Properties.Add(noteProperty, true);
                serviceAsPSObj.TypeNames.Insert(0, "System.Service.ServiceController#UserName");

                noteProperty = new PSProperty("Description", description.lpDescription);
                serviceAsPSObj.Properties.Add(noteProperty, true);

Review comment (bug):
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.

Code after:
            finally
            {
                if (scManagerHandle != nint.Zero)
                {
                    bool succeeded = NativeMethods.CloseServiceHandle(scManagerHandle);
                    Diagnostics.Assert(succeeded, "SCManager handle close failed");
                }
            }
        }

        #endregion Overrides

#nullable enable
        /// <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)
        {
            NakedWin32Handle hService = nint.Zero;

            // As these are optional values, a failure due to permissions or
            // other problem is ignored and the properties are set to null.
            bool? isDelayedAutoStart = null;
            string? binPath = null;
            string? description = null;
            string? startName = null;
            ServiceStartupType startupType = ServiceStartupType.InvalidValue;
            try
            {
                // We don't use service.ServiceHandle as that requests
                // SERVICE_ALL_ACCESS when we only need SERVICE_QUERY_CONFIG.
                hService = NativeMethods.OpenServiceW(
                    scManagerHandle,
                    service.ServiceName,
                    NativeMethods.SERVICE_QUERY_CONFIG
                );
                if (hService != nint.Zero)
                {
                    if (NativeMethods.QueryServiceConfig2(
                        hService,
                        NativeMethods.SERVICE_CONFIG_DESCRIPTION,
                        out NativeMethods.SERVICE_DESCRIPTIONW descriptionInfo))
                    {
                        description = descriptionInfo.lpDescription;
                    }

                    if (NativeMethods.QueryServiceConfig2(
                        hService,