-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Update ComputerUnix.cs to fix Restart- and Stop-Computer on Linux/Mac #19780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1fe8294
1ec7d7f
36f334c
439f210
a721390
fe7f66a
d2c064f
1190c86
fa1cc99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,16 @@ namespace Microsoft.PowerShell.Commands | |
| public sealed class RestartComputerCommand : CommandLineCmdletBase | ||
| { | ||
| // TODO: Support remote computers? | ||
|
|
||
| #region "Parameters" | ||
|
|
||
| /// <summary> | ||
| /// Force the operation to take place if possible. | ||
| /// </summary> | ||
| [Parameter] | ||
| public SwitchParameter Force { get; set; } | ||
|
|
||
| #endregion "Parameters" | ||
|
|
||
| #region "Overrides" | ||
|
|
||
|
|
@@ -29,6 +39,36 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase | |
| /// </summary> | ||
| protected override void BeginProcessing() | ||
| { | ||
| const string unixRestartCommand = "/sbin/shutdown"; | ||
| const string unixRestartArgs = "-r now"; | ||
|
|
||
| const string macOSRestartCommand = "osascript"; | ||
| const string macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; | ||
|
|
||
| const string macOSForceRestartCommand = "/sbin/shutdown"; | ||
| const string macOSForceRestartArgs = "-r now"; | ||
|
|
||
| string command; | ||
| string args; | ||
|
|
||
| if (Platform.IsMacOS) | ||
| { | ||
| if (Force.IsPresent) | ||
| { | ||
| command = macOSForceRestartCommand; | ||
| args = macOSForceRestartArgs; | ||
| } | ||
| else | ||
| { | ||
| command = macOSRestartCommand; | ||
| args = macOSRestartArgs; | ||
| } | ||
| } | ||
| else { | ||
| command = unixRestartCommand; | ||
| args = unixRestartArgs; | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now that you've changed the way shutdown is being called (and adding the more gentle osascript approach, I would be happier if we boosted the tests here, at least to have validation between force and not-force on mac
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In CI, a test hook will have to be needed to verify intended behavior |
||
| if (InternalTestHooks.TestStopComputer) | ||
| { | ||
| var retVal = InternalTestHooks.TestStopComputerResults; | ||
|
|
@@ -42,7 +82,7 @@ protected override void BeginProcessing() | |
| return; | ||
| } | ||
|
|
||
| RunCommand("/sbin/shutdown", "-r now"); | ||
| RunCommand(command, args); | ||
| } | ||
| #endregion "Overrides" | ||
| } | ||
|
|
@@ -58,19 +98,56 @@ protected override void BeginProcessing() | |
| public sealed class StopComputerCommand : CommandLineCmdletBase | ||
| { | ||
| // TODO: Support remote computers? | ||
|
|
||
| #region "Parameters" | ||
|
|
||
| /// <summary> | ||
| /// Force the operation to take place if possible. | ||
| /// </summary> | ||
| [Parameter] | ||
| public SwitchParameter Force { get; set; } | ||
|
|
||
| #endregion "Parameters" | ||
|
|
||
| #region "Overrides" | ||
|
|
||
| /// <summary> | ||
| /// BeginProcessing. | ||
| /// </summary> | ||
| protected override void BeginProcessing() | ||
| { | ||
| var args = "-P now"; | ||
|
|
||
| const string unixStopCommand = "/sbin/shutdown"; | ||
| const string unixStopArgs = "-P now"; | ||
|
|
||
| const string macOSStopCommand = "osascript"; | ||
| const string macOSStopArgs = @"-e 'tell application ""System Events"" to shut down'"; | ||
|
|
||
| const string macOSForceStopCommand = "/sbin/shutdown"; | ||
| const string macOSForceStopArgs = "-h now"; | ||
|
|
||
| string command; | ||
| string args; | ||
|
|
||
| if (Platform.IsMacOS) | ||
| { | ||
| args = "now"; | ||
| if (Force.IsPresent) | ||
| { | ||
| command = macOSForceStopCommand; | ||
| args = macOSForceStopArgs; | ||
| } | ||
| else | ||
| { | ||
| command = macOSStopCommand; | ||
| args = macOSStopArgs; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| command = unixStopCommand; | ||
| args = unixStopArgs; | ||
| } | ||
|
|
||
| if (InternalTestHooks.TestStopComputer) | ||
| { | ||
| var retVal = InternalTestHooks.TestStopComputerResults; | ||
|
|
@@ -84,7 +161,7 @@ protected override void BeginProcessing() | |
| return; | ||
| } | ||
|
|
||
| RunCommand("/sbin/shutdown", args); | ||
| RunCommand(command, args); | ||
| } | ||
| #endregion "Overrides" | ||
| } | ||
|
|
@@ -153,13 +230,13 @@ protected override void StopProcessing() | |
| /// <summary> | ||
| /// Run a command. | ||
| /// </summary> | ||
| protected void RunCommand(String command, String args) { | ||
| protected void RunCommand(string command, string args) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way |
||
| _process = new Process() | ||
| { | ||
| StartInfo = new ProcessStartInfo | ||
| { | ||
| FileName = "/sbin/shutdown", | ||
| Arguments = string.Empty, | ||
| FileName = command, | ||
| Arguments = args, | ||
| RedirectStandardOutput = false, | ||
| UseShellExecute = false, | ||
| CreateNoWindow = true, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is the more gentle approach, would it be a good idea to pop UI for confirmation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ShouldProcess might be appropriate.