Hey, Scripting Guy! How Can I Collect System Restore Information in Windows Vista?

Hey, Scripting Guy! How Can I Collect System Restore Information in Windows Vista?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! I was looking at System Restore on my Windows Vista computer, and I had an idea. Wouldn’t it be great if I could script the collection of information for System Restore? I would like it to tell me how much disk space is being consumed, as well as when it is scheduled to run. I think you should write a script that will let me do this, because it would obviously be of great value to others as well.

- BB

SpacerHey, Scripting Guy! Answer

Hi BB,

We are so glad we caught your e-mail in the scripter@microsoft.com mailbox. We have been absolutely swamped with e-mail this past week as we conclude our final preparations for the 2009 Summer Scripting Games. Actually, reading the scripter e-mail is one of our favorite things to do for relaxation. Ed generally gets to the scripter e-mail either late in the evening or first thing in the morning, and he likes to sip a nice cup of freshly brewed tea. Of course, the time of day and his mood determines the type of tea he will fix. Today, it is green tea in a tea bag with a cinnamon stick in a Microsoft paper cup. He seems a bit distracted and is probably missing his little teapot, his various tins of tea, and tea setting that are back home in South Carolina. Oh well, he will get over it. Let’s give him a script to write.

BB, here is the GetSystemRestoreSettings.ps1 script. It will allow you to list the system restore schedule, as well as provide information about the amount of disk space that is being used by system restore.

GetSystemRestoreSettings.ps1

Param($computer = "localhost", $help)

function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: GetOffLineFiles.ps1 
Prints the offline files config on a local or remote machine.

PARAMETERS: 
-computer Specifies name of the computer upon which to run the script
-help     prints help file

SYNTAX:
GetSystemRestoreSettings.ps1 -computer MunichServer

Lists system restore config on a computer named MunichServer

GetSystemRestoreSettings.ps1 

Lists system restore config on local computer

GetSystemRestoreSettings.ps1 -help ?

Displays the help topic for the script

"@
$helpText
exit
}

if($help){ funline("Obtaining help ...") ; funhelp }

New-Variable -Name SecInDay -option constant -value 86400 
$objWMI = Get-WmiObject -Namespace root\default `
         -Class SystemRestoreConfig -computername $computer
for($i=0; $i -le 15; $i++)
{
 Write-Host -ForegroundColor $i "Retrieving System Restore Settings"
 Start-Sleep -Milliseconds 60
 cls
}

if($computer -eq "localhost")
 { 
  Write-Host "System Restore Settings on $env:computername"
 }
 ELSE
 { 
  Write-Host "System Restore Settings on $computer"
 }

format-table -InputObject $objWMI -property `
  @{
    Label="Max disk utilization" ; 
expression={  "{0:n0}"-f ($_.DiskPercent ) + " %"} 
},
  @{
    Label="Scheduled Backup" ; 
expression={  "{0:n2}"-f ($_.RPGlobalInterval / $SecInDay) + " days"} 
},
  @{
    Label="Max age of backups" ; 
expression={ "{0:n2}"-f ($_.RPLifeInterval / $SecInDay) + " days" }

BB, there are basically two WMI classes that can be used to manage system restore on a machine. The only thing that is tricky about these classes is that they exist in the Root\Default WMI namespace, which by the way is not the default WMI namespace. The default WMI namespace is Root\Cimv2. (See, we told you it was tricky.) These two WMI classes are listed here:

  • Systemrestore
  • Systemrestoreconfig

In the GetSystemRestoreSettings.ps1 script, we first use the param statement to permit the use of command-line arguments to the script. We define two parameters: -computer and –help. This will allow us to target a remote computer, and to get Help if required. The –computer parameter is set to a default value of localhost. This line of code is seen here:

Param($computer = "localhost", $help)

We then have the funhelp function. The funhelp function is used to print out a Help file when the script is run with the –help parameter specified. To create the Help file, we use the variable $helpText and set it equal to a Here-String. The Here-String allows us to ignore quoting rules while typing out the text in the script. In the Here-String, we define a description, the parameters, and the syntax of the script. Once the Here-String is created, we then print out the value contained in the $helpText variable, and exit the script. This is seen here:

function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: GetOffLineFiles.ps1 
Prints the offline files config on a local or remote machine.

PARAMETERS: 
-computer Specifies name of the computer upon which to run the script
-help     prints help file

SYNTAX:
GetSystemRestoreSettings.ps1 -computer MunichServer

Lists system restore config on a computer named MunichServer

GetSystemRestoreSettings.ps1 

Lists system restore config on local computer

GetSystemRestoreSettings.ps1 -help ?

Displays the help topic for the script

"@
$helpText
exit
}

To determine if the script will need to display Help, we look for the presence of the $help variable. If we find the $help variable, we first call the funline function and print out a progress indicator that is underlined. Next, we call the funhelp function. This code is seen here:

if($help){ funline("Obtaining help ...") ; funhelp }

We then create a constant. This constant is called SecInDay and is set to a value of 86400. To create a constant, we need to use the New-Variable cmdlet and specify the –option parameter with the constant argument. This is seen here:

New-Variable -Name SecInDay -option constant -value 86400

Now it is time to make the connection into WMI. To do this, we use the Get-Wmiobject cmdlet and connect to the root\default WMI namespace. We use the –class parameter to specify the SystemRestoreConfig WMI class name, and the –computername parameter to allow us to target a specific computer. We then store the resulting management object in the $objWMI variable. This code is seen here:

$objWMI = Get-WmiObject -Namespace root\default `
         -Class SystemRestoreConfig -computername $computer

Next we use the for statement to count from 0 to 15 and increment the $i variable. In the code block of the for statemement, we use the Write-Host cmdlet and supply the $i variable to the –foregroundcolor parameter. We then print out a status prompt, wait for 60 milliseconds, clear the screen, and then repeat. The effect is like a multicolored rolling progress indicator that will grab the users’ attention and alert them to the progress. This section of code is seen here:

for($i=0; $i -le 15; $i++)
{
 Write-Host -ForegroundColor $i "Retrieving System Restore Settings"
 Start-Sleep -Milliseconds 60
 cls
}

Now we need to decide if we are going to use the computername environment variable from the Windows PowerShell PSDrive, or if we will use the value that was supplied to the –computer parameter. The reason for this is that the Windows PowerShell PSDrive is only available for the local machine. If the computer is localhost, it is local.

if($computer -eq "localhost")
 { 
  Write-Host "System Restore Settings on $env:computername"
 }

However, if the computer name is some other value, we will use that value instead. This logic is seen here:

ELSE
 { 
  Write-Host "System Restore Settings on $computer"
 }

We next need to format our output. To do this we will use the Format-Table cmdlet. In this example we use the –inputobject parameter and supply the management object stored in the $objWMI variable to the cmdlet. Next, we use the –property parameter to specify the properties to be listed in the table. The unusual aspect of this script is using a hash table to change the formatting of the printout of the property values. The hash table begins with an at symbol (@) and an opening code block. We then specify the label to use and the expression to calculate the property value. When this is printed out, we will report the backup time in days, instead of seconds. We also display the percent disk utilization with the percentage symbol. This section of code is seen here:

format-table -InputObject $objWMI -property `
  @{
    Label="Max disk utilization" ; 
expression={  "{0:n0}"-f ($_.DiskPercent ) + " %"} 
},
  @{
    Label="Scheduled Backup" ; 
expression={  "{0:n2}"-f ($_.RPGlobalInterval / $SecInDay) + " days"} 
},
  @{
    Label="Max age of backups" ; 
expression={ "{0:n2}"-f ($_.RPLifeInterval / $SecInDay) + " days" } 
}

When the GetSystemRestoreSettings.ps1 script is run, this is what is displayed:

Image of what is displayed when the script is run

 

BB, that is all there is to the GetSystemRestoreSettings.ps1 script. This also brings us to the end of Managing User Data week on the Script Center. Join us next week as we begin to reveal the Summer Scripting Games events. Tomorrow we open up the virtual mail bag, and grab a bunch of short answer questions. That’s right, it’s time for another edition of Quick-Hits Friday! See you tomorrow.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Scripting Guy,

          I modified the disk space to be used for system restore to 10%, but the powershell is not at all reflecting the modified values to me. In Windows XP it is always displaying as 12% and in Windows 7 it is displaying as 15%. Can you tell me how can I retrieve the correct information?

    Thanks,

    Sree

  • So I know this is an old subject. But I'm seeing the same thing stsree is seeing. 15%, no matter what I modify the values to be.  IS there a way to get the correct values?