Repository navigation
Adding comments and reasons to Restart-Computer/Stop-Computer - #8112
Luke (lukepayne) wants to merge 1 commit into
Conversation
This change adds the ability to specify a comment as well as reason codes for a reboot or shutdown of the computer. Also added is the option to specify a timeout before the reboot/shutdown occurs. This issues is raised in issue no. 4857
|
Luke (@lukepayne) if you put something like "fixes #4857" in the PR description, then the issue will be closed when the maintainers merge the PR. Just saves them a few moments extra work 😉 |
| private const uint TerminalServices = 0x00000020; | ||
| private const uint Unstable = 0x00000006; | ||
| private const uint Upgrade = 0x00000003; | ||
| private const uint WMI = 0x00000015; |
There was a problem hiding this comment.
There are quite a few reason codes available which would indicate to me that this would be much better off as an enum for cleanliness and clarity; much easier for users to discover as well, if the parameter is typed with the enum type!
| "Unstable", | ||
| "Upgrade", | ||
| "WMI")] | ||
| public string MinorReason { get; set; } |
There was a problem hiding this comment.
Per my other comment, you can skip ValidateSet entirely and you could opt to either have two enums (one for major and one for minor reason) or you could opt to roll it all into a single enum and parameter, depending on which is appropriate.
If multiple codes can be specified, tag the enum(s) with the [Flags] attribute to permit this.
There was a problem hiding this comment.
Seems we should review the list. How we can call the cmdlet if "BlueScreen"?
There was a problem hiding this comment.
I tried to go for parity with what the Win32ShutdownTracker supports, but it makes sense to remove some of the illogical ones. Do you have suggestions on what should be removed?
There was a problem hiding this comment.
I'd look options in Windows restart/shootdown dialog box.
There was a problem hiding this comment.
It seems a bit restrictive given what the API supports.
because not all options make sense in interactive mode.
There was a problem hiding this comment.
Correct. So I would suggest not whittling it down to this list.
| break; | ||
| case WaitForServiceTypes.PowerShell: | ||
| _powershell = System.Management.Automation.PowerShell.Create(); | ||
| _powershell = global::System.Management.Automation.PowerShell.Create(); |
There was a problem hiding this comment.
Is this change necessary & what does it accomplish here? Never seen this sort of syntax in c# before, and I don't know if it's used anywhere else in the repo here.
There was a problem hiding this comment.
This forces the call to search in the global namespace, otherwise the 'System' prefix grabs the local System variable. However, this likely won't be an issue after the the reason codes are converted to enums.
| private const uint TerminalServices = 0x00000020; | ||
| private const uint Unstable = 0x00000006; | ||
| private const uint Upgrade = 0x00000003; | ||
| private const uint WMI = 0x00000015; |
There was a problem hiding this comment.
Same as above; it looks like you could roll most or all of these into a single enum that is shared between both commands.
| /// <param name="cancelToken">Cancel token</param> | ||
| /// <returns>True on success</returns> | ||
| internal static bool InvokeWin32ShutdownUsingWsman( | ||
| internal static bool InvokeWin32ShutdownTrackerUsingWsman( |
There was a problem hiding this comment.
I don't think it's a good idea to change the method name itself. If a new overload or a new method that does what you need is necessary, that's probably better, unless you're absolutely sure that nothing else calls this method at any point. It's flagged as internal, so potentially anything in the same namespace could call it, though I'm not sure if anything currently does.
There was a problem hiding this comment.
There's no specific reason why it needs to be changed, it's just to match the name of the new CimOperatingSystemShutdownMethod. I can revert to the previous name as I don't think an overload is necessary.
There was a problem hiding this comment.
Gotcha. Yeah, if you can check the references to that method to see if anything else happened to also use it that would be best.
Otherwise I'll be more than happy to do a quick check in a bit just to make sure. 😆
There was a problem hiding this comment.
Just did a quick check, and it doesn't look like anything else is using this method, so it's probably fine either way. 😄
There was a problem hiding this comment.
Oh great, thanks. I might leave it as is then to match the new shutdown method name 😀
Thanks for the tip! I'll add that in :) |
Ilya (iSazonov)
left a comment
There was a problem hiding this comment.
We shouldn't duplicate code (dry principle) in both cmdlets.
| @@ -173,6 +173,44 @@ public class RestartComputerCommand : PSCmdlet, IDisposable | |||
| private const string DefaultParameterSet = "DefaultSet"; | |||
| private const int forcedReboot = 6; // see https://msdn.microsoft.com/library/aa394058(v=vs.85).aspx | |||
|
|
|||
There was a problem hiding this comment.
Please update the link. (Now it is redirected).
And add doc link for new constants you are added below.
| /// Provide a comment for rebooting the computer. | ||
| /// </summary> | ||
| [Parameter(ParameterSetName = DefaultParameterSet)] | ||
| public string Comment { get; set; } = ""; |
There was a problem hiding this comment.
Please use string.Empty.
Style issue: all public comments for properties with { get; set; } should start with "Gets or sets"; (line 265)
| "Power", | ||
| "Software", | ||
| "System")] | ||
| public string MajorReason { get; set; } |
There was a problem hiding this comment.
It should enum too.
| "Unstable", | ||
| "Upgrade", | ||
| "WMI")] | ||
| public string MinorReason { get; set; } |
There was a problem hiding this comment.
Seems we should review the list. How we can call the cmdlet if "BlueScreen"?
| _restartDelay = value; | ||
| } | ||
| } | ||
| private UInt32 _restartDelay = 0; |
There was a problem hiding this comment.
Please remove and use auto-property public UInt32 RestartDelay { get; set; }.
No need to init - default is 0.
| _status = ComputerResources.RestartComplete; | ||
| WriteProgress(_indicator[indicatorIndex % 4] + _activity, _status, 100, ProgressRecordType.Completed); | ||
| _timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite); | ||
| _timer.Change(global::System.Threading.Timeout.Infinite, global::System.Threading.Timeout.Infinite); |
There was a problem hiding this comment.
Win32ShutdownTracker supports timeout so we could remove the code. /cc Steve Lee (@SteveL-MSFT)
|
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. |
|
I'm hoping a comment is sufficient to keep this open for now. I intend to finish and commit once I'm back from holidays. Thanks! |
|
Luke (@lukepayne) even if stale-bot closes a PR, it's not deleted, so you can always come back and reactivate it. stale-bot just helps clean up some noise so people know which PRs are active and require attention. |
|
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. |
|
This PR has been automatically closed because it is stale. If you wish to continue working on the PR, please first update the PR, then reopen it. |


PR Summary
This change adds the ability to specify a comment as well as reason codes
for a reboot or shutdown of the computer. Also added is the option to
specify a timeout before the reboot/shutdown occurs.
fixes #4857
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests