Skip to content

Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet#27721

Open
kvprasoon wants to merge 3 commits into
PowerShell:masterfrom
kvprasoon:feat/stopmessage-4857
Open

Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet#27721
kvprasoon wants to merge 3 commits into
PowerShell:masterfrom
kvprasoon:feat/stopmessage-4857

Conversation

@kvprasoon

Copy link
Copy Markdown
Contributor

PR Summary

Recreated closing old PR 17479 which fixes: #4857

PR Context

This PR helps as it adds parameters which helps PowerShell users use Stop-Computer and Restart-Computer more like shutdown.exe.

PR Checklist

Copilot AI review requested due to automatic review settings July 23, 2026 17:40
@kvprasoon
kvprasoon requested a review from a team as a code owner July 23, 2026 17:40
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@kvprasoon kvprasoon changed the title Feat/stopmessage 4857 Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet Jul 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the Stop-Computer and Restart-Computer cmdlets with additional shutdown-tracker-related parameters (comment, reason, delay) to align more closely with shutdown.exe scenarios from #4857, and updates the WSMan/CIM invocation to call Win32ShutdownTracker so that metadata can be passed to the OS.

Changes:

  • Add a ShutdownReasons enum and expose new cmdlet parameters (Comment, Reason, and delay-related parameters).
  • Switch WSMan shutdown/restart invocation to Win32ShutdownTracker and pass Timeout, Comment, ReasonCode, and Flags.
  • Add Pester coverage for the new -Comment and -Reason parameters.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs Introduces shutdown reason enum/parameters and switches WSMan shutdown invocation to Win32ShutdownTracker with additional CIM parameters.
test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 Adds tests asserting Stop-Computer accepts -Comment and -Reason.
test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 Adds tests asserting Restart-Computer accepts -Comment and -Reason.
Comments suppressed due to low confidence (4)

src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs:1482

  • [ValidateRange(0, ValidateRangeKind.Positive)] is not a valid ValidateRangeAttribute overload and is likely to break compilation. Use a numeric range (as done for RestartDelay) to allow 0..UInt32.MaxValue.
        [Parameter]
        [ValidateRange(0, ValidateRangeKind.Positive)]
        public UInt32 ShutdownDelay { get; set; }

src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs:1489

  • StopComputerCommand.Comment is passed through to the Win32ShutdownTracker call even when the parameter isn't bound. Defaulting it to string.Empty avoids sending null to CIM/WMI and also matches the Restart-Computer implementation.
        [Parameter]
        [ValidateNotNullOrEmpty]
        public string Comment { get; set; }

src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs:1536

  • -Reason Planned cannot work for Stop-Computer because the switch doesn't handle the Planned enum name; it falls through to default and sends Other instead.
                case "Application":
                    flags[2] = ShutdownReasons.Application;
                    break;
                case "Hardware":
                    flags[2] = ShutdownReasons.Hardware;

src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs:2548

  • flags[2] is populated with ShutdownReasons enum values, but the CIM parameter is declared as UInt32. Passing a boxed enum here can cause runtime type validation failures in CimMethodParameter.Create. Convert the value to uint before adding the parameter.
                    methodParameters.Add(CimMethodParameter.Create(
                        "ReasonCode",
                        flags[2],
                        Microsoft.Management.Infrastructure.CimType.UInt32,
                        CimFlags.None));

Comment on lines +334 to +339
/// <summary>
/// Specify a delay before the reboot occurs.
/// </summary>
[Parameter]
[ValidateRange(0, UInt32.MaxValue)]
public UInt32 ShutdownDelay { get; set; }
Comment on lines +57 to +59
foreach ( $reason in $ReasonList ) {
Stop-Computer -Reason $reason | Should -BeNullOrEmpty
}
Comment on lines +56 to +58
foreach ( $reason in $ReasonList ) {
Restart-Computer -Reason $reason | Should -BeNullOrEmpty
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restart-Computer and Stop-Computer should take custom message like shutdown.exe

2 participants