Add an implementation of Stop-Computer for Linux and macOS (#11151)

This commit is contained in:
Brendan Burns 2019-12-02 17:29:11 -08:00 committed by Travis Plunk
parent 570ba43a24
commit d38541fdce
5 changed files with 107 additions and 8 deletions

View file

@ -1181,11 +1181,7 @@ $result
/// </summary>
public void Dispose()
{
try
{
_cancel.Dispose();
}
catch (ObjectDisposedException) { }
_cancel.Dispose();
}
#endregion "IDisposable Members"

View file

@ -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
/// <summary>
/// Cmdlet to stop computer.
/// </summary>
[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"
/// <summary>
/// Dispose Method.
/// </summary>
public void Dispose()
{
_process.Dispose();
}
#endregion "IDisposable Members"
#region "Overrides"
/// <summary>
/// BeginProcessing.
/// </summary>
protected override void BeginProcessing()
{
doShutdown();
}
/// <summary>
/// To implement ^C.
/// </summary>
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

View file

@ -54,5 +54,6 @@ CmdletsToExport=@("Add-Content",
"Resolve-Path",
"Set-Content",
"Set-ItemProperty",
"Get-TimeZone")
"Get-TimeZone",
"Stop-Computer")
}

View file

@ -13,7 +13,6 @@ Describe "Unimplemented Management Cmdlet Tests" -Tags "CI" {
"New-Service",
"Restart-Computer",
"Stop-Computer",
"Rename-Computer",
"Get-ComputerInfo",

View file

@ -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"