Auto reconnect to a server you just rebooted with PowerShell 3.0

January 8, 2012

Yesterday I saw a tweet that caught my eye:


image


Sounds familiar? What do you usually do to reconnect to your server once it is back online? How do you monitor it’s availability? In this post, Justin shares a great function (115 lines) that restarts a server, waits for it to come back online and then starts a remote desktop connection to it.


Now, what if I told you that in PowerShell 3.0 you can replace that function with only two lines of code?


In PowerShell 3.0, the Restart-Computer cmdlet got improved (mainly to support Workflow scenarios) and it now supports a few more parameters, notably the –Wait and –For parameters. When –Wait is specified, the command waits for all of the following service types to be available (can take a lot of time) before you can proceed to the next line of code. The following command lists the service types:

PS> [Enum]::GetNames(‘Microsoft.PowerShell.Commands.WaitForServiceTypes’)
Wmi
WinRM
PowerShell

Actually, these types are the possible values of the –For parameter. In the previous CTP version, there used to be a ‘Network’ type too, but looks like it was removed. We can specify one of above types, Restart-Computer waits for that component only to be available and then the rest of our code is executed.


In our case, we need to wait for the Remote Desktop Services service to start, but there’s no related service type we can use for that. We can wait for the ‘Wmi’ service. That’s because the WMI and the RDS services are dependent on the Remote Procedure Call (RPC) service, so if the WMI service is started we can assume that the RDS service is up as well.

Restart-Computer –ComputerName Server1 -Wait -For Wmi –Force
mstsc –v Server1 /admin

That’s all it takes to connect to Server1 once it’s back online. Notice that the Restart-Computer command may fail if there are users logged on to the remote computer or if there are opened applications, so make sure you include the –Force switch. One “caveat” though, the command waits until the computer has finished restarting and doesn’t give back your prompt, you’re stuck waiting for it to finish. To continue working you can spin a new background job:

Start-Job -ScriptBlock{
Restart-Computer –ComputerName $args[0] -Wait -For Wmi–Force
mstsc -v $args[0] /admin
} -ArgumentList Server1

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

5 comments

  1. Ifiok MosesFebruary 28, 2012 ב 07:03

    Was attempting to re-connect too soon and failing. Got it to work successfully by adding the line:

    sleep -s 30

    before the mstsc command. see below:

    Start-Job -ScriptBlock{
    Restart-Computer –ComputerName $args[0] -Wait -For Wmi –Force
    sleep -Seconds 30
    mstsc -v $args[0] /admin
    } -ArgumentList server01

    Thanks for this.

    Reply
  2. NateshJuly 28, 2012 ב 11:20

    What if i want i want to send message to users to log off before rebooting the servers? how to intract with the user sessions? Please let me know if you can help me on this
    Regards,
    natidutt at gmail dot com

    Reply
  3. ScriptFanaticJuly 28, 2012 ב 14:52

    It depends. There are many forms of logged on users, like: interactively, RDP, programatically, shares, and so on.

    One way would e to send messages using ‘net send’ or msg.exe. For RDP users you can use the http://psterminalservices.codeplex.com module and send messages using the Send-TSMessage function.

    Reply
  4. NateshAugust 1, 2012 ב 16:07

    Thanks for your response ScriptFanatic.

    I was not clear at the time i think.

    I want to sent message to the users who esablished the ICA connection which is esablished by Citrix XenApp.

    Is there a way to achive this?

    Regards,
    Natesh.

    Reply
  5. ScriptFanaticAugust 1, 2012 ב 17:26

    I don’t manage Citrix servers so I don’t really know how. I guess you can use the Citrix commands to get a list of active sessions and send each session a message either by using a special Citrix utility or by using msg.exe/net send command?

    Reply