Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1181,11 +1181,7 @@ public sealed class StopComputerCommand : PSCmdlet, IDisposable
/// </summary>
public void Dispose()
{
try
{
_cancel.Dispose();
}
catch (ObjectDisposedException) { }
_cancel.Dispose();
}

#endregion "IDisposable Members"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ CmdletsToExport=@("Add-Content",
"Resolve-Path",
"Set-Content",
"Set-ItemProperty",
"Get-TimeZone")
"Get-TimeZone",
"Stop-Computer")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Describe "Unimplemented Management Cmdlet Tests" -Tags "CI" {
"New-Service",

"Restart-Computer",
"Stop-Computer",
"Rename-Computer",

"Get-ComputerInfo",
Expand Down
2 changes: 1 addition & 1 deletion test/powershell/engine/Basic/DefaultCommands.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down