From 3e3a51c70867470308382c9e2dbc5f101a31522f Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 19 Jun 2023 15:00:29 -0700 Subject: [PATCH 01/23] Computer cmdlets should fail with error when not run via sudo on Unix --- .../commands/management/ComputerUnix.cs | 10 ++++++++++ .../Restart-Computer.Tests.ps1 | 6 ++++++ .../Stop-Computer.Tests.ps1 | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index a11fb0270c9..0dd4d64cdc4 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -161,11 +161,21 @@ protected void RunCommand(String command, String args) { FileName = "/sbin/shutdown", Arguments = string.Empty, RedirectStandardOutput = false, + RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, } }; _process.Start(); + _process.WaitForExit(); + if (_process.ExitCode != 0) + { + string stderr = _process.StandardError.ReadToEnd(); + string errMsg = StringUtil.Format("Command returned 0x{0:X}: {1}", _process.ExitCode, stderr); + ErrorRecord error = new ErrorRecord( + new InvalidOperationException(errMsg), "CommandFailed", ErrorCategory.OperationStopped, null); + WriteError(error); + } } #endregion } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 8a2c050a90f..4eb1d5b1fc2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -107,3 +107,9 @@ finally Disable-Testhook -testhookName $restartTesthookName Set-TesthookResult -testhookName $restartTesthookResultName -value 0 } + +Describe 'Non-admin on Unix' { + It 'Reports error if not run under sudo' -Skip:($IsWindows) { + { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" + } +} diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 index c2a89117167..e3ccf4f1448 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 @@ -58,3 +58,9 @@ finally Disable-Testhook -testhookName $stopTesthook Set-TesthookResult -testhookName $stopTesthookResultName -Value $DefaultResultValue } + +Describe 'Non-admin on Unix' { + It 'Reports error if not run under sudo' -Skip:($IsWindows) { + { Stop-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.StopComputerCommand" + } +} From 59aa58b72bf793bb39eaead6af0e8ac3419ae930 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 26 Jun 2023 14:01:31 -0700 Subject: [PATCH 02/23] add tracing for test failure --- .../Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 4eb1d5b1fc2..a3bdeb0dfaf 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -110,6 +110,6 @@ finally Describe 'Non-admin on Unix' { It 'Reports error if not run under sudo' -Skip:($IsWindows) { - { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" + { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" -Because (Get-Error | Out-String) } } From 97210bc61e541376071fd89cbb3b9ebbc29ce9a6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 26 Jun 2023 14:58:24 -0700 Subject: [PATCH 03/23] fix test to skip if running as root --- .../Restart-Computer.Tests.ps1 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index a3bdeb0dfaf..f601bed406c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -109,7 +109,16 @@ finally } Describe 'Non-admin on Unix' { - It 'Reports error if not run under sudo' -Skip:($IsWindows) { - { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" -Because (Get-Error | Out-String) + BeforeAll { + if ($IsWindows -or (id -u) -eq 0) { + $IsWindowsOrSudo = $true + } + else { + $IsWindowsOrSudo = $false + } + } + + It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { + { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" } } From c34bbe06694e2260310b07a9bd79a215d157bacf Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 26 Jun 2023 15:57:46 -0700 Subject: [PATCH 04/23] add test tracing to see if running as root --- .../Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index f601bed406c..995aec05272 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -116,6 +116,8 @@ Describe 'Non-admin on Unix' { else { $IsWindowsOrSudo = $false } + + Write-Verbose -Verbose "IsWindowsOrSudo: $IsWindowsOrSudo" } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { From 9a6c5a6c1bfde082c5e0571cfebbb6de7bbe9696 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 26 Jun 2023 16:17:54 -0700 Subject: [PATCH 05/23] change to use group id and add more traces --- .../Restart-Computer.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 995aec05272..e775155bf9c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -110,13 +110,15 @@ finally Describe 'Non-admin on Unix' { BeforeAll { - if ($IsWindows -or (id -u) -eq 0) { + if ($IsWindows -or (id -g) -eq 0) { $IsWindowsOrSudo = $true } else { $IsWindowsOrSudo = $false } + Write-Verbose -Verbose "user id: $(id -u)" + Write-Verbose -Verbose "group id: $(id -g)" Write-Verbose -Verbose "IsWindowsOrSudo: $IsWindowsOrSudo" } From df0337fdbfc42d917643529ed69c66d04d97dda6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 27 Jun 2023 08:24:13 -0700 Subject: [PATCH 06/23] add back tracing of error --- .../Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index e775155bf9c..3a415c59642 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -123,6 +123,6 @@ Describe 'Non-admin on Unix' { } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { - { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" + { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" -Because (Get-Error) } } From 488877ebec5db87da9c7710cd6822cee64a29d1c Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 27 Jun 2023 09:03:55 -0700 Subject: [PATCH 07/23] clear the error var --- .../Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 3a415c59642..3e5e4e7da0a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -123,6 +123,7 @@ Describe 'Non-admin on Unix' { } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { + $error.Clear() { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" -Because (Get-Error) } } From 2553820c99278a4d34e075c95064a4692d51fc34 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 27 Jun 2023 09:45:59 -0700 Subject: [PATCH 08/23] explicitly use out-string for error --- .../Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 3e5e4e7da0a..e9ade453238 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -124,6 +124,6 @@ Describe 'Non-admin on Unix' { It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { $error.Clear() - { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" -Because (Get-Error) + { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" -Because (Get-Error | Out-String) } } From f9b68e472e6dee8c47b9dcf7dbf039b9d9e6855a Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 27 Jun 2023 13:26:53 -0700 Subject: [PATCH 09/23] add additional path to search for shutdown command, move error messages to resx --- .../commands/management/ComputerUnix.cs | 24 +++++++++++++++---- .../resources/ComputerResources.resx | 6 +++++ .../Restart-Computer.Tests.ps1 | 4 ---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 0dd4d64cdc4..b14c1f87c86 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; +using System.IO; using System.Management.Automation; using System.Management.Automation.Internal; using System.Runtime.InteropServices; @@ -36,7 +37,7 @@ protected override void BeginProcessing() { string errMsg = StringUtil.Format("Command returned 0x{0:X}", retVal); ErrorRecord error = new ErrorRecord( - new InvalidOperationException(errMsg), "Command Failed", ErrorCategory.OperationStopped, "localhost"); + new InvalidOperationException(errMsg), "CommandFailed", ErrorCategory.OperationStopped, "localhost"); WriteError(error); } return; @@ -78,7 +79,7 @@ protected override void BeginProcessing() { string errMsg = StringUtil.Format("Command returned 0x{0:X}", retVal); ErrorRecord error = new ErrorRecord( - new InvalidOperationException(errMsg), "Command Failed", ErrorCategory.OperationStopped, "localhost"); + new InvalidOperationException(errMsg), "CommandFailed", ErrorCategory.OperationStopped, "localhost"); WriteError(error); } return; @@ -154,11 +155,26 @@ protected override void StopProcessing() /// Run a command. /// protected void RunCommand(String command, String args) { + string shutdownPath = "/sbin/shutdown"; + const string altShutdownPath = "/usr/sbin/shutdown"; + + if (!File.Exists(shutdownPath) && File.Exists(altShutdownPath)) + { + shutdownPath = altShutdownPath; + } + else + { + ErrorRecord error = new ErrorRecord( + new InvalidOperationException(ComputerResources.ShutdownCommandNotFound), "CommandNotFound", ErrorCategory.ObjectNotFound, null); + WriteError(error); + return; + } + _process = new Process() { StartInfo = new ProcessStartInfo { - FileName = "/sbin/shutdown", + FileName = shutdownPath, Arguments = string.Empty, RedirectStandardOutput = false, RedirectStandardError = true, @@ -171,7 +187,7 @@ protected void RunCommand(String command, String args) { if (_process.ExitCode != 0) { string stderr = _process.StandardError.ReadToEnd(); - string errMsg = StringUtil.Format("Command returned 0x{0:X}: {1}", _process.ExitCode, stderr); + string errMsg = StringUtil.Format(ComputerResources.CommandFailed, _process.ExitCode, stderr); ErrorRecord error = new ErrorRecord( new InvalidOperationException(errMsg), "CommandFailed", ErrorCategory.OperationStopped, null); WriteError(error); diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx index 63a671c0248..9a2f14420a5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx @@ -387,4 +387,10 @@ The {0} parameter is not supported for CoreCLR. + + Command failed with exit code {0}: {1} + + + 'shutdown' command was not found and required. + diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index e9ade453238..b700b7a71ef 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -116,10 +116,6 @@ Describe 'Non-admin on Unix' { else { $IsWindowsOrSudo = $false } - - Write-Verbose -Verbose "user id: $(id -u)" - Write-Verbose -Verbose "group id: $(id -g)" - Write-Verbose -Verbose "IsWindowsOrSudo: $IsWindowsOrSudo" } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { From c4c8870eff19ed54ae28ef0a8c6ed7c5971ae275 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 27 Jun 2023 15:54:08 -0700 Subject: [PATCH 10/23] trace if shutdown is found --- .../Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index b700b7a71ef..a796abc9ee3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -116,6 +116,9 @@ Describe 'Non-admin on Unix' { else { $IsWindowsOrSudo = $false } + + $shutdown = Get-Command shutdown -ErrorAction Ignore + Write-Verbose "shutdown: $shutdown" -Verbose } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { From f78c1ee0001cdfcb45316d9702aeb567c48ae027 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 27 Jun 2023 21:25:47 -0700 Subject: [PATCH 11/23] add path to output --- .../Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index a796abc9ee3..28d313952e6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -118,7 +118,7 @@ Describe 'Non-admin on Unix' { } $shutdown = Get-Command shutdown -ErrorAction Ignore - Write-Verbose "shutdown: $shutdown" -Verbose + Write-Verbose "shutdown: $($shutdown.path)" -Verbose } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { From cd1937cea338a8d530c7a467cef7421c2b3259e9 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 1 Jul 2023 12:34:02 -0700 Subject: [PATCH 12/23] update RunCommand for shutdown, remove unneeded string --- .../commands/management/ComputerUnix.cs | 8 ++++---- .../resources/ConsoleHostStrings.resx | 3 --- .../Restart-Computer.Tests.ps1 | 6 +----- .../Stop-Computer.Tests.ps1 | 11 ++++++++++- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index b14c1f87c86..f586a0ebcad 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -43,7 +43,7 @@ protected override void BeginProcessing() return; } - RunCommand("/sbin/shutdown", "-r now"); + RunShutdown("-r now"); } #endregion "Overrides" } @@ -85,7 +85,7 @@ protected override void BeginProcessing() return; } - RunCommand("/sbin/shutdown", args); + RunShutdown(args); } #endregion "Overrides" } @@ -152,9 +152,9 @@ protected override void StopProcessing() #region "Internals" /// - /// Run a command. + /// Run shutdown command /// - protected void RunCommand(String command, String args) { + protected void RunShutdown(String args) { string shutdownPath = "/sbin/shutdown"; const string altShutdownPath = "/usr/sbin/shutdown"; diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/ConsoleHostStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/ConsoleHostStrings.resx index e8dcccb3dee..ce124ec084c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/ConsoleHostStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/ConsoleHostStrings.resx @@ -164,9 +164,6 @@ End time: {0:yyyyMMddHHmmss} {0}:{1,-3} {2} - - An error occurred while running '{0}': {1} - The current session does not support debugging; execution will continue. diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 28d313952e6..bd728f939bf 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -116,13 +116,9 @@ Describe 'Non-admin on Unix' { else { $IsWindowsOrSudo = $false } - - $shutdown = Get-Command shutdown -ErrorAction Ignore - Write-Verbose "shutdown: $($shutdown.path)" -Verbose } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { - $error.Clear() - { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" -Because (Get-Error | Out-String) + { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 index e3ccf4f1448..080fcb73e34 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 @@ -60,7 +60,16 @@ finally } Describe 'Non-admin on Unix' { - It 'Reports error if not run under sudo' -Skip:($IsWindows) { + BeforeAll { + if ($IsWindows -or (id -g) -eq 0) { + $IsWindowsOrSudo = $true + } + else { + $IsWindowsOrSudo = $false + } + } + + It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { { Stop-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.StopComputerCommand" } } From 72e4b991aa77b27d63995f55c5c1203600f858c7 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 10 Jul 2023 09:46:56 -0700 Subject: [PATCH 13/23] Update src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs Co-authored-by: Ilya --- .../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 f586a0ebcad..b0b0c80b75a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -152,7 +152,7 @@ protected override void StopProcessing() #region "Internals" /// - /// Run shutdown command + /// Run shutdown command. /// protected void RunShutdown(String args) { string shutdownPath = "/sbin/shutdown"; From 2426a18395683d9d9cac9017b8d4f6a6f4496d65 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 10 Jul 2023 10:10:13 -0700 Subject: [PATCH 14/23] Update src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs Co-authored-by: Ilya --- .../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 b0b0c80b75a..bed8c654c3a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -165,7 +165,7 @@ protected void RunShutdown(String args) { else { ErrorRecord error = new ErrorRecord( - new InvalidOperationException(ComputerResources.ShutdownCommandNotFound), "CommandNotFound", ErrorCategory.ObjectNotFound, null); + new InvalidOperationException(ComputerResources.ShutdownCommandNotFound), "CommandNotFound", ErrorCategory.ObjectNotFound, targetObject: null); WriteError(error); return; } From c8fe45555a56513a0c49c2601c5c656bc782156b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 10 Jul 2023 10:10:32 -0700 Subject: [PATCH 15/23] Update src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx Co-authored-by: Ilya --- .../resources/ComputerResources.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx index 9a2f14420a5..6787a671331 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx @@ -391,6 +391,6 @@ Command failed with exit code {0}: {1} - 'shutdown' command was not found and required. + Required 'shutdown' command was not found. From 63daba64703a6ca22eec5e582a84ec0da706c3d0 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 10 Jul 2023 10:17:16 -0700 Subject: [PATCH 16/23] address Ilya's feedback --- .../commands/management/ComputerUnix.cs | 5 +++-- .../Restart-Computer.Tests.ps1 | 2 +- .../Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 | 2 +- 3 files changed, 5 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 bed8c654c3a..4ba0bc7011c 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -151,12 +151,13 @@ protected override void StopProcessing() #region "Internals" + private static string shutdownPath = "/usr/sbin/shutdown"; + /// /// Run shutdown command. /// protected void RunShutdown(String args) { - string shutdownPath = "/sbin/shutdown"; - const string altShutdownPath = "/usr/sbin/shutdown"; + const string altShutdownPath = "/sbin/shutdown"; if (!File.Exists(shutdownPath) && File.Exists(altShutdownPath)) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index bd728f939bf..4dd4d3d8b7b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -110,7 +110,7 @@ finally Describe 'Non-admin on Unix' { BeforeAll { - if ($IsWindows -or (id -g) -eq 0) { + if ([environment]::IsPrivilegedProcess) { $IsWindowsOrSudo = $true } else { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 index 080fcb73e34..66889ccf5a4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 @@ -61,7 +61,7 @@ finally Describe 'Non-admin on Unix' { BeforeAll { - if ($IsWindows -or (id -g) -eq 0) { + if ([environment]::IsPrivilegedProcess) { $IsWindowsOrSudo = $true } else { From f9a04ba99891169f6b50ea979389c76660bced70 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 10 Jul 2023 15:12:54 -0700 Subject: [PATCH 17/23] change to using command discovery --- .../commands/management/ComputerUnix.cs | 33 +++++++++++-------- .../Restart-Computer.Tests.ps1 | 2 ++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 4ba0bc7011c..0890aaf2df4 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -10,6 +10,8 @@ using System.Management.Automation.Internal; using System.Runtime.InteropServices; +#nullable enable + namespace Microsoft.PowerShell.Commands { #region Restart-Computer @@ -96,7 +98,7 @@ protected override void BeginProcessing() public class CommandLineCmdletBase : PSCmdlet, IDisposable { #region Private Members - private Process _process = null; + private Process? _process = null; #endregion #region "IDisposable Members" @@ -151,24 +153,29 @@ protected override void StopProcessing() #region "Internals" - private static string shutdownPath = "/usr/sbin/shutdown"; + private static string? shutdownPath; /// /// Run shutdown command. /// protected void RunShutdown(String args) { - const string altShutdownPath = "/sbin/shutdown"; - - if (!File.Exists(shutdownPath) && File.Exists(altShutdownPath)) - { - shutdownPath = altShutdownPath; - } - else + if (shutdownPath is null) { - ErrorRecord error = new ErrorRecord( - new InvalidOperationException(ComputerResources.ShutdownCommandNotFound), "CommandNotFound", ErrorCategory.ObjectNotFound, targetObject: null); - WriteError(error); - return; + CommandInfo cmdinfo = CommandDiscovery.LookupCommandInfo( + "shutdown", CommandTypes.Application, + SearchResolutionOptions.None, CommandOrigin.Internal, this.Context); + + if (cmdinfo is not null) + { + shutdownPath = cmdinfo.Definition; + } + else + { + ErrorRecord error = new ErrorRecord( + new InvalidOperationException(ComputerResources.ShutdownCommandNotFound), "CommandNotFound", ErrorCategory.ObjectNotFound, targetObject: null); + WriteError(error); + return; + } } _process = new Process() diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 4dd4d3d8b7b..11ce28384e3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -116,6 +116,8 @@ Describe 'Non-admin on Unix' { else { $IsWindowsOrSudo = $false } + + Write-Verbose -Verbose (Get-Command shutdown | Format-List | Out-String) } It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { From f347ffb6b96bd85ad25b64a27ab1d4f2ef425d85 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 10 Jul 2023 15:43:51 -0700 Subject: [PATCH 18/23] skip test if shutdown command is not available --- .../Restart-Computer.Tests.ps1 | 12 ++++-------- .../Stop-Computer.Tests.ps1 | 13 ++++++------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 index 11ce28384e3..9f845f81af9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -110,17 +110,13 @@ finally Describe 'Non-admin on Unix' { BeforeAll { - if ([environment]::IsPrivilegedProcess) { - $IsWindowsOrSudo = $true + $skip = $false + if ($IsWindows -or [environment]::IsPrivilegedProcess -or ($null -eq (Get-Command shutdown -CommandType Application -ErrorAction Ignore))) { + $skip = $true } - else { - $IsWindowsOrSudo = $false - } - - Write-Verbose -Verbose (Get-Command shutdown | Format-List | Out-String) } - It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { + It 'Reports error if not run under sudo' -Skip:($skip) { { Restart-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.RestartComputerCommand" } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 index 66889ccf5a4..ad827609a5e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 @@ -61,15 +61,14 @@ finally Describe 'Non-admin on Unix' { BeforeAll { - if ([environment]::IsPrivilegedProcess) { - $IsWindowsOrSudo = $true - } - else { - $IsWindowsOrSudo = $false + BeforeAll { + $skip = $false + if ($IsWindows -or [environment]::IsPrivilegedProcess -or ($null -eq (Get-Command shutdown -CommandType Application -ErrorAction Ignore))) { + $skip = $true + } } - } - It 'Reports error if not run under sudo' -Skip:($IsWindowsOrSudo) { + It 'Reports error if not run under sudo' -Skip:($skip) { { Stop-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.StopComputerCommand" } } From 6fd2b6073dc3ce9b41ff6476ad7d94bc6352fe31 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 10 Jul 2023 16:02:38 -0700 Subject: [PATCH 19/23] fix test --- .../Stop-Computer.Tests.ps1 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 index ad827609a5e..1bda1471e78 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 @@ -61,12 +61,11 @@ finally Describe 'Non-admin on Unix' { BeforeAll { - BeforeAll { - $skip = $false - if ($IsWindows -or [environment]::IsPrivilegedProcess -or ($null -eq (Get-Command shutdown -CommandType Application -ErrorAction Ignore))) { - $skip = $true - } + $skip = $false + if ($IsWindows -or [environment]::IsPrivilegedProcess -or ($null -eq (Get-Command shutdown -CommandType Application -ErrorAction Ignore))) { + $skip = $true } + } It 'Reports error if not run under sudo' -Skip:($skip) { { Stop-Computer -ErrorAction Stop } | Should -Throw -ErrorId "CommandFailed,Microsoft.PowerShell.Commands.StopComputerCommand" From 69f4c0750692fd600d910eea4bc893f1b4350a40 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 17 Jul 2023 10:17:51 -0700 Subject: [PATCH 20/23] Update src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs Co-authored-by: Dongbo Wang --- .../commands/management/ComputerUnix.cs | 3 ++- 1 file changed, 2 insertions(+), 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 0890aaf2df4..4c694653905 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -158,7 +158,8 @@ protected override void StopProcessing() /// /// Run shutdown command. /// - protected void RunShutdown(String args) { + protected void RunShutdown(String args) + { if (shutdownPath is null) { CommandInfo cmdinfo = CommandDiscovery.LookupCommandInfo( From 702093c8bc452ed2d60ebf2e418da2969a7d2be6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 17 Jul 2023 10:18:54 -0700 Subject: [PATCH 21/23] Update src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx Co-authored-by: Dongbo Wang --- .../resources/ComputerResources.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx index 6787a671331..9f543005617 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx @@ -391,6 +391,6 @@ Command failed with exit code {0}: {1} - Required 'shutdown' command was not found. + The required native command 'shutdown' was not found. From 95594d790eb8b7db6992135a446ddef6a60016fc Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 17 Jul 2023 11:30:43 -0700 Subject: [PATCH 22/23] address Dongbo's feedback --- .../commands/management/ComputerUnix.cs | 7 +++---- .../resources/ComputerResources.resx | 3 --- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 4c694653905..dfb0ccdaf9e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -174,7 +174,7 @@ protected void RunShutdown(String args) { ErrorRecord error = new ErrorRecord( new InvalidOperationException(ComputerResources.ShutdownCommandNotFound), "CommandNotFound", ErrorCategory.ObjectNotFound, targetObject: null); - WriteError(error); + ThrowTerminatingError(error); return; } } @@ -196,10 +196,9 @@ protected void RunShutdown(String args) if (_process.ExitCode != 0) { string stderr = _process.StandardError.ReadToEnd(); - string errMsg = StringUtil.Format(ComputerResources.CommandFailed, _process.ExitCode, stderr); ErrorRecord error = new ErrorRecord( - new InvalidOperationException(errMsg), "CommandFailed", ErrorCategory.OperationStopped, null); - WriteError(error); + new InvalidOperationException(stderr), "CommandFailed", ErrorCategory.OperationStopped, null); + ThrowTerminatingError(error); } } #endregion diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx index 9f543005617..95685239fd7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/ComputerResources.resx @@ -387,9 +387,6 @@ The {0} parameter is not supported for CoreCLR. - - Command failed with exit code {0}: {1} - The required native command 'shutdown' was not found. From 57808f5507fe075266907cb42399ba96a50eb8a2 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 17 Jul 2023 11:54:01 -0700 Subject: [PATCH 23/23] Update src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs --- .../commands/management/ComputerUnix.cs | 1 - 1 file changed, 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 dfb0ccdaf9e..eb286f7c190 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -175,7 +175,6 @@ protected void RunShutdown(String args) ErrorRecord error = new ErrorRecord( new InvalidOperationException(ComputerResources.ShutdownCommandNotFound), "CommandNotFound", ErrorCategory.ObjectNotFound, targetObject: null); ThrowTerminatingError(error); - return; } }