diff --git a/.bin/Scripts/functions/windows_updates.py b/.bin/Scripts/functions/windows_updates.py index 4257f066..ee7ec279 100644 --- a/.bin/Scripts/functions/windows_updates.py +++ b/.bin/Scripts/functions/windows_updates.py @@ -4,27 +4,66 @@ from functions.common import * # Functions +def delete_folder(folder_path): + """Near-useless wrapper for shutil.rmtree.""" + shutil.rmtree(folder_path) + + def disable_service(service_name): """Set service startup to 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(): """Disable windows updates and clear SoftwareDistribution folder.""" + indent=2 + width=52 update_folders = [ r'{WINDIR}\SoftwareDistribution'.format(**global_vars['Env']), r'{SYSTEMDRIVE}\$WINDOWS.~BT'.format(**global_vars['Env']), ] - # Stop services for service in ('wuauserv', 'bits'): - stop_service(service) - disable_service(service) + # Stop 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 for folder_path in update_folders: 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): @@ -37,14 +76,59 @@ def enable_service(service_name): def enable_windows_updates(): """Enable windows updates""" - enable_service('bits') - enable_service('wuauserv') + indent=2 + 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): """Stop service.""" 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__': print("This file is not meant to be called directly.") diff --git a/.bin/Scripts/settings/launchers.py b/.bin/Scripts/settings/launchers.py index 0360aac1..48f73388 100644 --- a/.bin/Scripts/settings/launchers.py +++ b/.bin/Scripts/settings/launchers.py @@ -81,14 +81,14 @@ LAUNCHERS = { 'L_TYPE': 'PyScript', 'L_PATH': 'Scripts', 'L_ITEM': 'windows_updates.py', - 'L_ARGS': '--disable', + 'L_ARGS': 'd7mode --disable', 'L_ELEV': 'True', }, 'Enable Windows Updates': { 'L_TYPE': 'PyScript', 'L_PATH': 'Scripts', 'L_ITEM': 'windows_updates.py', - 'L_ARGS': '--enable', + 'L_ARGS': 'd7mode --enable', 'L_ELEV': 'True', }, }, diff --git a/.bin/Scripts/windows_updates.py b/.bin/Scripts/windows_updates.py index 53ab7172..a9856cd0 100644 --- a/.bin/Scripts/windows_updates.py +++ b/.bin/Scripts/windows_updates.py @@ -9,6 +9,7 @@ from functions.windows_updates import * init_global_vars() os.system('title {}: Windows Updates Tool'.format(KIT_NAME_FULL)) set_log_file('Windows Updates Tool.log') +D7_MODE = 'd7mode' in sys.argv if __name__ == '__main__': try: @@ -17,29 +18,31 @@ if __name__ == '__main__': # Check args if '--disable' in sys.argv: - result = try_and_print( - message='Disabling Windows Updates...', - function=disable_windows_updates) + disable_windows_updates() elif '--enable' in sys.argv: - result = try_and_print( - message='Enabling Windows Updates...', - function=enable_windows_updates) + enable_windows_updates() else: print_error('Bad mode.') 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 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: pass except: