Create semi-automated reboot procedure in PowerShell?

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year, 6 months ago.

  • Author
    Posts
  • #27000
    Profile photo of Michael Wells
    Michael Wells
    Participant

    Hey there all,
    I am looking to create something that will make remote rebooting faster when I am running installs and updates on some of our boxes.

    I have a series of computers that I am routinely servicing. Knowing Windows, there is a lot of restarting involved. I was wondering if I could create a PowerShell script to do the command needed for rebooting(shutdown -i for the Command Line) as quickly and with as little babysitting as possible. All of these computers have 9 character Device IDs, where the last 3 are the only ones that change.

    So I'm looking for a script of CMDLet That would have a popup to ask for the last 3 characters, and then reboot the remote computer, maybe saving the task for the session so I could reboot the previous computer even quicker.

    It seems like PowerShell should be able to do this, but I haven't done that much with it, so I'm not positive.

    Thanks!
    Mike Wells

  • #27003
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, Restart-Computer will restart the computer, but I'm not certain what you mean about "popup to ask for the last 3 characters" or "saving the task for the session."

    If you have a series of tasks that you need to complete, and a restart is needed between some of them, then PowerShell Workflow is something to consider. Once a workflow starts, it can remember where it was at, and continue running after restart. Or, look at Desired State Configuration. It can ensure that a computer is placed into a desired state, even if that involves one or more reboots along the way.

  • #27004
    Profile photo of Michael Wells
    Michael Wells
    Participant

    Don, our computer names are all XXXXXXYYY, with they 'Y' part being the only part that changes, so I was looking to create a one click option that would just ask which computer I wanted to reboot, and fill the 'X' part of the name automatically. Putting in only the last 3 characters(The 'Y' part) would save time.

  • #27005
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So... just use Read-Host to prompt for that information, store it in a variable, construct the entire computer name, and use Restart-Computer?

  • #27006
    Profile photo of Michael Wells
    Michael Wells
    Participant

    Don, that's pretty much what I was looking for, an idea where to start and which commands to use. Looks like Read-Host prompts, like you said, then I could combine it with a static value(The X's in my example), and then use Restart computer with the newly assembled Device ID. That sounds like exactly what I was looking for, if I have any issues, I'll poke around the board.

    Thank you

  • #27007
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Versus Read-Host, you might consider a function. If your company name was Tech Corp, you would put a TC in your function naming conventions to know they are specific to your company. If you put this function in a Powershell profile, it would be available when you start Powershell. If you start to get multiple functions, consider making it a module and loading that module automatically with your profile.

    function Restart-TCComputer {
        [CmdLetBinding()]
        param (
            [ValidateLength(3,3)]
            [Parameter(Position=0,
            Mandatory=$true,
            ValueFromPipeline=$true)]
            [string[]]$ComputerNumber,
            [string]$Prefix = "XXXXXXXXX"
        )
        begin{}
        process{
            foreach ($Number in $ComputerNumber) {
                $ComputerName = "{0}{1}" -f $Prefix, $Number
                Write-Verbose ("Restarting computer {0}" -f $ComputerName)
                Restart-Computer -ComputerName $ComputerName -Force -WhatIf
            }
        }
        end{}
    }
    
    123, 153 | Restart-TCComputer
    Restart-TCComputer -ComputerNumber 234
    

    Since a number is mandatory, if you tried to just call Restart-TCComputer, you would be prompted for input:

    C:\Windows\System32\WindowsPowerShell\v1.0> Restart-TCComputer
    cmdlet Restart-TCComputer at command pipeline position 1
    Supply values for the following parameters:
    ComputerNumber[0]: 352
    ComputerNumber[1]: 321
    ComputerNumber[2]: 355
    ComputerNumber[3]: 
    

    Using a function, you also validate that the numbers are 3 characters using ValidateLength and can use a -Verbose switch if you want visual output to indicate what server is being rebooted.

  • #27008
    Profile photo of Michael Wells
    Michael Wells
    Participant

    Cool, thanks guys, this is great! I learn all of this as I go, I've done a little in PowerShell, and a bunch of cool stuff in VBA for Excel, once I get the starting position like this, I can go through and tweak things, follow up similar examples, and all that. I appreciate the help again!

  • #27015
    Profile photo of Michael Wells
    Michael Wells
    Participant

    Rob,
    This looks like it works pretty well for a single computer, except that I get no prompt to enter data.

    I commented out the "-WhatIf" to make it run, and I commented out line 22, as it was asking for computers with 123 and 153 as targets. I changed $Prefix to haev the starting part of the name. With line 23, it has -ComputerNumber 234, and if I change it to the last 3 characters of the one I need to reboot, it works perfectly. I just can't get a prompt of any kind.

    I found an example of 'Read-Host' to make it prompt for the number I need, but don't know how to plug that value in.

  • #27019
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If you don't pass a ComputerNumber to the function, you will be prompted for input because it is Mandatory. So, simply just call the function with no parameters to be prompted:

    Restart-TCComputer
    

You must be logged in to reply to this topic.