drive=“c:” #Provide the drive which you need to enable Restore Access
drive_percentage=‘10%’ #Provide the percentage which you need to set
import subprocess
import os
import ctypes
cmd=“vssadmin List ShadowStorage”
cmd= subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr = subprocess.PIPE,shell=True)
out=cmd.communicate()[0]
output = str(out)
def ecmd(command):
from subprocess import PIPE, Popen
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def enter(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def exit(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
with disable_file_system_redirection():
obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
out, err = obj.communicate()
ret=obj.returncode
if ret==0:
if out:
return out.strip()
else:
return ret
else:
if err:
return err.strip()
else:
return ret
def enable():
ps_content=r’’’
enable-computerrestore -drive "%s"
vssadmin resize shadowstorage /on=c: /for=c: /maxsize=%s
checkpoint-computer -description "Done"
'''%(drive,drive_percentage)
file_name='powershell_file.ps1'
file_path=os.path.join(os.environ['PROGRAMDATA'], file_name)
with open(file_path, 'wb') as wr:
wr.write(ps_content)
ecmd('powershell "Set-ExecutionPolicy RemoteSigned"')
val=ecmd('powershell "%s"'%file_path)
if "WARNING:" in val:
print "A new system restore point cannot be created because one has already been created within the past 24 hours. Please try again later..."
else:
print "Restore point has been successfully enabled with percentage..."
os.remove(file_path)
def disable():
ps_content=r’’’
disable-computerrestore -drive "%s"
'''%drive
file_name_1='powershell_file.ps1'
file_path=os.path.join(os.environ['PROGRAMDATA'], file_name)
with open(file_path, 'wb') as wr:
wr.write(ps_content)
ecmd('powershell "Set-ExecutionPolicy RemoteSigned"')
ecmd('powershell "%s"'%file_path)
os.remove(file_path)
if “No items found that satisfy the query.” in output:
print “Restore point not enabled in this machine”
enable()
elif “Shadow Copy Storage association” in output:
print “Restore point already enabled in this machine”
disable()
enable()
elif “Error: A Volume Shadow Copy Service component encountered an unexpected error.” in output:
print “Restore point already enabled in this machine”
disable()
enable()
elif “Error: You don’t have the correct permissions to run this command. Please run this utility from a command” in output:
print “Run this script as System User”