// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if UNIX
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
///
/// Cmdlet to stop computer.
///
[Cmdlet(VerbsLifecycle.Stop, "Computer", SupportsShouldProcess = true,
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097151", RemotingCapability = RemotingCapability.SupportedByCommand)]
public sealed class StopComputerCommand : CommandLineCmdletBase
{
// TODO: Support remote computers?
#region "Overrides"
///
/// 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;
}
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"
///
/// Dispose Method.
///
public void Dispose()
{
_process.Dispose();
}
#endregion "IDisposable Members"
#region "Overrides"
///
/// 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"
///
/// Run a command.
///
protected void RunCommand(String command, String args) {
String cmd = "";
_process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/sbin/shutdown",
Arguments = cmd,
RedirectStandardOutput = false,
UseShellExecute = false,
CreateNoWindow = true,
}
};
_process.Start();
}
#endregion
}
#endregion
}
#endif