diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index eb2cca3242f..f0254091348 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -6,10 +6,48 @@ using System; using System.Diagnostics; using System.Management.Automation; +using System.Management.Automation.Internal; using System.Runtime.InteropServices; namespace Microsoft.PowerShell.Commands { +#region Restart-Computer + + /// + /// Cmdlet to restart computer. + /// + [Cmdlet(VerbsLifecycle.Restart, "Computer", SupportsShouldProcess = true, + HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097060", RemotingCapability = RemotingCapability.SupportedByCommand)] + public sealed class RestartComputerCommand : CommandLineCmdletBase + { + // TODO: Support remote computers? + +#region "Overrides" + + /// + /// BeginProcessing. + /// + protected override void BeginProcessing() + { + if (InternalTestHooks.TestStopComputer) + { + var retVal = InternalTestHooks.TestStopComputerResults; + if (retVal != 0) + { + string errMsg = StringUtil.Format("Command returned 0x{0:X}", retVal); + ErrorRecord error = new ErrorRecord( + new InvalidOperationException(errMsg), "Command Failed", ErrorCategory.OperationStopped, "localhost"); + WriteError(error); + } + return; + } + + RunCommand("/sbin/shutdown", "-r now"); + } +#endregion "Overrides" + } +#endregion Restart-Computer + #region Stop-Computer /// @@ -17,15 +55,48 @@ namespace Microsoft.PowerShell.Commands /// [Cmdlet(VerbsLifecycle.Stop, "Computer", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097151", RemotingCapability = RemotingCapability.SupportedByCommand)] - public sealed class StopComputerCommand : PSCmdlet, IDisposable + public sealed class StopComputerCommand : CommandLineCmdletBase { -#region Private Members + // TODO: Support remote computers? - private Process _process = null; +#region "Overrides" -#endregion + /// + /// BeginProcessing. + /// + protected override void BeginProcessing() + { + var args = "-P now"; + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + args = "now"; + } + if (InternalTestHooks.TestStopComputer) + { + var retVal = InternalTestHooks.TestStopComputerResults; + if (retVal != 0) + { + string errMsg = StringUtil.Format("Command returned 0x{0:X}", retVal); + ErrorRecord error = new ErrorRecord( + new InvalidOperationException(errMsg), "Command Failed", ErrorCategory.OperationStopped, "localhost"); + WriteError(error); + } + return; + } - // TODO: Support remote computers? + RunCommand("/sbin/shutdown", args); + } +#endregion "Overrides" + } + + /// + /// A base class for cmdlets that can run shell commands. + /// + public class CommandLineCmdletBase : PSCmdlet, IDisposable + { +#region Private Members + private Process _process = null; +#endregion #region "IDisposable Members" @@ -40,15 +111,6 @@ public void Dispose() #endregion "IDisposable Members" #region "Overrides" - - /// - /// BeginProcessing. - /// - protected override void BeginProcessing() - { - doShutdown(); - } - /// /// To implement ^C. /// @@ -67,21 +129,15 @@ protected override void StopProcessing() catch (InvalidOperationException) {} catch (NotSupportedException) {} } - #endregion "Overrides" #region "Internals" - private void doShutdown() { + /// + /// Run a command. + /// + protected void RunCommand(String command, String args) { String cmd = ""; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - cmd = "-P now"; - } - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - cmd = "now"; - } _process = new Process() { diff --git a/src/Modules/Unix/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 b/src/Modules/Unix/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 index 963b2e6b303..e69dfe1a0d6 100644 --- a/src/Modules/Unix/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 +++ b/src/Modules/Unix/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 @@ -55,5 +55,6 @@ CmdletsToExport=@("Add-Content", "Set-Content", "Set-ItemProperty", "Get-TimeZone", - "Stop-Computer") + "Stop-Computer", + "Restart-Computer") } 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 f143a9d6868..8da605bd8c3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1 @@ -9,7 +9,6 @@ $DefaultResultValue = 0 try { # set up for testing - $PSDefaultParameterValues["it:skip"] = ! $IsWindows Enable-Testhook -testhookName $restartTesthookName Describe "Restart-Computer" -Tag Feature,RequireAdminOnWindows { @@ -29,13 +28,13 @@ try Restart-Computer -ErrorAction Stop | Should -BeNullOrEmpty } - It "Should support -computer parameter" { + It "Should support -computer parameter" -Skip:(!$IsWindows) { Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue $computerNames = "localhost","${env:COMPUTERNAME}" Restart-Computer -Computer $computerNames -ErrorAction Stop | Should -BeNullOrEmpty } - It "Should support WsmanAuthentication types" { + It "Should support WsmanAuthentication types" -Skip:(!$IsWindows) { $authChoices = "Default","Basic","Negotiate","CredSSP","Digest","Kerberos" foreach ( $auth in $authChoices ) { Restart-Computer -WsmanAuthentication $auth | Should -BeNullOrEmpty @@ -45,7 +44,7 @@ try # this requires setting a test hook, so we wrap the execution with try/finally of the # set operation. Internally, we want to suppress the progress, so # that is also wrapped in try/finally - It "Should wait for a remote system" { + It "Should wait for a remote system" -Skip:(!$IsWindows) { try { Enable-Testhook -testhookname TestWaitStopComputer @@ -77,16 +76,24 @@ try $RestartError.Exception.Message | Should -Match 0x300000 } - It "Should produce an error when 'Delay' is specified" { + It "Should produce an error when 'Delay' is specified" -Skip:(!$IsWindows) { { Restart-Computer -Delay 30 } | Should -Throw -ErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand" } - It "Should not support timeout on localhost" { + It "Should not support timeout on Unix" -Skip:($IsWindows) { + { Restart-Computer -timeout 3 -ErrorAction Stop } | Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.RestartComputerCommand" + } + + It "Should not support Delay on Unix" -Skip:($IsWindows) { + { Restart-Computer -Delay 30 } | Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.RestartComputerCommand" + } + + It "Should not support timeout on localhost" -Skip:(!$IsWindows) { Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue { Restart-Computer -timeout 3 -ErrorAction Stop } | Should -Throw -ErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand" } - It "Should not support timeout on localhost" { + It "Should not support timeout on localhost" -Skip:(!$IsWindows) { Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue { Restart-Computer -timeout 3 -ErrorAction Stop } | Should -Throw -ErrorId "RestartComputerInvalidParameter,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 1555e0f4357..ff611a0b44d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Stop-Computer.Tests.ps1 @@ -10,7 +10,6 @@ $DefaultResultValue = 0 try { # set up for testing - $PSDefaultParameterValues["it:skip"] = ! $IsWindows Enable-Testhook -testhookName $stopTesthook Describe "Stop-Computer" -Tag Feature { @@ -30,13 +29,13 @@ try Stop-Computer -ErrorAction Stop | Should -BeNullOrEmpty } - It "Should support -Computer parameter" { + It "Should support -Computer parameter" -Skip:(!$IsWindows) { Set-TesthookResult -testhookName $stopTesthookResultName -Value $defaultResultValue $computerNames = "localhost","${env:COMPUTERNAME}" Stop-Computer -Computer $computerNames -ErrorAction Stop | Should -BeNullOrEmpty } - It "Should support WsmanAuthentication types" { + It "Should support WsmanAuthentication types" -Skip:(!$IsWindows) { $authChoices = "Default","Basic","Negotiate","CredSSP","Digest","Kerberos" foreach ( $auth in $authChoices ) { Stop-Computer -WsmanAuthentication $auth | Should -BeNullOrEmpty diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Unimplemented-Cmdlet.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Unimplemented-Cmdlet.Tests.ps1 index 4e1e3be9fce..4bb1d58c7ee 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Unimplemented-Cmdlet.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Unimplemented-Cmdlet.Tests.ps1 @@ -12,7 +12,6 @@ Describe "Unimplemented Management Cmdlet Tests" -Tags "CI" { "Set-Service", "New-Service", - "Restart-Computer", "Rename-Computer", "Get-ComputerInfo", diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index f11e6abd911..922a123b3c6 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -407,7 +407,7 @@ Describe "Verify approved aliases list" -Tags "CI" { "Cmdlet", "Rename-ItemProperty", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium" "Cmdlet", "Reset-ComputerMachinePassword", "", $($FullCLR ), "", "", "" "Cmdlet", "Resolve-Path", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" -"Cmdlet", "Restart-Computer", "", $($FullCLR -or $CoreWindows ), "", "", "Medium" +"Cmdlet", "Restart-Computer", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium" "Cmdlet", "Restart-Service", "", $($FullCLR -or $CoreWindows ), "", "", "Medium" "Cmdlet", "Restore-Computer", "", $($FullCLR ), "", "", "" "Cmdlet", "Resume-Job", "", $($FullCLR ), "", "", ""