parent
5aeeb41995
commit
e590547e3c
3 changed files with 111 additions and 24 deletions
|
|
@ -4,27 +4,66 @@ from functions.common import *
|
||||||
|
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
|
def delete_folder(folder_path):
|
||||||
|
"""Near-useless wrapper for shutil.rmtree."""
|
||||||
|
shutil.rmtree(folder_path)
|
||||||
|
|
||||||
|
|
||||||
def disable_service(service_name):
|
def disable_service(service_name):
|
||||||
"""Set service startup to disabled."""
|
"""Set service startup to disabled."""
|
||||||
run_program(['sc', 'config', service_name, 'start=', 'disabled'])
|
run_program(['sc', 'config', service_name, 'start=', 'disabled'])
|
||||||
|
|
||||||
|
# Verify service was disabled
|
||||||
|
start_type = get_service_start_type(service_name)
|
||||||
|
if not start_type.lower().startswith('disabled'):
|
||||||
|
raise GenericError('Failed to disable service {}'.format(service_name))
|
||||||
|
|
||||||
|
|
||||||
def disable_windows_updates():
|
def disable_windows_updates():
|
||||||
"""Disable windows updates and clear SoftwareDistribution folder."""
|
"""Disable windows updates and clear SoftwareDistribution folder."""
|
||||||
|
indent=2
|
||||||
|
width=52
|
||||||
update_folders = [
|
update_folders = [
|
||||||
r'{WINDIR}\SoftwareDistribution'.format(**global_vars['Env']),
|
r'{WINDIR}\SoftwareDistribution'.format(**global_vars['Env']),
|
||||||
r'{SYSTEMDRIVE}\$WINDOWS.~BT'.format(**global_vars['Env']),
|
r'{SYSTEMDRIVE}\$WINDOWS.~BT'.format(**global_vars['Env']),
|
||||||
]
|
]
|
||||||
|
|
||||||
# Stop services
|
|
||||||
for service in ('wuauserv', 'bits'):
|
for service in ('wuauserv', 'bits'):
|
||||||
stop_service(service)
|
# Stop service
|
||||||
disable_service(service)
|
result = try_and_print(
|
||||||
|
'Stopping service {}...'.format(service),
|
||||||
|
indent=indent, width=width,
|
||||||
|
function=stop_service, service_name=service)
|
||||||
|
if not result['CS']:
|
||||||
|
result = try_and_print(
|
||||||
|
'Stopping service {}...'.format(service),
|
||||||
|
indent=indent, width=width,
|
||||||
|
function=stop_service, service_name=service)
|
||||||
|
if not result['CS']:
|
||||||
|
raise GenericError('Service {} could not be stopped.'.format(service))
|
||||||
|
|
||||||
|
# Disable service
|
||||||
|
result = try_and_print(
|
||||||
|
'Disabling service {}...'.format(service),
|
||||||
|
indent=indent, width=width,
|
||||||
|
function=disable_service, service_name=service)
|
||||||
|
if not result['CS']:
|
||||||
|
result = try_and_print(
|
||||||
|
'Disabling service {}...'.format(service),
|
||||||
|
indent=indent, width=width,
|
||||||
|
function=disable_service, service_name=service)
|
||||||
|
if not result['CS']:
|
||||||
|
raise GenericError('Service {} could not be disabled.'.format(service))
|
||||||
|
|
||||||
# Delete update folders
|
# Delete update folders
|
||||||
for folder_path in update_folders:
|
for folder_path in update_folders:
|
||||||
if os.path.exists(folder_path):
|
if os.path.exists(folder_path):
|
||||||
shutil.rmtree(folder_path)
|
result = try_and_print(
|
||||||
|
'Deleting folder {}...'.format(folder_path),
|
||||||
|
indent=indent, width=width,
|
||||||
|
function=delete_folder, folder_path=folder_path)
|
||||||
|
if not result['CS']:
|
||||||
|
raise GenericError('Failed to remove folder {}'.format(folder_path))
|
||||||
|
|
||||||
|
|
||||||
def enable_service(service_name):
|
def enable_service(service_name):
|
||||||
|
|
@ -37,14 +76,59 @@ def enable_service(service_name):
|
||||||
|
|
||||||
def enable_windows_updates():
|
def enable_windows_updates():
|
||||||
"""Enable windows updates"""
|
"""Enable windows updates"""
|
||||||
enable_service('bits')
|
indent=2
|
||||||
enable_service('wuauserv')
|
width=52
|
||||||
|
|
||||||
|
for service in ('bits', 'wuauserv'):
|
||||||
|
# Enable service
|
||||||
|
result = try_and_print(
|
||||||
|
'Enabling service {}...'.format(service),
|
||||||
|
indent=indent, width=width,
|
||||||
|
function=enable_service, service_name=service)
|
||||||
|
if not result['CS']:
|
||||||
|
result = try_and_print(
|
||||||
|
'Enabling service {}...'.format(service),
|
||||||
|
indent=indent, width=width,
|
||||||
|
function=enable_service, service_name=service)
|
||||||
|
if not result['CS']:
|
||||||
|
raise GenericError('Service {} could not be enabled.'.format(service))
|
||||||
|
|
||||||
|
|
||||||
|
def get_service_status(service_name):
|
||||||
|
"""Get service status using psutil, returns str."""
|
||||||
|
status = 'Unknown'
|
||||||
|
try:
|
||||||
|
service = psutil.win_service_get(service_name)
|
||||||
|
status = service.status()
|
||||||
|
except psutil.NoSuchProcess:
|
||||||
|
# Ignore and return 'Unknown' below
|
||||||
|
pass
|
||||||
|
|
||||||
|
return status
|
||||||
|
|
||||||
|
|
||||||
|
def get_service_start_type(service_name):
|
||||||
|
"""Get service startup type using psutil, returns str."""
|
||||||
|
start_type = 'Unknown'
|
||||||
|
try:
|
||||||
|
service = psutil.win_service_get(service_name)
|
||||||
|
start_type = service.start_type()
|
||||||
|
except psutil.NoSuchProcess:
|
||||||
|
# Ignore and return 'Unknown' below
|
||||||
|
pass
|
||||||
|
|
||||||
|
return start_type
|
||||||
|
|
||||||
|
|
||||||
def stop_service(service_name):
|
def stop_service(service_name):
|
||||||
"""Stop service."""
|
"""Stop service."""
|
||||||
run_program(['net', 'stop', service_name], check=False)
|
run_program(['net', 'stop', service_name], check=False)
|
||||||
|
|
||||||
|
# Verify service was stopped
|
||||||
|
status = get_service_status(service_name)
|
||||||
|
if not status.lower().startswith('stopped'):
|
||||||
|
raise GenericError('Failed to stop service {}'.format(service_name))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print("This file is not meant to be called directly.")
|
print("This file is not meant to be called directly.")
|
||||||
|
|
|
||||||
|
|
@ -81,14 +81,14 @@ LAUNCHERS = {
|
||||||
'L_TYPE': 'PyScript',
|
'L_TYPE': 'PyScript',
|
||||||
'L_PATH': 'Scripts',
|
'L_PATH': 'Scripts',
|
||||||
'L_ITEM': 'windows_updates.py',
|
'L_ITEM': 'windows_updates.py',
|
||||||
'L_ARGS': '--disable',
|
'L_ARGS': 'd7mode --disable',
|
||||||
'L_ELEV': 'True',
|
'L_ELEV': 'True',
|
||||||
},
|
},
|
||||||
'Enable Windows Updates': {
|
'Enable Windows Updates': {
|
||||||
'L_TYPE': 'PyScript',
|
'L_TYPE': 'PyScript',
|
||||||
'L_PATH': 'Scripts',
|
'L_PATH': 'Scripts',
|
||||||
'L_ITEM': 'windows_updates.py',
|
'L_ITEM': 'windows_updates.py',
|
||||||
'L_ARGS': '--enable',
|
'L_ARGS': 'd7mode --enable',
|
||||||
'L_ELEV': 'True',
|
'L_ELEV': 'True',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ from functions.windows_updates import *
|
||||||
init_global_vars()
|
init_global_vars()
|
||||||
os.system('title {}: Windows Updates Tool'.format(KIT_NAME_FULL))
|
os.system('title {}: Windows Updates Tool'.format(KIT_NAME_FULL))
|
||||||
set_log_file('Windows Updates Tool.log')
|
set_log_file('Windows Updates Tool.log')
|
||||||
|
D7_MODE = 'd7mode' in sys.argv
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
|
|
@ -17,29 +18,31 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
# Check args
|
# Check args
|
||||||
if '--disable' in sys.argv:
|
if '--disable' in sys.argv:
|
||||||
result = try_and_print(
|
disable_windows_updates()
|
||||||
message='Disabling Windows Updates...',
|
|
||||||
function=disable_windows_updates)
|
|
||||||
elif '--enable' in sys.argv:
|
elif '--enable' in sys.argv:
|
||||||
result = try_and_print(
|
enable_windows_updates()
|
||||||
message='Enabling Windows Updates...',
|
|
||||||
function=enable_windows_updates)
|
|
||||||
else:
|
else:
|
||||||
print_error('Bad mode.')
|
print_error('Bad mode.')
|
||||||
abort()
|
abort()
|
||||||
|
|
||||||
# Check for errors
|
|
||||||
if not result['CS']:
|
|
||||||
for line in str(result['Error']).splitlines():
|
|
||||||
print_standard(line)
|
|
||||||
print_standard(' ')
|
|
||||||
print_error('Error(s) encountered, see above.')
|
|
||||||
print_standard(' ')
|
|
||||||
pause('Press Enter to exit... ')
|
|
||||||
exit_script(1)
|
|
||||||
|
|
||||||
# Done
|
# Done
|
||||||
exit_script()
|
exit_script()
|
||||||
|
except GenericError as err:
|
||||||
|
# Failed to complete request, show error(s) and prompt tech
|
||||||
|
print_standard(' ')
|
||||||
|
for line in str(err).splitlines():
|
||||||
|
print_warning(line)
|
||||||
|
print_standard(' ')
|
||||||
|
print_error('Error(s) encountered, see above.')
|
||||||
|
print_standard(' ')
|
||||||
|
if '--disable' in sys.argv:
|
||||||
|
if D7_MODE:
|
||||||
|
print_warning('Please disable d7II auto mode!')
|
||||||
|
print_standard('Then reboot, re-enable this step, and try again.')
|
||||||
|
else:
|
||||||
|
print_standard('Please reboot and try again.')
|
||||||
|
pause('Press Enter to exit... ')
|
||||||
|
exit_script(1)
|
||||||
except SystemExit:
|
except SystemExit:
|
||||||
pass
|
pass
|
||||||
except:
|
except:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue