Uninstall specific KB Updates.

Hi,
Please refer the script for uninstalling the specific KB_updates from your system.
This script will display the list out KB Update packages present in your system, If you provide any specific update number in the below script then, it will uninstall it. or Otherwise, If you doesn’t provide any kb_update package in the below script then it will display the list of packages presnt in it and Finally, it will ask you to provide any specific kb_update package to uninstall.

NOTE: For some KB_packages there is no option for uninstallation, then this script will display the "Error failed to uninstalled Package[package name]'

Instructions:

1.First time when you run the script:: It will display the list of kb_updates present in your system and then It will give the message like ‘Please provide kb_update number in the kb_list_user list’, If there is No kb_update package present in your system then it will display like this 'No KB_UPDATES for security/update found in your system '
2. From the output you will get to know the kb_update number from the list of packages, just have to give the KB number
For Example:

Package_for_KB3125217~31bf3856ad364e35~amd6410.0 .1.1 is the package name, then copy the number ie. 3125217~31bf3856ad364e35~amd6410.0.1.1
and give in below script where kb_list_user=[]

kb_list_user=[‘3122962~31bf3856ad364e35~amd64~~10.0.1.0’]
If you want to give multiple kb_number just give like this , Based upon the requirement .

kb_list_user=['3122962~31bf3856ad364e35~amd6410.0.1.0’,'253658 ~31bf3856ad364e35~amd6410.0.1.0]

kb_list_user=[]
import os
import re
def ecmd(CMD, r=False):
    import ctypes
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    from subprocess import PIPE, Popen
    with disable_file_system_redirection():
        OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = OBJ.communicate()
    ret=OBJ.returncode
    if r:
        return ret
    else:            
        if ret==0:
            return out,ret
        else:
            return err,ret
cmd, ret1=ecmd('dism /online /get-packages /format:table')
#Display the kb_list to user.
print '
'
if ret1 == 740:
    print 'No KB_UPDATES for security/update, found in your system'
else:
    kb_list=[]
    for i in cmd.split():
        if i.startswith('Package_for_KB'):
            kb_list.append(i.split(' '))

    print 'Total number of KBUPDATES present in your system: %d' %len(kb_list)
    print '
'
    print 'List of kb which are present in your system.'
    print '
'
    for j in range(0,len(kb_list)):
        print ''.join(kb_list[j])
        ''.join(kb_list[j]).strip('Package_for_KB')

    if len(kb_list) == 0:
        print 'No KB_UPDATES for security/update found in your system'  
    elif not kb_list_user and len(kb_list)!=0:
        print '
'
        print 'Please provide kb_update number in the kb_list_user list: '
    else:
        print '
'
        print 'Unistalling KB updates......'
        for m in range(0,(len(kb_list_user))):
            fin=ecmd('DISM.exe /Online /Remove-Package /PackageName:Package_for_KB%s /quiet /norestart'%''.join(kb_list_user[m]).strip('Package_for_KB') ,False)
            if fin !=0:
                if m==0:
                    print 'Some updates are cannot be uninstalled.. please give specific kb number.........'
                print 'Error failed to uninstall Package_for_KB'+ ''.join(kb_list_user[m]).strip('Package_for_KB')
            else:
                print 'Uninstall KBupdate successfully,Package_for_KB'+ ''.join(kb_list_user[m]).strip('Package_for_KB')

            cmd1, ret2=ecmd('dism /online /get-packages /format:table')
            if ret2 == 740:
                print 'No KB_UPDATES for security/update, found in your system'
            else:
                kb_list_fin=[ ]
                for i in cmd1.split():
                    if i.startswith('Package_for_KB'):
                        kb_list_fin.append(i.split(' '))

                if len(kb_list_fin) !=len(kb_list):
                    print 'After removal of kb_updates.....'      
                    print 'Total number of KBUPDATES present in your system: %d' %len(kb_list_fin)
                    print '
'
                    print 'List of kb which are present in your system.'
                    for j in range(0,len(kb_list_fin)):
                        print ''.join(kb_list_fin[j])
                        
            

        

SAMPLE OUTPUT:

20170704-specific-kb_update_uninstall.json (4.65 KB)