Page 1 sur 1

Partage : Reset WindowsUpdate

Publié : 09 juin 2022 - 14:10
par t.heroult
Bonjour à tous.
Je partage avec vous un petit paquet que j'ai développé pour résoudre pas mal de problèmes avec WindowsUpdate.
En fait, c'est une compilation des principales actions connues pour dépanner un WindowsUpdate défaillant.

Actions réalisées :
  • Flush des DNS
  • Purges :
  • - Fichiers "qmgr*.dat" dans ALLUSERSPROFILE/Application Data/Microsoft/Network/Downloader
  • - Fichiers "qmgr*.dat" dans ALLUSERSPROFILE/Microsoft/Network/Downloader
  • - Tous les fichiers dans WINDIR/Logs/WindowsUpdate
  • Suppressions de dossiers (ils seront reconstruits au lancement du service Windows Update) :
  • - WINDIR/SoftwareDistribution
  • - SYSTEM32/Catroot2
  • Suppression de clé de registre :
  • - HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate
  • - HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\WindowsUpdate
  • GPUPDATE (au niveau machine)
  • Reset du service "bits"
  • Reset du service "Windows Update"
  • Ré-enregistrement de quelques dlls
  • Réinitialisation du winsock
  • Configuration du démarrage des services
  • Redémarrage si nécessaire des services
  • Si Waptwua est actif, alors désactive le service Windows Update
Et voici le code :

Code : Tout sélectionner

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)
Cordialement,
Tom

Re: Partage : Reset WindowsUpdate

Publié : 09 juin 2022 - 16:46
par erenodau
Bonjour,

Merci pour le partage, ça à l'air fort intéressant !

Bonne journée et à bientôt !