The Surly Admin

Father, husband, IT Pro, cancer survivor

Restart-Computers with Firm Confirmation

I’m always on the look out for new PowerShell projects.  Doesn’t much matter if they’re little projects–like this one–or big ones–like The DFS Monitor With History.  This is why I’m always on the Spiceworks IT Programming forums and PowerGui.org’s forums.

Today I ran across a little script someone was writing in batch and I decided to write it in PowerShell for a couple of reasons.  I had not really played with the Read-Host functionality yet and I wanted to play with the built-in documentation functionality of PowerShell which I discovered while on vacation.  Yes, I read PowerShell stuff while on vacation, it’s a weakness (so far I’ve been able to hold off on the PowerShell reference app for the iPad, but not sure how much longer)!!

Read-Host is a simple cmdlet that let’s you get input from the user.  Works exactly as you would expect it to and I really had no difficulty with it.  I was able to get it to work as a Parameter and later on without even assigning the result into a variable–which I think is one of the neatest features of PowerShell.

A word about Parameters!  This is kinda documented out there, but I thought I’d bring it up since I had this problem.  When running the first versions of this script I kept running into an error that stated the command PARAM was unknown.  This is crazy, I’ve used it many times!!  What’s going on here?

As it turns out, PARAM must be the first non-comment line in a script.  I had a simple Clear-Host command at the beginning of the script to clear the screen and that was killing my PARAM function.  Argh!

Next comes the built-in documentation capability within PowerShell.  I had a great page on Technet all set aside and I thought I had bookmarked it, but alas it is gone.  Instead I found this link by the Microsoft Scripting Guy, Ed Wilson, and it’s just as good though not as detailed.  Using a comment block and special dotted parameters you can fully document your script.  The really cool part is if someone uses the Get-Help cmdlet on your script:

Get-Help .\MyScript.ps1 -full

Then Get-Help will read those comments and produce output consistent with any built-in cmdlet!  Very cool and a simple way of bundling your documentation right into your script.  Here’s an example:

<#
.Synopsis
 Simple script to reboot computers that requires you to confirm before it runs.
.Description
 Simple script to reboot computers that requires you to confirm before it runs. Inspired
 by a scripting problem here: http://community.spiceworks.com/topic/254089-bat-script-help-need-prompt-for-specific-input-to-continue

 First you must confirm that you want to reboot the computers with a Y/N prompt (or use the parameter),
 then you must type SHUTDOWN-ALL-COMPUTERS at the prompt before the computer list will be rebooted.
.Parameter -Computers
 An array of computer names. Default is set and can be modified by you.
.Parameter -Confirm
 Two factor confirmation before servers are rebooted. Confirm must be "Y" for the script to
 proceed. If you do not put in the parameter the script will ask you.
.Parameter -File
 Path to a file that has a list of computer names in it. This will override -Computers. Computer
 names should be one name per line.
.Example
 .\RestartComputer.ps1
 Will prompt for a confirmation, then do the hard confirm and then reboot the default computers
 computer1, computer2 and computer3.
.Example
 .\RestartComputer.ps1 -computers server1,server2,server3 -confirm y
 Will do the hard confirm then reboot server1, server2 and server3.
.Example
 .\RestartComputer.ps1 -file servers.txt
 Will prompt you for a Y/N confirmation, then ask for the hard confirmation then reboot all computers
 in the server.txt file.
.Link
 http://community.spiceworks.com/topic/254089-bat-script-help-need-prompt-for-specific-input-to-continue
#>

So, back to RestartComputersWithConfirm.ps1!  It’s a pretty straight forward script that will default restart a set number of computers (you have to alter the script to put in defaults that work for you).  It will also accept an array of computers from the command line and you can even specify a text file that has a list of computers to reboot.  There are also 2 levels of confirmations, one you can do from the command line and the other you have to type manually no matter what.  This is pretty good practice when scripting something as severe as rebooting a computer.

You can find the full script here:  RestartComputersWithConfirm.ps1

Advertisement

August 28, 2012 - Posted by | PowerShell | , , , , ,

2 Comments »

  1. […] particular function rose out of another script I wrote a little while back, the Restart Computers with Firm Confirmation.  I wanted to make a particular piece of it available for others to use in their own scripts or at […]

    Pingback by Secure Credentials Function « The Surly Admin | October 3, 2012 | Reply

  2. […] while back I wrote a script that would restart a computer, but require a very firm confirmation from the administrator.  Two […]

    Pingback by Set-ShutdownComputers – Without Confirmation « The Surly Admin | February 7, 2013 | Reply


Leave a Reply to Set-ShutdownComputers – Without Confirmation « The Surly Admin Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: