diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index a11fb0270c9..42dd0b775b1 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -21,6 +21,16 @@ namespace Microsoft.PowerShell.Commands public sealed class RestartComputerCommand : CommandLineCmdletBase { // TODO: Support remote computers? + +#region "Parameters" + + /// + /// Force the operation to take place if possible. + /// + [Parameter] + public SwitchParameter Force { get; set; } + +#endregion "Parameters" #region "Overrides" @@ -29,6 +39,36 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// 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; + } + 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,7 +98,17 @@ protected override void BeginProcessing() public sealed class StopComputerCommand : CommandLineCmdletBase { // TODO: Support remote computers? + +#region "Parameters" + + /// + /// Force the operation to take place if possible. + /// + [Parameter] + public SwitchParameter Force { get; set; } +#endregion "Parameters" + #region "Overrides" /// @@ -66,11 +116,38 @@ public sealed class StopComputerCommand : CommandLineCmdletBase /// 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() /// /// Run a command. /// - protected void RunCommand(String command, String args) { + protected void RunCommand(string command, string args) { _process = new Process() { StartInfo = new ProcessStartInfo { - FileName = "/sbin/shutdown", - Arguments = string.Empty, + FileName = command, + Arguments = args, RedirectStandardOutput = false, UseShellExecute = false, CreateNoWindow = true,