Ezine 212 PowerShell WMI Restore Point

The practical task for this PowerShell script is to disable, or enable the operating system’s restore points.  From a learning point of view you can see how to build a PowerShell function and also discover another way of accessing the WMI classes. 

Topics for PowerShell Set-SysRestore Function

 ♣

This Week’s Secret

When I was trying to repair a Vista machine, I got into a knot trying a system restore; this was because the anti-virus program kept giving a false positive reaction to a file that I was trying to repair.  By creating a PowerShell function I was able to control system restore while I sorted out the anti-virus problem.

Set-SysRestore Function

It is traditional to name PowerShell functions using existing verbs such as ‘get-‘ or ‘set-‘.  At the heart of this function is the switch ($RestoreOpt) which calls upon the WMI class systemrestore.  It is this parameter which provides the option to enable or disable the system restore.

One important point, you need to supply a different value for $sysName.

##########################################
##
## Author: Dave Stone, Brooks Automation
## Date: 6 May 2010
## Enables or disables the system restore on a given workstation
##
## Usage Set-SysRestore option computername
##
######################################

Function Set-SysRestore {
param(
$RestoreOpt = $(throw "Please specify option, disable or enable"),
$sysName = $(throw "Please specify a computer name or IP Address.")
      )
switch ($RestoreOpt)
{
   "disable"
   {$SysRestore = [wmiclass]"\\$sysname\root\default:systemrestore"
    $SysRestore.Disable("C:\")}
   "enable"
   {$SysRestore = [wmiclass]"\\$sysname\root\default:systemrestore"
    $SysRestore.Enable("C:\")}
   }
                             }
Set-SysRestore -RestoreOpt enable -SysName 192.168.1.166

Note 0:  This is one of those PowerShell scripts where you need to ‘Run as Administrator’.  Also Restore Points are a feature of client operating systems such as Windows 7 or Vista, and not servers. SystemRestore WMI PowerShell

Note 1:  Trace the two options for the -RestoreOpt parameter.  The last line puts the function Set-SysRestore to work, and disables the system restore on the C:\.

Note 2:  To turn off your restore, issue the reverse command:
Set-SysRestore -RestoreOpt disable -SysName YourComputer

Example:
Set-SysRestore -RestoreOpt disable -SysName MyWin7Computer

Note 3:  Pay close attention to -SysName, your Windows 7 or Vista machine is unlikely to have an IP address of 192.168.1.166.  You could of course use the hostname.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Enable-ComputerRestore

One Advantage of the above WMI technique is that you can turn off system protection on the D, E or other drives.  However, if you just wish to disable system restore on the C:\ drive then you can use PowerShell v 2.0’s Disable-ComputerRestore cmdlet.

# PowerShell 2.0 Enable Computer Restore Script
Clear-Host
Enable-ComputerRestore -drive "C:\"
# Disable-ComputerRestore -drive "C:\"

Reminder of Guy’s Brief / Challenge:

  1. Use the cmdlet: Get-WinEvent
  2. Your PowerShell script must do something useful.
  3. No more than 10 lines.
  4. No copying from the internet!  It must be your code.
  5. The best script gets $10 paid into its writer’s PayPal account.

Summary of Function Set-SysRestore

Here is a great example of creating a PowerShell function to tackle one specific job, namely to switch system restore on, or off.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

If you like this page then please share it with your friends

 


See more Windows PowerShell examples

PowerShell Home   • Foreach loops   • PowerShell Foreach   • Foreach-Object cmdlet

Syntax   • Variables   • -whatIf   • -ErrorAction   • Windows 8 PowerShell   • Free CSV Import Tool

PowerShell Functions   • [System.Math]   • Get-Credential   • Windows PowerShell   • PowerShell 3.0

Please email me if you have a better example script.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to  correct the fault.