Skip to content

[WIP] Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet#17479

Closed
kvprasoon wants to merge 1429 commits into
PowerShell:masterfrom
kvprasoon:stopmessage-4857
Closed

[WIP] Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet#17479
kvprasoon wants to merge 1429 commits into
PowerShell:masterfrom
kvprasoon:stopmessage-4857

Conversation

@kvprasoon

@kvprasoon kvprasoon commented Jun 4, 2022

Copy link
Copy Markdown
Contributor

PR Summary

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

@ghost ghost assigned adityapatwardhan Jun 4, 2022
@kvprasoon kvprasoon changed the title Addin comment, Reason and RestartDelay parameter for Staop/REstart-Computer cmdlet [WIP] Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet Jun 4, 2022
@ghost ghost added the Review - Needed The PR is being reviewed label Jun 11, 2022
@ghost

ghost commented Jun 11, 2022

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

/// Specify a delay before the reboot occurs.
/// </summary>
[Parameter(ParameterSetName = DefaultParameterSet)]
[ValidateRange(0, UInt32.MaxValue)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
[ValidateRange(0, UInt32.MaxValue)]
[ValidateRange(ValidateRangeKind.Positive)]

flags[1] = Comment;
flags[2] = ShutdownReasons.Planned;

switch (Reason.ToString())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why can't we assign flags[2] = Reason?

Comment thread src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs Outdated
/// Provide a comment for rebooting the computer.
/// </summary>
[Parameter(ParameterSetName = DefaultParameterSet)]
public string Comment { get; set; } = string.Empty;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we assign empty string? Can the parameter be null? If no we should add validation attribute.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/PowerShell/PowerShell/pull/8112/files#r228037418

It accepts null, will change to nullOrEmpty.

/// <summary>
/// System related shutdown/restart.
/// </summary>
System = 0x80050000,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have doubts that we should allow all these values. Perhaps the best options is the ones offered by WIndows GUI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

was referring to previous discussions in the old PR
#8112 (comment)

Thought its an agreement to go ahead with the entire list.

@pull-request-quantifier-deprecated

Copy link
Copy Markdown

This PR has 320 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Large
Size       : +305 -15
Percentile : 72%

Total files changed: 3

Change summary by file extension:
.cs : +281 -15
.ps1 : +24 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@kvprasoon
kvprasoon requested a review from iSazonov May 6, 2023 17:12
xtqqczze and others added 20 commits October 26, 2025 15:55
PowerShell#26315)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
…owerShell#26322)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
…PowerShell#26268)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
Co-authored-by: Dongbo Wang <dongbow@microsoft.com>
Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
…owerShell#26326)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
The following experimental features are made stable:
- PSNativeWindowsTildeExpansion
- PSRedirectToVariable
- PSSubsystemPluginModel
…owerShell#26355)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
… existing actions to use reusable get-changed-files action (PowerShell#26350)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Travis Plunk (HE/HIM) <tplunk@ntdev.microsoft.com>
…PowerShell#26365)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
pwshBot and others added 5 commits June 10, 2026 15:02
Co-authored-by: Aditya Patwardhan <adityap@microsoft.com>
…#27608)

Co-authored-by: PowerShell Team Bot <69177312+pwshBot@users.noreply.github.com>
Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
Co-authored-by: Patrick Meinecke <SeeminglyScience@users.noreply.github.com>
Co-authored-by: Justin Chung <chungjustin@microsoft.com>
@kilasuit

Copy link
Copy Markdown
Collaborator

@kvprasoon are you still interested in finishing off this PR?

behradbhrmi and others added 2 commits June 23, 2026 11:11
Co-authored-by: Justin Chung <124807742+jshigetomi@users.noreply.github.com>
Co-authored-by: Ilya <darpa@yandex.ru>
@kvprasoon
kvprasoon requested a review from a team as a code owner June 23, 2026 18:09
Copilot AI review requested due to automatic review settings June 23, 2026 18:09
@kvprasoon

Copy link
Copy Markdown
Contributor Author

Hey @kilasuit - I would like to. This was pending review from very long time. I will try to rebase and update the PR this week.

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 Windows implementations of Stop-Computer and Restart-Computer to support -Comment, -Reason, and delay parameters (to align more closely with shutdown.exe behavior), and updates the WSMan CIM invocation to call Win32ShutdownTracker with the additional arguments.

Changes:

  • Add ShutdownReasons enum plus new cmdlet parameters (Comment, Reason, and delay) to Stop-Computer / Restart-Computer.
  • Switch WSMan shutdown invocation from Win32Shutdown to Win32ShutdownTracker and pass Timeout, Comment, and ReasonCode.
  • Add Pester tests intended to validate parameter availability for Stop-Computer / Restart-Computer.

Reviewed changes

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

File Description
test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 Adds tests for new -Comment and -Reason parameters.
test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 Adds tests for new -Comment and -Reason parameters.
src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs Adds new parameters/enums and updates WSMan shutdown invocation to use Win32ShutdownTracker.

Comment on lines +52 to +56
$ReasonList = "Application", "Hardware", "OperatingSystem", "Other", "Power", "Software", "System", `
"BlueScreen", "Disk", "Environment", "Driver", "HotFix", "HotFixUninstall", "Unresponsive", "Installation", `
"Maintenance", "MMC", "NetworkConnectivity", "NetworkCard", "Other", "OtherDriver", "PowerSupply", "Processor", "Reconfigure", `
"SecurityIssue", "SecurityPatch","SecurityPatchUninstallation", "ServicePack", "ServicePackUninstallation", "TerminalServices", `
"Unstable", "Upgrade", "WMI"
Comment on lines +50 to +55
It "Should support Reason types" {
$ReasonList = "Application", "Hardware", "OperatingSystem", "Other", "Power", "Software", "System", `
"BlueScreen", "Disk", "Environment", "Driver", "HotFix", "HotFixUninstall", "Unresponsive", "Installation", `
"Maintenance", "MMC", "NetworkConnectivity", "NetworkCard", "Other", "OtherDriver", "PowerSupply", "Processor", "Reconfigure", `
"SecurityIssue", "SecurityPatch","SecurityPatchUninstallation", "ServicePack", "ServicePackUninstallation", "TerminalServices", `
"Unstable", "Upgrade", "WMI"
Comment on lines 2016 to 2019
// If successful and the Restart parameter is specified, restart the computer
object[] flags = new object[] { 6, 0 };
ComputerWMIHelper.InvokeWin32ShutdownUsingWsman(
object[] flags = new object[] { 0, 0, 0, 6 };
ComputerWMIHelper.InvokeWin32ShutdownTrackerUsingWsman(
this,
Comment on lines +1522 to +1524
[Parameter]
[ValidateRange(ValidateRangeKind.Positive)]
public UInt32 ShutdownDelay { get; set; }
Comment on lines +1519 to +1521
/// <summary>
/// Specify a delay before the reboot occurs.
/// </summary>
Comment on lines +151 to +159
/// <summary>
/// Application related shutdown/restart.
/// </summary>
Application = 0x00040000,

/// <summary>
/// Hardware related shutdown/restart.
/// </summary>
Hardware = 0x80010000,
Comment on lines +45 to +49
It "Should support -Comment parameter" {
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
$comment = "Testing comment"
Stop-Computer -Comment $comment -ErrorAction Stop | Should -BeNullOrEmpty
}
Comment on lines +51 to +60
It "Should support Reason types" {
$ReasonList = "Application", "Hardware", "OperatingSystem", "Other", "Power", "Software", "System", `
"BlueScreen", "Disk", "Environment", "Driver", "HotFix", "HotFixUninstall", "Unresponsive", "Installation", `
"Maintenance", "MMC", "NetworkConnectivity", "NetworkCard", "Other", "OtherDriver", "PowerSupply", "Processor", "Reconfigure", `
"SecurityIssue", "SecurityPatch","SecurityPatchUninstallation", "ServicePack", "ServicePackUninstallation", "TerminalServices", `
"Unstable", "Upgrade", "WMI"
foreach ( $reason in $ReasonList ) {
Stop-Computer Reason $reason | Should -BeNullOrEmpty
}
}
Comment on lines +44 to +48
It "Should support -Comment parameter" {
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
$comment = "Testing comment"
Restart-Computer -Comment $comment -ErrorAction Stop | Should -BeNullOrEmpty
}
Comment on lines +50 to +59
It "Should support Reason types" {
$ReasonList = "Application", "Hardware", "OperatingSystem", "Other", "Power", "Software", "System", `
"BlueScreen", "Disk", "Environment", "Driver", "HotFix", "HotFixUninstall", "Unresponsive", "Installation", `
"Maintenance", "MMC", "NetworkConnectivity", "NetworkCard", "Other", "OtherDriver", "PowerSupply", "Processor", "Reconfigure", `
"SecurityIssue", "SecurityPatch","SecurityPatchUninstallation", "ServicePack", "ServicePackUninstallation", "TerminalServices", `
"Unstable", "Upgrade", "WMI"
foreach ( $reason in $ReasonList ) {
Restart-Computer -Reason $reason | Should -BeNullOrEmpty
}
}
yotsuda and others added 12 commits June 26, 2026 11:15
…rShell#26185)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Justin Chung <124807742+jshigetomi@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Justin Chung <124807742+jshigetomi@users.noreply.github.com>
…owerShell#27611)

Co-authored-by: Justin Chung <chungjustin@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ipeline (PowerShell#27667)

1. Create the `LocProject.json` file for PowerShell localization.
2. Remove the unused `.resx` resource files and source files.
3. Update the `ResourceValidation` tests.
Co-authored-by: Ilya <darpa@yandex.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Large Review - Needed The PR is being reviewed

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