{"id":53583,"date":"2009-06-03T23:42:00","date_gmt":"2009-06-03T23:42:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/06\/03\/hey-scripting-guy-how-can-i-collect-system-restore-information-in-windows-vista\/"},"modified":"2009-06-03T23:42:00","modified_gmt":"2009-06-03T23:42:00","slug":"hey-scripting-guy-how-can-i-collect-system-restore-information-in-windows-vista","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-collect-system-restore-information-in-windows-vista\/","title":{"rendered":"Hey, Scripting Guy! How Can I Collect System Restore Information in Windows Vista?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! I was looking at System Restore on my Windows Vista computer, and I had an idea. Wouldn\u2019t 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.<BR><BR>&#8211; BB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hi BB,<\/P>\n<P>We are so glad we caught your e-mail in the <A href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/A> 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 <B>scripter<\/B> 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\u2019s give him a script to write.<\/P>\n<P>BB, here is the <B>GetSystemRestoreSettings.ps1<\/B> 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.<\/P>\n<P><B>GetSystemRestoreSettings.ps1<\/B><\/P><PRE class=\"codeSample\">Param($computer = &#8220;localhost&#8221;, $help)<\/p>\n<p>function funHelp()\n{\n$helpText=@&#8221;\nDESCRIPTION:\nNAME: GetOffLineFiles.ps1 \nPrints the offline files config on a local or remote machine.<\/p>\n<p>PARAMETERS: \n-computer Specifies name of the computer upon which to run the script\n-help     prints help file<\/p>\n<p>SYNTAX:\nGetSystemRestoreSettings.ps1 -computer MunichServer<\/p>\n<p>Lists system restore config on a computer named MunichServer<\/p>\n<p>GetSystemRestoreSettings.ps1 <\/p>\n<p>Lists system restore config on local computer<\/p>\n<p>GetSystemRestoreSettings.ps1 -help ?<\/p>\n<p>Displays the help topic for the script<\/p>\n<p>&#8220;@\n$helpText\nexit\n}<\/p>\n<p>if($help){ funline(&#8220;Obtaining help &#8230;&#8221;) ; funhelp }<\/p>\n<p>New-Variable -Name SecInDay -option constant -value 86400 \n$objWMI = Get-WmiObject -Namespace root\\default `\n         -Class SystemRestoreConfig -computername $computer\nfor($i=0; $i -le 15; $i++)\n{\n Write-Host -ForegroundColor $i &#8220;Retrieving System Restore Settings&#8221;\n Start-Sleep -Milliseconds 60\n cls\n}<\/p>\n<p>if($computer -eq &#8220;localhost&#8221;)\n { \n  Write-Host &#8220;System Restore Settings on $env:computername&#8221;\n }\n ELSE\n { \n  Write-Host &#8220;System Restore Settings on $computer&#8221;\n }<\/p>\n<p>format-table -InputObject $objWMI -property `\n  @{\n    Label=&#8221;Max disk utilization&#8221; ; \nexpression={  &#8220;{0:n0}&#8221;-f ($_.DiskPercent ) + &#8221; %&#8221;} \n},\n  @{\n    Label=&#8221;Scheduled Backup&#8221; ; \nexpression={  &#8220;{0:n2}&#8221;-f ($_.RPGlobalInterval \/ $SecInDay) + &#8221; days&#8221;} \n},\n  @{\n    Label=&#8221;Max age of backups&#8221; ; \nexpression={ &#8220;{0:n2}&#8221;-f ($_.RPLifeInterval \/ $SecInDay) + &#8221; days&#8221; }\n<\/PRE>\n<P>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 <B>Root\\Default<\/B> WMI namespace, which by the way is not the default WMI namespace. The default WMI namespace is <B>Root\\Cimv2<\/B>. (See, we told you it was tricky.) These two WMI classes are listed here:<\/P>\n<UL>\n<LI>Systemrestore<\/LI>\n<LI>Systemrestoreconfig<\/LI><\/UL>\n<P>In the <B>GetSystemRestoreSettings.ps1<\/B> script, we first use the <B>param<\/B> statement to permit the use of command-line arguments to the script. We define two parameters: <B>-computer<\/B> and <B>\u2013help<\/B>. This will allow us to target a remote computer, and to get Help if required. The <B>\u2013computer<\/B> parameter is set to a default value of <B>localhost<\/B>. This line of code is seen here:<\/P><PRE class=\"codeSample\">Param($computer = &#8220;localhost&#8221;, $help)\n<\/PRE>\n<P>We then have the <B>funhelp<\/B> function. The <B>funhelp<\/B> function is used to print out a Help file when the script is run with the <B>\u2013help<\/B> parameter specified. To create the Help file, we use the variable <B>$helpText<\/B> and set it equal to a <B>Here-String<\/B>. The <B>Here-String<\/B> allows us to ignore quoting rules while typing out the text in the script. In the <B>Here-String<\/B>, we define a description, the parameters, and the syntax of the script. Once the <B>Here-String<\/B> is created, we then print out the value contained in the <B>$helpText<\/B> variable, and exit the script. This is seen here:<\/P><PRE class=\"codeSample\">function funHelp()\n{\n$helpText=@&#8221;\nDESCRIPTION:\nNAME: GetOffLineFiles.ps1 \nPrints the offline files config on a local or remote machine.<\/p>\n<p>PARAMETERS: \n-computer Specifies name of the computer upon which to run the script\n-help     prints help file<\/p>\n<p>SYNTAX:\nGetSystemRestoreSettings.ps1 -computer MunichServer<\/p>\n<p>Lists system restore config on a computer named MunichServer<\/p>\n<p>GetSystemRestoreSettings.ps1 <\/p>\n<p>Lists system restore config on local computer<\/p>\n<p>GetSystemRestoreSettings.ps1 -help ?<\/p>\n<p>Displays the help topic for the script<\/p>\n<p>&#8220;@\n$helpText\nexit\n}\n<\/PRE>\n<P>To determine if the script will need to display Help, we look for the presence of the <B>$help<\/B> variable. If we find the <B>$help<\/B> variable, we first call the <B>funline<\/B> function and print out a progress indicator that is underlined. Next, we call the <B>funhelp<\/B> function. This code is seen here:<\/P><PRE class=\"codeSample\">if($help){ funline(&#8220;Obtaining help &#8230;&#8221;) ; funhelp }\n<\/PRE>\n<P>We then create a constant. This constant is called <B>SecInDay<\/B> and is set to a value of 86400. To create a constant, we need to use the <B>New-Variable<\/B> cmdlet and specify the <B>\u2013option<\/B> parameter with the <B>constant<\/B> argument. This is seen here:<\/P><PRE class=\"codeSample\">New-Variable -Name SecInDay -option constant -value 86400\n<\/PRE>\n<P>Now it is time to make the connection into WMI. To do this, we use the <B>Get-Wmiobject<\/B> cmdlet and connect to the <B>root\\default<\/B> WMI namespace. We use the <B>\u2013class<\/B> parameter to specify the <B>SystemRestoreConfig<\/B> WMI class name, and the <B>\u2013computername<\/B> parameter to allow us to target a specific computer. We then store the resulting management object in the <B>$objWMI<\/B> variable. This code is seen here:<\/P><PRE class=\"codeSample\">$objWMI = Get-WmiObject -Namespace root\\default `\n         -Class SystemRestoreConfig -computername $computer\n<\/PRE>\n<P>Next we use the <B>for<\/B> statement to count from 0 to 15 and increment the <B>$i<\/B> variable. In the code block of the <B>for<\/B> statemement, we use the <B>Write-Host<\/B> cmdlet and supply the <B>$i<\/B> variable to the <B>\u2013foregroundcolor<\/B> 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\u2019 attention and alert them to the progress. This section of code is seen here:<\/P><PRE class=\"codeSample\">for($i=0; $i -le 15; $i++)\n{\n Write-Host -ForegroundColor $i &#8220;Retrieving System Restore Settings&#8221;\n Start-Sleep -Milliseconds 60\n cls\n}\n<\/PRE>\n<P>Now we need to decide if we are going to use the <B>computername<\/B> environment variable from the Windows PowerShell PSDrive, or if we will use the value that was supplied to the <B>\u2013computer<\/B> parameter. The reason for this is that the Windows PowerShell PSDrive is only available for the local machine. If the computer is <B>localhost<\/B>, it is local. <\/P><PRE class=\"codeSample\">if($computer -eq &#8220;localhost&#8221;)\n { \n  Write-Host &#8220;System Restore Settings on $env:computername&#8221;\n }\n<\/PRE>\n<P>However, if the computer name is some other value, we will use that value instead. This logic is seen here:<\/P><PRE class=\"codeSample\">ELSE\n { \n  Write-Host &#8220;System Restore Settings on $computer&#8221;\n }\n<\/PRE>\n<P>We next need to format our output. To do this we will use the <B>Format-Table<\/B> cmdlet. In this example we use the <B>\u2013inputobject<\/B> parameter and supply the <B>management<\/B> object stored in the <B>$objWMI<\/B> variable to the cmdlet. Next, we use the <B>\u2013property<\/B> 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 (<B>@<\/B>) 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:<\/P><PRE class=\"codeSample\">format-table -InputObject $objWMI -property `\n  @{\n    Label=&#8221;Max disk utilization&#8221; ; \nexpression={  &#8220;{0:n0}&#8221;-f ($_.DiskPercent ) + &#8221; %&#8221;} \n},\n  @{\n    Label=&#8221;Scheduled Backup&#8221; ; \nexpression={  &#8220;{0:n2}&#8221;-f ($_.RPGlobalInterval \/ $SecInDay) + &#8221; days&#8221;} \n},\n  @{\n    Label=&#8221;Max age of backups&#8221; ; \nexpression={ &#8220;{0:n2}&#8221;-f ($_.RPLifeInterval \/ $SecInDay) + &#8221; days&#8221; } \n}\n<\/PRE>\n<P>When the <B>GetSystemRestoreSettings.ps1<\/B> script is run, this is what is displayed:<\/P><IMG border=\"0\" alt=\"Image of what is displayed when the script is run\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/june\/hey0604\/hsg-06-04-09-01.jpg\" width=\"500\" height=\"148\"> \n<P>&nbsp;<\/P>\n<P>BB, that is all there is to the <B>GetSystemRestoreSettings.ps1<\/B> 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\u2019s right, it\u2019s time for another edition of Quick-Hits Friday! See you tomorrow.<\/P>\n<P>&nbsp;<\/P>\n<P><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I was looking at System Restore on my Windows Vista computer, and I had an idea. Wouldn\u2019t 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[423,3,723,45,725],"class_list":["post-53583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-backup-and-system-restore","tag-scripting-guy","tag-system-restore","tag-windows-powershell","tag-windows-vista"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I was looking at System Restore on my Windows Vista computer, and I had an idea. Wouldn\u2019t 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=53583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=53583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}