diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs index ef152c642..64d0c82a4 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs @@ -1181,11 +1181,7 @@ $result /// public void Dispose() { - try - { - _cancel.Dispose(); - } - catch (ObjectDisposedException) { } + _cancel.Dispose(); } #endregion "IDisposable Members" diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs new file mode 100644 index 000000000..2aca2c0b1 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#if UNIX + +using System; +using System.Diagnostics; +using System.Management.Automation; +using System.Runtime.InteropServices; + +namespace Microsoft.PowerShell.Commands +{ + #region Stop-Computer + + /// + /// Cmdlet to stop computer. + /// + [Cmdlet(VerbsLifecycle.Stop, "Computer", SupportsShouldProcess = true, + HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135263", RemotingCapability = RemotingCapability.SupportedByCommand)] + public sealed class StopComputerCommand : PSCmdlet, IDisposable + { + #region Private Members + + private Process _process = null; + + #endregion + + // TODO: Support remote computers? + + #region "IDisposable Members" + + /// + /// Dispose Method. + /// + public void Dispose() + { + _process.Dispose(); + } + + #endregion "IDisposable Members" + + #region "Overrides" + + /// + /// BeginProcessing. + /// + protected override void BeginProcessing() + { + doShutdown(); + } + + /// + /// To implement ^C. + /// + protected override void StopProcessing() + { + if (_process == null) { + return; + } + + try { + if (!_process.HasExited) { + _process.Kill(); + } + WriteObject(_process.ExitCode); + } + catch (InvalidOperationException) {} + catch (NotSupportedException) {} + } + + #endregion "Overrides" + + #region "Internals" + + private void doShutdown() { + String cmd = ""; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + cmd = "-P now"; + } + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + cmd = "now"; + } + + _process = new Process() + { + StartInfo = new ProcessStartInfo + { + FileName = "/sbin/shutdown", + Arguments = cmd, + RedirectStandardOutput = false, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + _process.Start(); + } + #endregion + } + #endregion +} +#endif 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 547358125..cda492f33 100644 --- a/src/Modules/Unix/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 +++ b/src/Modules/Unix/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 @@ -54,5 +54,6 @@ CmdletsToExport=@("Add-Content", "Resolve-Path", "Set-Content", "Set-ItemProperty", - "Get-TimeZone") + "Get-TimeZone", + "Stop-Computer") } 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 4b27e37ba..4e1e3be9f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Unimplemented-Cmdlet.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Unimplemented-Cmdlet.Tests.ps1 @@ -13,7 +13,6 @@ Describe "Unimplemented Management Cmdlet Tests" -Tags "CI" { "New-Service", "Restart-Computer", - "Stop-Computer", "Rename-Computer", "Get-ComputerInfo", diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index 8e1bffaf4..8d1e285cd 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -451,7 +451,7 @@ Describe "Verify approved aliases list" -Tags "CI" { "Cmdlet", "Start-Sleep", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Start-Transaction", "", $($FullCLR ), "", "", "" "Cmdlet", "Start-Transcript", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" -"Cmdlet", "Stop-Computer", "", $($FullCLR -or $CoreWindows ), "", "", "Medium" +"Cmdlet", "Stop-Computer", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium" "Cmdlet", "Stop-Job", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium" "Cmdlet", "Stop-Process", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium" "Cmdlet", "Stop-Service", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"