Use PowerShell DSC to Check Pending Reboot


Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and DSC to check a pending reboot.

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I started the week by heading to the Microsoft office to get our flu shots. Luckily, the flu shot did not require a reboot—but that actually got me thinking.

In the old days, I mean, in the really old days, it was fairly easy to check for a pending reboot. I simply read a key from the registry, and that was (as they say) that. But things have gotten more complicated. In fact, Microsoft PFE, Brian Wilhite, wrote a pretty popular script that detects pending reboot. (You can find this script on the Script Center Repository: Get-PendingReboot—Query Computers for Pending Reboot State.)

I believe at the heart of it, he checks four different things. Brian's script is cool—but dude, it is a lot of lines of code. I prefer Windows PowerShell and DSC.

Using the DSC Resource Kit xPendingReboot resource

One of the new DSC resources from the DSC Resource Kit is the xPendingReboot resource. It permits me to check if a reboot is required, and if it is, to perform that reboot.

There are a few prerequisites. There is a Windows update for Windows 8.1 and Windows Server 2012 R2 that is required for the Import-DSCResource command to work. To download this update, see article 2883200 in the Microsoft Knowledge Base. This update comes via Windows Update automatically, so if your machine is up-to-date on updates, it is already there. If not, ensure that you have the update.

After this one, there is another update. See article 289872 in the Microsoft Knowledge Base. After you have the updates and reboot, you will not be able to use Import-DSCResource directly. You can only use it inside the Windows PowerShell ISE and when you are writing a DSC. It looks like a cmdlet, but it is not. Tab expansion does not work for this command either, and it does not accept positional parameters. Therefore, the following command generates an error message:

Import-DSCResource xPendingReboot

The error message is rather complete, as shown here:

Keep in mind, however, that if I use the following command, I still get an error message. First, the Windows PowerShell ISE takes a long time to error out. Then it comes back and says it cannot find the resource because a quick check of the folder and all of the files reveals that everything should be in good shape. This one is not so easy to figure out:

Image of command output

Why can’t it find the resource? Why can’t it load the resource? Well, because I need to specify –module, and not –name. When I do that, everything works like clockwork. Here is the command:

Import-DscResource -module xPendingReboot

     Note  Remember that with each of the xfiles (DSC resources from the DSC Resource Kit), I need to explicitly use
     Import-DSCResource to import the DSC resource. Also keep in mind that although the command Import-DSCResource
     appears to be a cmdlet, it is not. It does not support Tab expansion, it does not take positional parameters, and you
     cannot use Get-Help Import-DSCResource.

So how do I use this resource provider? It is simple. I create a new configuration and provide it with a name. I also specify my node name (but I can use this on multiple nodes). Here is that portion of the configuration:

Configuration CheckForPendingReboot

{       

  Import-DscResource -module xPendingReboot

    Node ‘edlt’

    {  

Now I call the provider, and I specify the name:

xPendingReboot Reboot1

        {

            Name = ‘BeforeSoftwareInstall’

        }

I call the LocalConfigurationManager, and I specify to reboot if a reboot is needed:

LocalConfigurationManager

        {

            RebootNodeIfNeeded = 'True'

        } 

 Now I create my .mof file from the configuration, and I start the configuration:

CheckForPendingReboot -OutputPath c:\edlt\PendingReboot

Start-DscConfiguration -path c:\edlt\PendingReboot -wait -Verbose

Here is the complete script:

Configuration CheckForPendingReboot

{       

  Import-DscResource -module xPendingReboot

    Node ‘edlt’

    { 

        xPendingReboot Reboot1

        {

            Name = ‘BeforeSoftwareInstall’

        }

        LocalConfigurationManager

        {

            RebootNodeIfNeeded = 'True'

        }

    } 

}

 

CheckForPendingReboot -OutputPath c:\edlt\PendingReboot

Start-DscConfiguration -path c:\edlt\PendingReboot -wait -Verbose

When I specify the configuration with the –Verbose cmdlet, I get a nice output that tells me about each step of the configuration and how long the activities take. Here is the script and the output:

Image of command output

That is all there is to using Windows PowerShell to check a pending reboot. DSC Resource Kit Week will continue 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 (3)

  1. loans for people with bad credit says:

    The amount of cash you should not have to settle for a http://ukloansforbadcredits.co.uk/">loans for bad credit long distance and painful waiting period for short-term loans. They will assess your address, work status and age. There
    is no need to fax something and http://ukloansforbadcredits.co.uk/">bad credit loans the entire application takes a few minutes on the web. Even if you encounter a problem during the application or questions, such problems can be resolved
    quickly.

  2. shainw79 says:

    This means you need a bit of cash before closing the sale. Normally there is no regular salary based on hours as a normal office environment. A commission is usually thousands of dollars, but is not as regular as office work. These loans help realtors
    obtained by arriving before the next big commission.

    http://6monthtextloansnoguarantor.co.uk
    http://uk6monthloans.co.uk/
    http://www.elongtermpaydayloansuk.co.uk/12-month-loans.html

Skip to main content