Jump to content

How to check if system protection is enabled on a drive


Recommended Posts

I am using the following code in my visual basic script to enable system protection in windows 10 and it works perfectly fine. I am looking for a visual basic code that checks if protection on particular drive is enabled or not?

and Sets Max usage to 15%. 

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If
Dim oSR

Set oSR = GetObject("winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore")


  oSR.Enable("C:" & "\")

If someone can highlight the version of windows this code is compatible for, that would be great. :)

Edited by maniG
Link to comment
Share on other sites


I am not sure what you want but using this reference  https://msdn.microsoft.com/en-us/library/windows/desktop/aa378858(v=vs.85).aspx

I put together this script run it and see if it what you need.

Dim d,oWMI, oSR, colItem, objItem
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set oSR = GetObject("winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore")
Set colItem = oWMI.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3")
On Error Resume Next 
  For Each objItem In colItem 
   d = objItem.Name & "\"
   If (oSR.Enable(d)) = 0 Then
    wscript.Echo "Success" & vbTab & d
   Else 
    wscript.Echo "Failed " & vbTab & d
   End If
  Next
Link to comment
Share on other sites

To check if system protection is enabled, I would probably use the registry to determine if RPSessionInterval is greater than 0.

In a batch file to enable system protection on the OS drive with a maximum 15% size I would try this As administrator:

@ECHO OFF
SET "rKey=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore"
SET "rVal=RPSessionInterval"
SET "dVal="
FOR /F "EOL=H Tokens=2*" %%# IN ('REG Query "%rKey%" /V "%rVal%" 2^>NUL'
) DO SET/A "dVal=%%$"
IF NOT DEFINED dVal (ECHO ERROR! & PAUSE & EXIT/B)
IF %dVal% EQU 0 (
	WMIC /NameSpace:\\root\default PATH SystemRestore CALL Enable %SystemDrive%)
VSSADMIN Resize Shadowstorage /For=%SystemDrive% /On=%SystemDrive% /MaxSize=15%%

This is untested, (I am not telling you to change scripting language, just showing you a method of detection of the state of the mechanism).

Link to comment
Share on other sites

On 6/16/2017 at 9:37 PM, gunsmokingman said:

I am not sure what you want but using this reference  https://msdn.microsoft.com/en-us/library/windows/desktop/aa378858(v=vs.85).aspx

I put together this script run it and see if it what you need.


Dim d,oWMI, oSR, colItem, objItem
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set oSR = GetObject("winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore")
Set colItem = oWMI.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3")
On Error Resume Next 
  For Each objItem In colItem 
   d = objItem.Name & "\"
   If (oSR.Enable(d)) = 0 Then
    wscript.Echo "Success" & vbTab & d
   Else 
    wscript.Echo "Failed " & vbTab & d
   End If
  Next

Hey thankyou for the answer. I have made changes to the code. Basically before we create a restore point, system restore must be enabled and I wanted to know a way to check if it is enabled or not.

Link to comment
Share on other sites

10 hours ago, Yzöwl said:

To check if system protection is enabled, I would probably use the registry to determine if RPSessionInterval is greater than 0.

In a batch file to enable system protection on the OS drive with a maximum 15% size I would try this As administrator:


@ECHO OFF
SET "rKey=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore"
SET "rVal=RPSessionInterval"
SET "dVal="
FOR /F "EOL=H Tokens=2*" %%# IN ('REG Query "%rKey%" /V "%rVal%" 2^>NUL'
) DO SET/A "dVal=%%$"
IF NOT DEFINED dVal (ECHO ERROR! & PAUSE & EXIT/B)
IF %dVal% EQU 0 (
	WMIC /NameSpace:\\root\default PATH SystemRestore CALL Enable %SystemDrive%)
VSSADMIN Resize Shadowstorage /For=%SystemDrive% /On=%SystemDrive% /MaxSize=15%%

This is untested, (I am not telling you to change scripting language, just showing you a method of detection of the state of the mechanism).

That seems to be a good way to check. So far what I have done is my script will attempt to create a restore point and in case of error it will call a function that will enable protection and then again create a restore point or will exit in case of success. Thank you for your help. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...