Skip to content

Commit d38541f

Browse files
brendandburnsTravisEz13
authored andcommitted
Add an implementation of Stop-Computer for Linux and macOS (PowerShell#11151)
1 parent 570ba43 commit d38541f

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1181,11 +1181,7 @@ public sealed class StopComputerCommand : PSCmdlet, IDisposable
11811181
/// </summary>
11821182
public void Dispose()
11831183
{
1184-
try
1185-
{
1186-
_cancel.Dispose();
1187-
}
1188-
catch (ObjectDisposedException) { }
1184+
_cancel.Dispose();
11891185
}
11901186

11911187
#endregion "IDisposable Members"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#if UNIX
5+
6+
using System;
7+
using System.Diagnostics;
8+
using System.Management.Automation;
9+
using System.Runtime.InteropServices;
10+
11+
namespace Microsoft.PowerShell.Commands
12+
{
13+
#region Stop-Computer
14+
15+
/// <summary>
16+
/// Cmdlet to stop computer.
17+
/// </summary>
18+
[Cmdlet(VerbsLifecycle.Stop, "Computer", SupportsShouldProcess = true,
19+
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135263", RemotingCapability = RemotingCapability.SupportedByCommand)]
20+
public sealed class StopComputerCommand : PSCmdlet, IDisposable
21+
{
22+
#region Private Members
23+
24+
private Process _process = null;
25+
26+
#endregion
27+
28+
// TODO: Support remote computers?
29+
30+
#region "IDisposable Members"
31+
32+
/// <summary>
33+
/// Dispose Method.
34+
/// </summary>
35+
public void Dispose()
36+
{
37+
_process.Dispose();
38+
}
39+
40+
#endregion "IDisposable Members"
41+
42+
#region "Overrides"
43+
44+
/// <summary>
45+
/// BeginProcessing.
46+
/// </summary>
47+
protected override void BeginProcessing()
48+
{
49+
doShutdown();
50+
}
51+
52+
/// <summary>
53+
/// To implement ^C.
54+
/// </summary>
55+
protected override void StopProcessing()
56+
{
57+
if (_process == null) {
58+
return;
59+
}
60+
61+
try {
62+
if (!_process.HasExited) {
63+
_process.Kill();
64+
}
65+
WriteObject(_process.ExitCode);
66+
}
67+
catch (InvalidOperationException) {}
68+
catch (NotSupportedException) {}
69+
}
70+
71+
#endregion "Overrides"
72+
73+
#region "Internals"
74+
75+
private void doShutdown() {
76+
String cmd = "";
77+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
78+
{
79+
cmd = "-P now";
80+
}
81+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
82+
{
83+
cmd = "now";
84+
}
85+
86+
_process = new Process()
87+
{
88+
StartInfo = new ProcessStartInfo
89+
{
90+
FileName = "/sbin/shutdown",
91+
Arguments = cmd,
92+
RedirectStandardOutput = false,
93+
UseShellExecute = false,
94+
CreateNoWindow = true,
95+
}
96+
};
97+
_process.Start();
98+
}
99+
#endregion
100+
}
101+
#endregion
102+
}
103+
#endif

src/Modules/Unix/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ CmdletsToExport=@("Add-Content",
5454
"Resolve-Path",
5555
"Set-Content",
5656
"Set-ItemProperty",
57-
"Get-TimeZone")
57+
"Get-TimeZone",
58+
"Stop-Computer")
5859
}

test/powershell/Modules/Microsoft.PowerShell.Management/Unimplemented-Cmdlet.Tests.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Describe "Unimplemented Management Cmdlet Tests" -Tags "CI" {
1313
"New-Service",
1414

1515
"Restart-Computer",
16-
"Stop-Computer",
1716
"Rename-Computer",
1817

1918
"Get-ComputerInfo",

test/powershell/engine/Basic/DefaultCommands.Tests.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
451451
"Cmdlet", "Start-Sleep", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
452452
"Cmdlet", "Start-Transaction", "", $($FullCLR ), "", "", ""
453453
"Cmdlet", "Start-Transcript", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
454-
"Cmdlet", "Stop-Computer", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
454+
"Cmdlet", "Stop-Computer", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
455455
"Cmdlet", "Stop-Job", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
456456
"Cmdlet", "Stop-Process", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
457457
"Cmdlet", "Stop-Service", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"

0 commit comments

Comments
 (0)