From 1fe8294047e8d5ef82a9dade47ce5a8a1a3859dd Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Sat, 10 Jun 2023 18:24:16 -0400 Subject: [PATCH 1/9] Update ComputerUnix.cs to fix Restart- and Stop-Computer on Linux and Mac Fix RunCommand to respect 'command' and 'args' inputs. --- .../commands/management/ComputerUnix.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index a11fb0270c9..a4af0c5f3ad 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -153,13 +153,13 @@ protected override void StopProcessing() /// /// Run a command. /// - protected void RunCommand(String command, String args) { + protected void RunCommand(String command = "/sbin/shutdown", String args = string.Empty) { _process = new Process() { StartInfo = new ProcessStartInfo { - FileName = "/sbin/shutdown", - Arguments = string.Empty, + FileName = command, + Arguments = args, RedirectStandardOutput = false, UseShellExecute = false, CreateNoWindow = true, From 1ec7d7ff6b78bb5ad9b9b1af60afb873a4bea5a9 Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Sat, 10 Jun 2023 20:41:12 -0400 Subject: [PATCH 2/9] Change RunCommand args default value to constant Can't use string.empty as a default parameter value. Changed to "". --- .../commands/management/ComputerUnix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index a4af0c5f3ad..fcd1edadd8e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -153,7 +153,7 @@ protected override void StopProcessing() /// /// Run a command. /// - protected void RunCommand(String command = "/sbin/shutdown", String args = string.Empty) { + protected void RunCommand(String command = "/sbin/shutdown", String args = "") { _process = new Process() { StartInfo = new ProcessStartInfo From 36f334c480cb4f1cd79c816acaa92b7b63652fce Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:25:10 -0400 Subject: [PATCH 3/9] Update RunCommand params per PR input from iSazinov Remove default parameter assignments and change 'String' to 'string'. --- .../commands/management/ComputerUnix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index fcd1edadd8e..5bfaa54a7f5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -153,7 +153,7 @@ protected override void StopProcessing() /// /// Run a command. /// - protected void RunCommand(String command = "/sbin/shutdown", String args = "") { + protected void RunCommand(string command, string args) { _process = new Process() { StartInfo = new ProcessStartInfo From 439f2108d56977b05a112fb98d62a83407918abc Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Tue, 13 Jun 2023 22:34:27 -0400 Subject: [PATCH 4/9] Update ComputerUnix.cs change MacOs Stop- args to -h now Change MacOs 'Stop-Computer' args from 'now' to '-h now' per PR directive. --- .../commands/management/ComputerUnix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 5bfaa54a7f5..15c8a265701 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -69,7 +69,7 @@ protected override void BeginProcessing() var args = "-P now"; if (Platform.IsMacOS) { - args = "now"; + args = "-h now"; } if (InternalTestHooks.TestStopComputer) { From a7213909aa28e9272b31909345299e5463cbe678 Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Thu, 15 Jun 2023 22:54:52 -0400 Subject: [PATCH 5/9] Update ComputerUnix.cs with MacOs and Unix force and noforce args Added handling for MacOS and Unix Force and NoForce restarts and shutdowns. --- .../commands/management/ComputerUnix.cs | 85 ++++++++++++++++++- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 15c8a265701..ef71b40e517 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; } = false; + +#endregion "Parameters" #region "Overrides" @@ -29,6 +39,36 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// protected override void BeginProcessing() { + string unixRestartCommand = "/sbin/shutdown"; + string unixRestartArgs = "-r now"; + + string macOSRestartCommand = "osascript"; + string macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; + + string macOSForceRestartCommand = "/sbin/shutdown"; + 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; } = false; +#endregion "Parameters" + #region "Overrides" /// @@ -66,11 +116,38 @@ public sealed class StopComputerCommand : CommandLineCmdletBase /// protected override void BeginProcessing() { - var args = "-P now"; + + string unixStopCommand = "/sbin/shutdown"; + string unixStopArgs = "-P now"; + + string macOSStopCommand = "osascript"; + string macOSStopArgs = @"-e 'tell application ""System Events"" to shut down'"; + + string macOSForceStopCommand = "/sbin/shutdown"; + string macOSForceStopArgs = "-h now"; + + string command; + string args; + if (Platform.IsMacOS) { - args = "-h 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" } From fe7f66a476c66ec7b6cf2328712e7fe3785933c0 Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Fri, 16 Jun 2023 00:23:27 -0400 Subject: [PATCH 6/9] Update ComputerUnix.cs - add const to string declarations and remove switch default value Implement requested changes from ISazonov. Change string to string const Remove unnecessary default value of false on Force parameter declarations. --- .../commands/management/ComputerUnix.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index ef71b40e517..51d3e220a4b 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -28,7 +28,7 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; } = false; + public SwitchParameter Force { get; set; }; #endregion "Parameters" @@ -39,14 +39,14 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// protected override void BeginProcessing() { - string unixRestartCommand = "/sbin/shutdown"; - string unixRestartArgs = "-r now"; + string const unixRestartCommand = "/sbin/shutdown"; + string const unixRestartArgs = "-r now"; - string macOSRestartCommand = "osascript"; - string macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; + string const macOSRestartCommand = "osascript"; + string const macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; - string macOSForceRestartCommand = "/sbin/shutdown"; - string macOSForceRestartArgs = "-r now"; + string const macOSForceRestartCommand = "/sbin/shutdown"; + string const macOSForceRestartArgs = "-r now"; string command; string args; @@ -105,7 +105,7 @@ public sealed class StopComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; } = false; + public SwitchParameter Force { get; set; }; #endregion "Parameters" @@ -117,14 +117,14 @@ public sealed class StopComputerCommand : CommandLineCmdletBase protected override void BeginProcessing() { - string unixStopCommand = "/sbin/shutdown"; - string unixStopArgs = "-P now"; + string const unixStopCommand = "/sbin/shutdown"; + string const unixStopArgs = "-P now"; - string macOSStopCommand = "osascript"; - string macOSStopArgs = @"-e 'tell application ""System Events"" to shut down'"; + string const macOSStopCommand = "osascript"; + string const macOSStopArgs = @"-e 'tell application ""System Events"" to shut down'"; - string macOSForceStopCommand = "/sbin/shutdown"; - string macOSForceStopArgs = "-h now"; + string const macOSForceStopCommand = "/sbin/shutdown"; + string const macOSForceStopArgs = "-h now"; string command; string args; From d2c064ff573e81aefc3b2080cb54f45f5800e4a9 Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Fri, 16 Jun 2023 00:47:06 -0400 Subject: [PATCH 7/9] Update ComputerUnix.cs Restore default value FALSE to FORCE params Removing the assignment seems to have caused validation testing to fail. --- .../commands/management/ComputerUnix.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 51d3e220a4b..fda53a18c5a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -28,7 +28,7 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; }; + public SwitchParameter Force { get; set; } = false; #endregion "Parameters" @@ -105,7 +105,7 @@ public sealed class StopComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; }; + public SwitchParameter Force { get; set; } = false; #endregion "Parameters" From 1190c862245913abd787211213ec90416172e6f0 Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Fri, 16 Jun 2023 10:34:24 -0400 Subject: [PATCH 8/9] Update ComputerUnix.cs fix const string syntax error --- .../commands/management/ComputerUnix.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index fda53a18c5a..1b7c5d47d99 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -28,7 +28,7 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; } = false; + public SwitchParameter Force { get; set; }; #endregion "Parameters" @@ -39,14 +39,14 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// protected override void BeginProcessing() { - string const unixRestartCommand = "/sbin/shutdown"; - string const unixRestartArgs = "-r now"; + const string unixRestartCommand = "/sbin/shutdown"; + const string unixRestartArgs = "-r now"; - string const macOSRestartCommand = "osascript"; - string const macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; + const string macOSRestartCommand = "osascript"; + const string macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; - string const macOSForceRestartCommand = "/sbin/shutdown"; - string const macOSForceRestartArgs = "-r now"; + const string macOSForceRestartCommand = "/sbin/shutdown"; + const string macOSForceRestartArgs = "-r now"; string command; string args; @@ -105,7 +105,7 @@ public sealed class StopComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; } = false; + public SwitchParameter Force { get; set; }; #endregion "Parameters" @@ -117,14 +117,14 @@ public sealed class StopComputerCommand : CommandLineCmdletBase protected override void BeginProcessing() { - string const unixStopCommand = "/sbin/shutdown"; - string const unixStopArgs = "-P now"; + const string unixStopCommand = "/sbin/shutdown"; + const string unixStopArgs = "-P now"; - string const macOSStopCommand = "osascript"; - string const macOSStopArgs = @"-e 'tell application ""System Events"" to shut down'"; + const string macOSStopCommand = "osascript"; + const string macOSStopArgs = @"-e 'tell application ""System Events"" to shut down'"; - string const macOSForceStopCommand = "/sbin/shutdown"; - string const macOSForceStopArgs = "-h now"; + const string macOSForceStopCommand = "/sbin/shutdown"; + const string macOSForceStopArgs = "-h now"; string command; string args; From fa1cc9948ba25ac25a18b6e3f34796705e16c2ba Mon Sep 17 00:00:00 2001 From: mark-wilson-spc <129769889+mark-wilson-spc@users.noreply.github.com> Date: Fri, 16 Jun 2023 11:01:20 -0400 Subject: [PATCH 9/9] Update ComputerUnix.cs fix syntax error --- .../commands/management/ComputerUnix.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 1b7c5d47d99..42dd0b775b1 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -28,7 +28,7 @@ public sealed class RestartComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; }; + public SwitchParameter Force { get; set; } #endregion "Parameters" @@ -105,7 +105,7 @@ public sealed class StopComputerCommand : CommandLineCmdletBase /// Force the operation to take place if possible. /// [Parameter] - public SwitchParameter Force { get; set; }; + public SwitchParameter Force { get; set; } #endregion "Parameters"