Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet#27721
Adding comment, Reason and RestartDelay parameter for Stop/Restart-Computer cmdlet#27721kvprasoon wants to merge 3 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
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
ShutdownReasonsenum and expose new cmdlet parameters (Comment,Reason, and delay-related parameters). - Switch WSMan shutdown/restart invocation to
Win32ShutdownTrackerand passTimeout,Comment,ReasonCode, andFlags. - Add Pester coverage for the new
-Commentand-Reasonparameters.
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 validValidateRangeAttributeoverload and is likely to break compilation. Use a numeric range (as done forRestartDelay) 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.Commentis passed through to the Win32ShutdownTracker call even when the parameter isn't bound. Defaulting it tostring.Emptyavoids sendingnullto CIM/WMI and also matches theRestart-Computerimplementation.
[Parameter]
[ValidateNotNullOrEmpty]
public string Comment { get; set; }
src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs:1536
-Reason Plannedcannot work forStop-Computerbecause the switch doesn't handle thePlannedenum name; it falls through todefaultand sendsOtherinstead.
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 withShutdownReasonsenum values, but the CIM parameter is declared asUInt32. Passing a boxed enum here can cause runtime type validation failures inCimMethodParameter.Create. Convert the value touintbefore adding the parameter.
methodParameters.Add(CimMethodParameter.Create(
"ReasonCode",
flags[2],
Microsoft.Management.Infrastructure.CimType.UInt32,
CimFlags.None));
| /// <summary> | ||
| /// Specify a delay before the reboot occurs. | ||
| /// </summary> | ||
| [Parameter] | ||
| [ValidateRange(0, UInt32.MaxValue)] | ||
| public UInt32 ShutdownDelay { get; set; } |
| foreach ( $reason in $ReasonList ) { | ||
| Stop-Computer -Reason $reason | Should -BeNullOrEmpty | ||
| } |
| foreach ( $reason in $ReasonList ) { | ||
| Restart-Computer -Reason $reason | Should -BeNullOrEmpty | ||
| } |
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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header