Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
721ae5b
changes for reason, comment and delay
kvprasoon Jul 21, 2026
4a64862
correct referring to old review comments
kvprasoon Jul 23, 2026
d94ca1c
add pester tests
kvprasoon Jul 23, 2026
5af4021
address copilot findings
kvprasoon Aug 1, 2026
4f7c00b
Merge branch 'master' of https://github.com/powershell/powershell int…
kvprasoon Aug 1, 2026
9e40b04
address codefactor issues
kvprasoon Aug 1, 2026
efb734e
address remaining codefactor issues
kvprasoon Aug 1, 2026
46bc9a3
Correct prperty documentation summary text issue
kvprasoon Aug 1, 2026
b3135fc
Address review comments
kvprasoon Aug 3, 2026
fd4a893
cleanup and address copilot comments
kvprasoon Aug 7, 2026
949fb79
Merge branch 'master' of https://github.com/powershell/powershell int…
kvprasoon Sep 1, 2026
2f25643
Apply suggestion from @iSazonov
kvprasoon Jun 23, 2026
4348158
changes for reason, comment and delay
kvprasoon Jul 21, 2026
4f08ada
correct referring to old review comments
kvprasoon Jul 23, 2026
96ba80a
add pester tests
kvprasoon Jul 23, 2026
e0f852e
address copilot findings
kvprasoon Aug 1, 2026
674d3fc
address codefactor issues
kvprasoon Aug 1, 2026
325805f
address remaining codefactor issues
kvprasoon Aug 1, 2026
f32079b
Correct prperty documentation summary text issue
kvprasoon Aug 1, 2026
0a31151
Address review comments
kvprasoon Aug 3, 2026
5892d18
cleanup and address copilot comments
kvprasoon Aug 7, 2026
88b0eb3
Apply suggestion from @iSazonov
kvprasoon Jun 23, 2026
18ccc77
use proper reason in tests
kvprasoon Sep 2, 2026
fbcc3ac
address copilot comments
kvprasoon Sep 2, 2026
4b5834b
address copilot review comments
kvprasoon Sep 2, 2026
f784999
address missing copilot review comments
kvprasoon Sep 2, 2026
f84a12e
address copilot suggestion
kvprasoon Sep 2, 2026
1e72cc7
correct casing
kvprasoon Sep 2, 2026
98a54f5
address copilot suggestion
kvprasoon Sep 2, 2026
dbb507f
address copilot suggestion
kvprasoon Sep 2, 2026
083a034
cosmetic suggestion from copilot
kvprasoon Sep 2, 2026
3f0a1eb
address copilot minor review comments
kvprasoon Sep 2, 2026
02917d7
delay matching max timeout, enum rename
kvprasoon Sep 3, 2026
691c9f6
set max chat size to 512 to comply with Win32ShutdownTracker comment …
kvprasoon Sep 3, 2026
e4296d5
set max char size to 512 to comply with Win32ShutdownTracker comment …
kvprasoon Sep 3, 2026
3fe3bf3
Merge branch 'feat/stopmessage-4857' of https://github.com/kvprasoon/…
kvprasoon Sep 3, 2026
a5f6a10
Merge branch 'master' into feat/stopmessage-4857
kvprasoon Sep 3, 2026
26b5996
correct casing for SystemBlueScreen
kvprasoon Sep 3, 2026
a6cd09b
Merge branch 'feat/stopmessage-4857' of https://github.com/kvprasoon/…
kvprasoon Sep 3, 2026
e664d58
fix codefactor issues
kvprasoon Sep 4, 2026
55f95ef
fix another codefactor issue
kvprasoon Sep 4, 2026
78f23ab
Merge branch 'master' into feat/stopmessage-4857
kvprasoon Nov 15, 2026
6fcd097
Merge branch 'master' into feat/stopmessage-4857
kvprasoon Nov 18, 2026
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 @@ -28,6 +28,164 @@ namespace Microsoft.PowerShell.Commands
{
#region Restart-Computer

/// <summary>
/// Supported ShutdownReason for restarting or shutting down.
/// </summary>
public enum ShutdownReason : uint
{
/// <summary>
/// ApplicationHung related shutdown/restart.
/// </summary>
ApplicationHung = 0x00040000 | 0x00000005,

/// <summary>
/// ApplicationInstallationPlanned related shutdown/restart.
/// </summary>
ApplicationInstallationPlanned = 0x00040000 | 0x00000002 | 0x80000000,

/// <summary>
/// ApplicationMaintenance related shutdown/restart.
/// </summary>
ApplicationMaintenance = 0x00040000 | 0x00000001,

/// <summary>
/// ApplicationMaintenancePlanned related shutdown/restart.
/// </summary>
ApplicationMaintenancePlanned = 0x00040000 | 0x00000001 | 0x80000000,

/// <summary>
/// ApplicationUnstable related shutdown/restart.
/// </summary>
ApplicationUnstable = 0x00040000 | 0x00000006,

/// <summary>
/// HardwareInstallation related shutdown/restart.
/// </summary>
HardwareInstallation = 0x00010000 | 0x00000002,

/// <summary>
/// HardwareInstallationPlanned related shutdown/restart.
/// </summary>
HardwareInstallationPlanned = 0x00010000 | 0x00000002 | 0x80000000,

/// <summary>
/// HardwareMaintenance related shutdown/restart.
/// </summary>
HardwareMaintenance = 0x00010000 | 0x00000001,

/// <summary>
/// HardwareMaintenancePlanned related shutdown/restart.
/// </summary>
HardwareMaintenancePlanned = 0x00010000 | 0x00000001 | 0x80000000,

/// <summary>
/// LegacyApi related shutdown/restart.
/// </summary>
LegacyApi = 0x00070000,

/// <summary>
/// OperatingSystemHotfix related shutdown/restart.
/// </summary>
OperatingSystemHotfix = 0x00020000 | 0x00000011,

/// <summary>
/// OperatingSystemHotfixPlanned related shutdown/restart.
/// </summary>
OperatingSystemHotfixPlanned = 0x00020000 | 0x00000011 | 0x80000000,

/// <summary>
/// OperatingSystemReconfig related shutdown/restart.
/// </summary>
OperatingSystemReconfig = 0x00020000 | 0x00000004,

/// <summary>
/// OperatingSystemReconfigPlanned related shutdown/restart.
/// </summary>
OperatingSystemReconfigPlanned = 0x00020000 | 0x00000004 | 0x80000000,

/// <summary>
/// OperatingSystemSecurityFix related shutdown/restart.
/// </summary>
OperatingSystemSecurityFix = 0x00020000 | 0x00000012,

/// <summary>
/// OperatingSystemSecurityFixPlanned related shutdown/restart.
/// </summary>
OperatingSystemSecurityFixPlanned = 0x00020000 | 0x00000012 | 0x80000000,

/// <summary>
/// OperatingSystemServicePackPlanned related shutdown/restart.
/// </summary>
OperatingSystemServicePackPlanned = 0x00020000 | 0x00000010 | 0x80000000,

/// <summary>
/// OperatingsystemUpgradePlanned related shutdown/restart.
/// </summary>
OperatingSystemUpgradePlanned = 0x00020000 | 0x00000003 | 0x80000000,

/// <summary>
/// Other related shutdown/restart.
/// </summary>
Other = 0x00000000,

/// <summary>
/// OtherPlanned related shutdown/restart.
/// </summary>
OtherPlanned = 0x00000000 | 0x80000000,

/// <summary>
/// OtherHung related shutdown/restart.
/// </summary>
OtherHung = 0x00000000 | 0x00000005,

/// <summary>
/// PowerCordUnplugged related shutdown/restart.
/// </summary>
PowerCordUnplugged = 0x00060000 | 0x0000000b,

/// <summary>
/// PowerEnvironment related shutdown/restart.
/// </summary>
PowerEnvironment = 0x00060000 | 0x0000000c,

/// <summary>
/// SystemBlueScreen related shutdown/restart.
/// </summary>
SystemBlueScreen = 0x00050000 | 0x0000000F,

/// <summary>
/// SystemNetworkConnectivity related shutdown/restart.
/// </summary>
SystemNetworkConnectivity = 0x00050000 | 0x00000014,

/// <summary>
/// SystemSecurity related shutdown/restart.
/// </summary>
SystemSecurity = 0x00050000 | 0x00000013,
}

/// <summary>
/// Defines the services that Restart-Computer can wait on.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1027:MarkEnumsWithFlags")]
public enum WaitForServiceTypes
{
/// <summary>
/// Wait for the WMI service to be ready.
/// </summary>
Wmi = 0x0,

/// <summary>
/// Wait for the WinRM service to be ready.
/// </summary>
WinRM = 0x1,

/// <summary>
/// Wait for the PowerShell to be ready.
/// </summary>
PowerShell = 0x2,
}

/// <summary>
/// This exception is thrown when the timeout expires before a computer finishes restarting.
/// </summary>
Expand Down Expand Up @@ -84,28 +242,6 @@ public RestartComputerTimeoutException(string message) : base(message) { }
public RestartComputerTimeoutException(string message, Exception innerException) : base(message, innerException) { }
}

/// <summary>
/// Defines the services that Restart-Computer can wait on.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1027:MarkEnumsWithFlags")]
public enum WaitForServiceTypes
{
/// <summary>
/// Wait for the WMI service to be ready.
/// </summary>
Wmi = 0x0,

/// <summary>
/// Wait for the WinRM service to be ready.
/// </summary>
WinRM = 0x1,

/// <summary>
/// Wait for the PowerShell to be ready.
/// </summary>
PowerShell = 0x2,
}

/// <summary>
/// Restarts the computer.
/// </summary>
Expand Down Expand Up @@ -160,6 +296,20 @@ public class RestartComputerCommand : PSCmdlet, IDisposable
[Credential]
public PSCredential Credential { get; set; }

/// <summary>
/// Gets or sets the comment for rebooting the computer.
/// </summary>
[Parameter]
[ValidateNotNull]
[ValidateLength(0, 512)]
public string Comment { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the reason for rebooting the computer.
/// </summary>
[Parameter]
public ShutdownReason Reason { get; set; } = ShutdownReason.Other;

/// <summary>
/// Using Force in conjunction with Reboot on a
/// remote computer immediately reboots the remote computer.
Expand All @@ -168,6 +318,14 @@ public class RestartComputerCommand : PSCmdlet, IDisposable
[Alias("f")]
public SwitchParameter Force { get; set; }

/// <summary>
/// Gets or sets the delay before the reboot occurs.
/// MAX_SHUTDOWN_TIMEOUT supported by Win32ShutdownTracker is 315360000.
/// </summary>
[Parameter(ParameterSetName = DefaultParameterSet)]
[ValidateRange(0, 315360000)]
public uint RestartDelaySec { get; set; }

/// <summary>
/// Specify the Wait parameter. Prompt will be blocked is the Timeout is not 0.
/// </summary>
Expand Down Expand Up @@ -786,10 +944,11 @@ protected override void ProcessRecord()
// Validate parameters
ValidateComputerNames();

object[] flags = new object[] { 2, 0 };
object[] flags = new object[] { RestartDelaySec, Comment, (uint)Reason, 2 };

if (Force)
{
flags[0] = forcedReboot;
flags[3] = forcedReboot;
}

if (ParameterSetName.Equals(DefaultParameterSet, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -829,7 +988,7 @@ protected override void ProcessRecord()
}

bool isSuccess =
ComputerWMIHelper.InvokeWin32ShutdownUsingWsman(this, isLocal, compname, flags, Credential, WsmanAuthentication, ComputerResources.RestartcomputerFailed, "RestartcomputerFailed", _cancel.Token);
ComputerWMIHelper.InvokeWin32ShutdownTrackerUsingWsman(this, isLocal, compname, flags, Credential, WsmanAuthentication, ComputerResources.RestartcomputerFailed, "RestartcomputerFailed", _cancel.Token);

if (isSuccess && Wait && _timeout != 0)
{
Expand Down Expand Up @@ -1169,6 +1328,28 @@ public sealed class StopComputerCommand : PSCmdlet, IDisposable
[Credential]
public PSCredential Credential { get; set; }

/// <summary>
/// Gets or sets the delay before the shutdown.
/// MAX_SHUTDOWN_TIMEOUT supported by Win32ShutdownTracker is 315360000.
/// </summary>
[Parameter]
[ValidateRange(0, 315360000)]
public uint ShutdownDelaySec { get; set; }

/// <summary>
/// Gets or sets the comment for shutting down the computer.
/// </summary>
[Parameter]
[ValidateNotNull]
[ValidateLength(0, 512)]
public string Comment { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the reason for shutting down the computer.
/// </summary>
[Parameter]
public ShutdownReason Reason { get; set; } = ShutdownReason.Other;

/// <summary>
/// Force the operation to take place if possible.
/// </summary>
Expand Down Expand Up @@ -1197,9 +1378,12 @@ public void Dispose()
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
protected override void ProcessRecord()
{
object[] flags = new object[] { 1, 0 };
object[] flags = new object[] { ShutdownDelaySec, Comment, (uint)Reason, 1 };

if (Force.IsPresent)
flags[0] = forcedShutdown;
{
flags[3] = forcedShutdown;
}

ProcessWSManProtocol(flags);
}
Expand Down Expand Up @@ -1251,7 +1435,7 @@ private void ProcessWSManProtocol(object[] flags)
}
else
{
ComputerWMIHelper.InvokeWin32ShutdownUsingWsman(
ComputerWMIHelper.InvokeWin32ShutdownTrackerUsingWsman(
this,
isLocalHost,
compname,
Expand Down Expand Up @@ -1541,8 +1725,15 @@ private void DoRenameComputerWsman(string computer, string computerName, string
if (_restart)
{
// If successful and the Restart parameter is specified, restart the computer
object[] flags = new object[] { 6, 0 };
ComputerWMIHelper.InvokeWin32ShutdownUsingWsman(
object[] flags = new object[]
{
0u,
StringUtil.Format(ComputerResources.RestartcomputerAfterRename, newName),
(uint)ShutdownReason.OperatingSystemReconfigPlanned,
6,
};

ComputerWMIHelper.InvokeWin32ShutdownTrackerUsingWsman(
Comment thread
kvprasoon marked this conversation as resolved.
Resolved
this,
isLocalhost,
computerName,
Expand Down Expand Up @@ -1787,7 +1978,7 @@ internal static class ComputerWMIHelper
/// <summary>
/// CimOperatingSystemShutdownMethod.
/// </summary>
internal const string CimOperatingSystemShutdownMethod = "Win32shutdown";
internal const string CimOperatingSystemShutdownMethod = "Win32ShutdownTracker";
Comment thread
kvprasoon marked this conversation as resolved.
Resolved

/// <summary>
/// CimQueryDialect.
Expand Down Expand Up @@ -2040,21 +2231,21 @@ internal static bool IsComputerNameValid(string computerName)
}

/// <summary>
/// Invokes the Win32Shutdown command on provided target computer using WSMan
/// Invokes the Win32ShutdownTracker command on provided target computer using WSMan
/// over a CIMSession. The flags parameter determines the type of shutdown operation
/// such as shutdown, reboot, force etc.
/// </summary>
/// <param name="cmdlet">Cmdlet host for reporting errors.</param>
/// <param name="isLocalhost">True if local host computer.</param>
/// <param name="computerName">Target computer.</param>
/// <param name="flags">Win32Shutdown flags.</param>
/// <param name="flags">Win32ShutdownTracker flags.</param>
/// <param name="credential">Optional credential.</param>
/// <param name="authentication">Optional authentication.</param>
/// <param name="formatErrorMessage">Error message format string that takes two parameters.</param>
/// <param name="ErrorFQEID">Fully qualified error Id.</param>
/// <param name="cancelToken">Cancel token.</param>
/// <returns>True on success.</returns>
internal static bool InvokeWin32ShutdownUsingWsman(
internal static bool InvokeWin32ShutdownTrackerUsingWsman(
PSCmdlet cmdlet,
bool isLocalhost,
string computerName,
Expand All @@ -2065,7 +2256,7 @@ internal static bool InvokeWin32ShutdownUsingWsman(
string ErrorFQEID,
CancellationToken cancelToken)
{
Dbg.Diagnostics.Assert(flags.Length == 2, "Caller need to verify the flags passed in");
Dbg.Diagnostics.Assert(flags.Length == 4, "Caller needs to verify the flags passed in");

bool isSuccess = false;
string targetMachine = isLocalhost ? "localhost" : computerName;
Expand Down Expand Up @@ -2098,14 +2289,26 @@ internal static bool InvokeWin32ShutdownUsingWsman(
var methodParameters = new CimMethodParametersCollection();
int retVal;
methodParameters.Add(CimMethodParameter.Create(
"Flags",
"Timeout",
flags[0],
Microsoft.Management.Infrastructure.CimType.SInt32,
Microsoft.Management.Infrastructure.CimType.UInt32,
CimFlags.None));
Comment thread
kvprasoon marked this conversation as resolved.
Resolved

methodParameters.Add(CimMethodParameter.Create(
"Reserved",
"Comment",
flags[1],
Microsoft.Management.Infrastructure.CimType.String,
CimFlags.None));

methodParameters.Add(CimMethodParameter.Create(
"ReasonCode",
flags[2],
Microsoft.Management.Infrastructure.CimType.UInt32,
CimFlags.None));

methodParameters.Add(CimMethodParameter.Create(
"Flags",
flags[3],
Microsoft.Management.Infrastructure.CimType.SInt32,
CimFlags.None));

Expand Down
Loading