Page 1 of 1

Share: Reset Windows Update

Published: June 9, 2022 - 2:10 PM
by t.heroult
Hello everyone.
I'm sharing with you a small package I developed to solve quite a few problems with WindowsUpdate.
In fact, it's a compilation of the main actions known to troubleshoot a failing WindowsUpdate.

Actions taken:
  • DNS Flush
  • Purges:
  • - "qmgr*.dat" files in ALLUSERSPROFILE/Application Data/Microsoft/Network/Downloader
  • - "qmgr*.dat" files in ALLUSERSPROFILE/Microsoft/Network/Downloader
  • - All files in WINDIR/Logs/WindowsUpdate
  • Folder deletions (they will be rebuilt when the Windows Update service starts):
  • - WINDIR/SoftwareDistribution
  • - SYSTEM32/Catroot2
  • Registry key deletion:
  • - HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate
  • - HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\WindowsUpdate
  • GPUPDATE (at the machine level)
  • Reset the "bits" service
  • Resetting the “Windows Update” service
  • Re-registering some DLLs
  • Winsock reset
  • Configuring service startup
  • Restart services if necessary
  • If Waptwua is active, then disable the Windows Update service
And here is the code:

Code: Select all

def install():
    print("Arrêt des services :")
    Stop_Service_if_needed('bits',"- Background Intelligent Transfer Service (bits).")
    Stop_Service_if_needed('wuauserv',"- Windows Update (wuauserv).")
    Stop_Service_if_needed('appidsvc',"- Application Identity (appidsvc).")
    Stop_Service_if_needed('cryptsvc',"- Cryptographic Services (cryptsvc).")

    print("Flush DNS")
    run("Ipconfig /flushdns")
    dossier = makepath(os.environ.get('ALLUSERSPROFILE'),'Application Data','Microsoft','Network','Downloader')
    if isdir(dossier):
        fichiers = find_all_files(dossier,include_patterns='qmgr*.dat')
        print('Purge des fichiers "qmgr*.dat" de ' + dossier)
        for fichier in fichiers:
            remove_file(fichier)
            print("Suppression de : " + fichier)

    dossier = makepath(os.environ.get('ALLUSERSPROFILE'),'Microsoft','Network','Downloader')
    if isdir(dossier):
        fichiers = find_all_files(dossier,include_patterns='qmgr*.dat')
        print('Purge des fichiers "qmgr*.dat" de ' + dossier)
        for fichier in fichiers:
            remove_file(fichier)
            print("Suppression de : " + fichier)

    dossier = makepath(os.environ.get('WINDIR'),'Logs','WindowsUpdate')
    if isdir(dossier):
        fichiers = find_all_files(dossier)
        print('Purge du dossier ' + dossier)
        for fichier in fichiers:
            remove_file(fichier)
            print("Suppression de : " + fichier)

    f_pending_xml = makepath(os.environ.get('WINDIR'),'winsxs','pending.xml')
    if isfile(f_pending_xml + '.bak'):
        remove_file(f_pending_xml + '.bak')
    if isfile(f_pending_xml):
        print("sauvegarde de " + f_pending_xml)
        os.rename(f_pending_xml,f_pending_xml + '.bak')

    dossier = makepath(os.environ.get('WINDIR'),'SoftwareDistribution')
    if isdir(dossier):
        print("Suppression de " + dossier)
        remove_tree(dossier)

    dossier = makepath(system32,'Catroot2')
    if isdir(dossier):
        print("Suppression de " + dossier)
        remove_tree(dossier)

    if reg_key_exists(HKEY_LOCAL_MACHINE,'SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate'):
        print('Purge de la clé de registe : HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate')
        registry_deletekey(HKEY_LOCAL_MACHINE,'SOFTWARE\Policies\Microsoft\Windows','WindowsUpdate')
    if reg_key_exists(HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\WindowsUpdate'):
        print('Purge de la clé de registe : HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\WindowsUpdate')
        registry_deletekey(HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Policies','WindowsUpdate')

    print("GPUPDATE (ordinateur)")
    run('gpupdate /force /target:computer')

    print('Reset du service "bits"')
    run('sc.exe sdset bits D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)')
    print('Reset du service "Windows Update"')
    run('sc.exe sdset wuauserv D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)')

    lst_dlls = ['atl.dll','urlmon.dll','mshtml.dll','shdocvw.dll','browseui.dll','jscript.dll','vbscript.dll','scrrun.dll','msxml.dll','msxml3.dll','msxml6.dll','actxprxy.dll','softpub.dll','wintrust.dll','dssenh.dll','rsaenh.dll','gpkcsp.dll','sccbase.dll','slbcsp.dll','cryptdlg.dll','oleaut32.dll','ole32.dll','shell32.dll','initpki.dll','wuapi.dll','wuaueng.dll','wuaueng1.dll','wucltui.dll','wups.dll','wups2.dll','wuweb.dll','qmgr.dll','qmgrprxy.dll','wucltux.dll','muweb.dll','wuwebv.dll','wudriver.dll']
    print("Ré-enregistrement de quelques dlls.")
    for f_dll in lst_dlls:
        fichier = makepath(system32,f_dll)
        run_notfatal('regsvr32.exe /s %s' % fichier)

    print("Réinitialisation du winsock.")
    run('netsh winsock reset')
    run('netsh winsock reset proxy')

    print("Configuration des services")
    run_notfatal('sc config wuauserv start= auto')
    run_notfatal('sc config bits start= auto ')
    run_notfatal('sc config DcomLaunch start= auto')


    print("Redémarrage des services selon leur propriété 'startup'")
    Start_Service_if_needed('bits',"- Background Intelligent Transfer Service (bits).")
    Start_Service_if_needed('wuauserv',"- Windows Update (wuauserv).")
    Start_Service_if_needed('appidsvc',"- Application Identity (appidsvc).")
    Start_Service_if_needed('cryptsvc',"- Cryptographic Services (cryptsvc).")

    # Si WAPTwua est actif sur la cible, alors désactivation et arrêt du service Windows Update
    if inifile_hassection(WAPT.config_filename,'waptwua'):
        waptwua_setting = inifile_readstring(WAPT.config_filename,'waptwua','enabled')
        if (waptwua_setting == 'True' or waptwua_setting == '1'):
            print("Waptwua est activé sur ce poste.")
            run_notfatal('sc config wuauserv start= disabled')
            Stop_Service_if_needed('wuauserv',"- Désactivation et arrêt du service Windows Update.")

    pass
    # put here what to do when package is installed on host
    # implicit context variables are WAPT, basedir, control, user, params, run

def Start_Service_if_needed(Service,DisplayInfo):
    if service_installed(Service):
        if not service_is_running(Service):
            key = reg_openkey_noredir(HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Services\%s' % (Service),KEY_READ)
            startup = reg_getvalue(key,'Start')
            if startup == '2':
                print(DisplayInfo)
                service_start(Service)

def Stop_Service_if_needed(Service,DisplayInfo):
    if service_installed(Service):
        if service_is_running(Service):
            print(DisplayInfo)
            service_stop(Service)
Sincerely,
Tom

Re: Sharing: Reset WindowsUpdate

Published: June 9, 2022 - 4:46 PM
by erenodau
Hello,

thank you for sharing, it looks very interesting!

Have a good day and see you soon!