Reoder Windows functions
This commit is contained in:
parent
f1592b92a6
commit
759cd12379
1 changed files with 102 additions and 100 deletions
|
|
@ -56,7 +56,7 @@ REG_MSISERVER = r'HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\MSIServ
|
|||
SLMGR = pathlib.Path(f'{os.environ.get("SYSTEMROOT")}/System32/slmgr.vbs')
|
||||
|
||||
|
||||
# Functions
|
||||
# Activation Functions
|
||||
def activate_with_bios():
|
||||
"""Attempt to activate Windows with a key stored in the BIOS."""
|
||||
# Code borrowed from https://github.com/aeruder/get_win8key
|
||||
|
|
@ -96,36 +96,6 @@ def activate_with_bios():
|
|||
raise GenericError('Activation Failed')
|
||||
|
||||
|
||||
def disable_safemode():
|
||||
"""Edit BCD to remove safeboot value."""
|
||||
cmd = ['bcdedit', '/deletevalue', '{default}', 'safeboot']
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
def disable_safemode_msi():
|
||||
"""Disable MSI access under safemode."""
|
||||
cmd = ['reg', 'delete', REG_MSISERVER, '/f']
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
def enable_safemode():
|
||||
"""Edit BCD to set safeboot as default."""
|
||||
cmd = ['bcdedit', '/set', '{default}', 'safeboot', 'network']
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
def enable_safemode_msi():
|
||||
"""Enable MSI access under safemode."""
|
||||
cmd = ['reg', 'add', REG_MSISERVER, '/f']
|
||||
run_program(cmd)
|
||||
cmd = [
|
||||
'reg', 'add', REG_MSISERVER, '/ve',
|
||||
'/t', 'REG_SZ',
|
||||
'/d', 'Service', '/f',
|
||||
]
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
def get_activation_string():
|
||||
"""Get activation status, returns str."""
|
||||
cmd = ['cscript', '//nologo', SLMGR, '/xpr']
|
||||
|
|
@ -144,75 +114,6 @@ def is_activated():
|
|||
return act_str and 'permanent' in act_str
|
||||
|
||||
|
||||
def run_chkdsk_offline():
|
||||
"""Set filesystem 'dirty bit' to force a CHKDSK during startup."""
|
||||
cmd = f'fsutil dirty set {os.environ.get("SYSTEMDRIVE")}'
|
||||
proc = run_program(cmd.split(), check=False)
|
||||
|
||||
# Check result
|
||||
if proc.returncode > 0:
|
||||
raise GenericError('Failed to set dirty bit.')
|
||||
|
||||
|
||||
def run_chkdsk_online():
|
||||
"""Run CHKDSK in a split window.
|
||||
|
||||
NOTE: If run on Windows 8+ online repairs are attempted.
|
||||
"""
|
||||
cmd = ['CHKDSK', os.environ.get('SYSTEMDRIVE', 'C:')]
|
||||
if OS_VERSION >= 8:
|
||||
cmd.extend(['/scan', '/perf'])
|
||||
log_path = format_log_path(log_name='CHKDSK', tool=True)
|
||||
err_path = log_path.with_suffix('.err')
|
||||
|
||||
# Run scan
|
||||
proc = run_program(cmd, check=False)
|
||||
|
||||
# Check result
|
||||
if proc.returncode == 1:
|
||||
raise GenericWarning('Repaired (or manually aborted)')
|
||||
if proc.returncode > 1:
|
||||
raise GenericError('Issue(s) detected')
|
||||
|
||||
# Save output
|
||||
os.makedirs(log_path.parent, exist_ok=True)
|
||||
with open(log_path, 'w') as _f:
|
||||
_f.write(proc.stdout)
|
||||
with open(err_path, 'w') as _f:
|
||||
_f.write(proc.stderr)
|
||||
|
||||
|
||||
def run_sfc_scan():
|
||||
"""Run SFC and save results."""
|
||||
cmd = ['sfc', '/scannow']
|
||||
log_path = format_log_path(log_name='SFC', tool=True)
|
||||
err_path = log_path.with_suffix('.err')
|
||||
|
||||
# Run SFC
|
||||
proc = run_program(cmd, check=False, encoding='utf-16')
|
||||
|
||||
# Fix paths
|
||||
log_path = non_clobber_path(log_path)
|
||||
err_path = non_clobber_path(err_path)
|
||||
|
||||
# Save output
|
||||
os.makedirs(log_path.parent, exist_ok=True)
|
||||
with open(log_path, 'w') as _f:
|
||||
_f.write(proc.stdout)
|
||||
with open(err_path, 'w') as _f:
|
||||
_f.write(proc.stderr)
|
||||
|
||||
# Check result
|
||||
if 'did not find any integrity violations' in proc.stdout:
|
||||
pass
|
||||
elif 'successfully repaired' in proc.stdout:
|
||||
raise GenericWarning('Repaired')
|
||||
elif 'found corrupt files' in proc.stdout:
|
||||
raise GenericError('Corruption detected')
|
||||
else:
|
||||
raise OSError
|
||||
|
||||
|
||||
# Registry Functions
|
||||
def reg_delete_key(hive, key, recurse=False):
|
||||
# pylint: disable=raise-missing-from
|
||||
|
|
@ -407,5 +308,106 @@ def reg_set_value(hive, key, name, data, data_type, option=None):
|
|||
winreg.SetValue(hive, key, data_type, data)
|
||||
|
||||
|
||||
# Repair Functions
|
||||
def run_chkdsk_offline():
|
||||
"""Set filesystem 'dirty bit' to force a CHKDSK during startup."""
|
||||
cmd = f'fsutil dirty set {os.environ.get("SYSTEMDRIVE")}'
|
||||
proc = run_program(cmd.split(), check=False)
|
||||
|
||||
# Check result
|
||||
if proc.returncode > 0:
|
||||
raise GenericError('Failed to set dirty bit.')
|
||||
|
||||
|
||||
def run_chkdsk_online():
|
||||
"""Run CHKDSK in a split window.
|
||||
|
||||
NOTE: If run on Windows 8+ online repairs are attempted.
|
||||
"""
|
||||
cmd = ['CHKDSK', os.environ.get('SYSTEMDRIVE', 'C:')]
|
||||
if OS_VERSION >= 8:
|
||||
cmd.extend(['/scan', '/perf'])
|
||||
log_path = format_log_path(log_name='CHKDSK', tool=True)
|
||||
err_path = log_path.with_suffix('.err')
|
||||
|
||||
# Run scan
|
||||
proc = run_program(cmd, check=False)
|
||||
|
||||
# Check result
|
||||
if proc.returncode == 1:
|
||||
raise GenericWarning('Repaired (or manually aborted)')
|
||||
if proc.returncode > 1:
|
||||
raise GenericError('Issue(s) detected')
|
||||
|
||||
# Save output
|
||||
os.makedirs(log_path.parent, exist_ok=True)
|
||||
with open(log_path, 'w') as _f:
|
||||
_f.write(proc.stdout)
|
||||
with open(err_path, 'w') as _f:
|
||||
_f.write(proc.stderr)
|
||||
|
||||
|
||||
def run_sfc_scan():
|
||||
"""Run SFC and save results."""
|
||||
cmd = ['sfc', '/scannow']
|
||||
log_path = format_log_path(log_name='SFC', tool=True)
|
||||
err_path = log_path.with_suffix('.err')
|
||||
|
||||
# Run SFC
|
||||
proc = run_program(cmd, check=False, encoding='utf-16')
|
||||
|
||||
# Fix paths
|
||||
log_path = non_clobber_path(log_path)
|
||||
err_path = non_clobber_path(err_path)
|
||||
|
||||
# Save output
|
||||
os.makedirs(log_path.parent, exist_ok=True)
|
||||
with open(log_path, 'w') as _f:
|
||||
_f.write(proc.stdout)
|
||||
with open(err_path, 'w') as _f:
|
||||
_f.write(proc.stderr)
|
||||
|
||||
# Check result
|
||||
if 'did not find any integrity violations' in proc.stdout:
|
||||
pass
|
||||
elif 'successfully repaired' in proc.stdout:
|
||||
raise GenericWarning('Repaired')
|
||||
elif 'found corrupt files' in proc.stdout:
|
||||
raise GenericError('Corruption detected')
|
||||
else:
|
||||
raise OSError
|
||||
|
||||
|
||||
# Safe Mode Functions
|
||||
def disable_safemode():
|
||||
"""Edit BCD to remove safeboot value."""
|
||||
cmd = ['bcdedit', '/deletevalue', '{default}', 'safeboot']
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
def disable_safemode_msi():
|
||||
"""Disable MSI access under safemode."""
|
||||
cmd = ['reg', 'delete', REG_MSISERVER, '/f']
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
def enable_safemode():
|
||||
"""Edit BCD to set safeboot as default."""
|
||||
cmd = ['bcdedit', '/set', '{default}', 'safeboot', 'network']
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
def enable_safemode_msi():
|
||||
"""Enable MSI access under safemode."""
|
||||
cmd = ['reg', 'add', REG_MSISERVER, '/f']
|
||||
run_program(cmd)
|
||||
cmd = [
|
||||
'reg', 'add', REG_MSISERVER, '/ve',
|
||||
'/t', 'REG_SZ',
|
||||
'/d', 'Service', '/f',
|
||||
]
|
||||
run_program(cmd)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("This file is not meant to be called directly.")
|
||||
|
|
|
|||
Loading…
Reference in a new issue