0

I am working on a GUI reboot modul in a tool of mine. I want to use the command prompt "shutdown" command line for this. Its purpose is to replace the "shutdown -i" on multiple servers and then I can ping them automatically to check if the reboot was succesfull.

In CMD the command line looks like this:

shutdown /r /t 30 /m \\server /c "reboot reason"

In my script I will ask for the:

  • reason = $comment
  • time = $time (the value and also in a checkbox if it is required or not)
  • server name = $server

I will test with a few "if"s the time option, is it checked or not and that the reboot reason is not empty and then add all of them to a variable:

$reboot = "/r /t " + $time + "/m \\" + $server + "/c " + $comment

and then use the variable in the command in powershell:

& shutdown $reboot

My question is will this work? Did anyone use it like this? Or there is a better way to do it? I can't test it for a few days because I don't have any servers right now on the network that I can reboot feely.

2
  • I think what you have should work but you could also use the Stop-Computer cmdlet which is there for the same reason . Although you cant seem to get comments with that Commented Feb 23, 2015 at 15:53
  • Thank you for the idea. I looked into that restart-computer command, and it looks pretty "variable frindly" but cand't find the "comment" option (it is a must have field for me) This is the Syntax i'v found: Stop-Computer [[-ComputerName] string[]] [[-Credential] PSCredential] [-Authentication AuthenticationLevel] [-Impersonation ImpersonationLevel] [-AsJob] [-Force] [-ThrottleLimit int] [-Confirm] [-WhatIf] [CommonParameters] Commented Feb 23, 2015 at 16:35

1 Answer 1

1

It should work, yes. I would also recommend using an array for the parameters to keep it clean.

$time = 120
$server = "mycomputer"
$comment = "this is my comment."
$reboot = @("/r", "/t", $time, "/m", "\\$server", "/c", $comment)

& shutdown $reboot

Or you could try doing it using WMI (untested):

$time = 120
$comment = "this is my comment."
$server = "mycomputer"

Invoke-WmiMethod -ComputerName $server -Class Win32_OperatingSystem -Name Win32ShutdownTracker -ArgumentList @($time, $comment, 0, 2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.