Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Use Desired State Configuration to Create System Restore Point

$
0
0

Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell Desired State Configuration to create a system restore point.

Microsoft Scripting Guy, Ed Wilson, is here. It is cold outside again. I nearly froze this morning when I headed to the gym for an easy walk on the treadmill. But it is a good way to wake up early in the morning. The good thing is the gym is open 24 hours a day, so it makes an early morning workout pretty easy. The bad thing about early morning workouts is, well, they happen early in the morning.

One thing that happens easily and does not require me to get up before the chickens, is Desired State Configuration (DSC). I can easily schedule DSC to happen whenever I want. After I have created my MOF files, all I need to do to reapply a DSC configuration is run the Start-DSCConfiguration command. I can schedule that command in the task scheduler in less than 30 seconds, and I can set my schedule to repeat as often as I want it to.

But that is not what I want to talk about today. I want to talk about using DSC to create a system restore point. One thing to remember about restore points is that they only work on the client workstation. they do not work on the server operating system.

Use PowerShell DSC to create a restore point

After I call the Configurationkeyword to create a new configuration script, I create my parameters. I use a filter with the Get-ADComputer cmdlet so that I return only Windows 8.1 and Windows 8 workstations. I store the resulting names in the $NodeName variable. Here is this portion of the script:

#Requires -version 4.0

Configuration CreateSystemRestorePoint

{

    Param

    (

        [String[]]$NodeName = (

        (Get-ADComputer -Filter "OperatingSystem -like 'Windows 8*'").name)

    )

The next thing I do is import the DSC resource. I have to do this because the xWindowsRestore DSC module is not standard. Here is the command:

Import-DSCResource -ModuleName xWindowsRestore

Now I specify the nodes for which I want to create configuration MOF files. I do this by using the Nodekey word. I then call the xSystemRestorePoint DSC resource (from the xWindowsRestore module), and I specify the name for the action. This is shown here:

  Node $NodeName

    {

         xSystemRestorePoint SystemRestorePointExample

I need to specify a few parameter values. I set the name for the Description, specify the type of restore, and ensure that this resource will be present (which means create a new restore point). This section of the script is shown here:

{

            Description = "Modify system settings"

            RestorePointType = "MODIFY_SETTINGS"

            Ensure = "Present"

        }

I call the CreateSystemRestorePoint configuration and specify an output folder location. If I do not specify an output folder location, the MOF files will be created in the current working directory. Lastly, I call Start-DSCConfiguration and point to the location for the MOF files. This causes the configuration to take place. Here is that portion of the script:

CreateSystemRestorePoint -output C:\serverConfig

Start-DscConfiguration -Path C:\serverConfig  -Wait -Force -Verbose

Here is the complete script:

#Requires -version 4.0

Configuration CreateSystemRestorePoint

{

    Param

    (

        [String[]]$NodeName = (

        (Get-ADComputer -Filter "OperatingSystem -like 'Windows 8*'").name)

    )

    Import-DSCResource -ModuleName xWindowsRestore

 

    Node $NodeName

    {

         xSystemRestorePoint SystemRestorePointExample

        {

            Description = "Modify system settings"

            RestorePointType = "MODIFY_SETTINGS"

            Ensure = "Present"

        }

    }

}

 

CreateSystemRestorePoint -output C:\serverConfig

Start-DscConfiguration -Path C:\serverConfig  -Wait -Force -Verbose

When I run the script, I see that the MOF file is created for the Windows 8* type of workstation, and then each action DSC performs appears as a verbose statement. This is shown in the following image:

Image of command output

I check to ensure that the checkpoint was actually created. I use the Get-ComputerRestorePoint cmdlet to do this. Here is the command and results:

PS C:\> Get-ComputerRestorePoint 

CreationTime    Description       SequenceNumber    EventType   RestorePoint Type

------------           -----------          --------------               ---------            ----

12/8/2014 3:08:24 PM   Scheduled Checkpoint         22          BEGIN_SYSTEM_C... 7  

1/2/2015 1:31:39 PM    Scheduled Checkpoint          23          BEGIN_SYSTEM_C... 7  

1/6/2015 2:10:39 PM    Modify system settings          24          BEGIN_SYSTEM_C... M...

Cool. It worked.

That is all there is to using DSC to create a system restore point. Join me tomorrow when I will talk about more cool Windows PowerShell 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 


Viewing all articles
Browse latest Browse all 3333

Trending Articles