Search code examples
windowsvbscriptwmisystem-restore

Enable system protection using visual basic script on windows 8/10


I am trying to enable system protection using following visual basic script on windows 10. But it gives an error "access denied". I have searched microsoft documentation about WMI classes and it feels like that following code is perfectly fine for windows 7 or xp but not for windows 10 and after spending a lot of time while looking for documentation on microsoft's website I have not been able to find any clue of how to do this.

Note: I am looking for a visual basic script ony that enables protection on windows 10 if it is not.

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\default")

Set objItem = objWMIService.Get("SystemRestore")
errResults = objItem.Enable("D:\")

'Inform User that the task is done.
Mybox = MsgBox("System Restore is now enabled on "& strComputer &""  & 
vbCRLF ,vbOkOnly,"System Restore Enabled")

Solution

  • I have got it figured. The following code successfully enables system protection in windows 10.

    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("D:" & "\")