Use PowerShell for an Easy Shutdown


Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to perform an easy shutdown.

Microsoft Scripting Guy, Ed Wilson, is here. One of the things I do with Windows PowerShell is to automate repetitive tasks. It may sound rather obvious, but I can assure you it is not. The reason? I become a creature of habit. I finally wrote this script, which I use on a regular basis: Use PowerShell to Start or Stop Virtual Machines in Order.

I could have written that script a long time ago, but I didn’t. I blindly went on…clicking every day to start the same four or five virtual machines over and over and over. I mean…dude!

Today’s script is no exception. It builds on the script I wrote to shut down virtual machines, but I add a couple of things and optimize the code. For example, when I am finished working for the day, I shut down all of the virtual machines. I also like to create a checkpoint (snapshot) on the virtual machines so that if I really hose things, I have something to fall back on.

Then I save my files in the Windows PowerShell ISE, and shut down my computer. It is always the same sorts of stuff, and the shutdown routine seems like it takes me 10 or 15 minutes each day. That ends up being over 60 hours a year, and 60 hours seems like a great ROI for the time I spent on today’s script.

Note  The script must run with administrator rights. I do this simply by selecting the RunAs Administrator option from the action menu when I right-click the Windows PowerShell ISE.

I want to use a Windows PowerShell workflow to shut down all of my virtual machines and to create a checkpoint to them before I shut down my laptop. To create a Windows PowerShell workflow, all I do is use the workflow keyword and specify the name of the workflow. I also want to require the PSWorkflow module, and the admin rights needed for the script. To do that I use the #requires directive. Here is that section of the script:

#Requires –Modules PSWorkflow

#requires -runasadministrator

workflow Stop-VMS

{

I create an array that contains the names of the virtual machines that I commonly use. I specify the order in which I want my virtual machines to shut down by specifying an order in the array. Here is my array:

$vms = 'c1_nwt','s2_nwt','s1_nwt','dc1_nwt' 

Now I use the Foreach statement to loop through my array of virtual machine names. I call the Stop-VM cmdlet to stop the virtual machine, and then I call the Checkpoint-VM cmdlet to create a checkpoint of the virtual machine. I use the Get-Random cmdlet to create a random number to assign to my checkpoint name. In this way, each checkpoint created via my script will stand out easily in Hyper-V Manager. Here is the code:

Foreach($vm in $vms)

        { Stop-VM -Name $vm

         Checkpoint-VM -name $vm -SnapshotName "Shutdown_$vm_$(Get-Random)"}

In the following image, I can easily differentiate between the two checkpoints (snapshots).

Image of command output

Now I call my workflow. It is called like I would call a function. Here is the command:

Stop-VMS

I want to be sure that my script in the Windows PowerShell ISE is saved. To do that, I check the IsSaved property. If it is not saved, I go ahead and save the script. Here is the line of code:

if(!($psISE.CurrentFile.IsSaved)){$psISE.CurrentFile.Save()}

Now that my virtual machines are shut down, I have checkpoints, and my script is saved, it is time to shut down my computer. Piece of cake.

I call the Stop-Computer cmdlet. I also specify the –Force parameter to force closed any blocking applications that may be running. (This works for me, but it is something you might need to test on your computer.) Here is the line of code:

Stop-Computer -Force

Here is the complete script:

#Requires –Modules PSWorkflow

#requires -runasadministrator

workflow Stop-VMS

{

 $vms = 'c1_nwt','s2_nwt','s1_nwt','dc1_nwt'

    Foreach($vm in $vms)

        { Stop-VM -Name $vm

         Checkpoint-VM -name $vm -SnapshotName "Shutdown_$vm_$(Get-Random)"}

}

Stop-VMS

if(!($psISE.CurrentFile.IsSaved)){$psISE.CurrentFile.Save()}

Stop-Computer -Force

That is all there is to using Windows PowerShell to shut down a computer. Join me tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Comments (1)

  1. s.mcknight says:

    Why do you check-point after the Stop command…seem that it would not then check-point…?

Skip to main content