import os
from subprocess import PIPE, Popen
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)

def ecmd(command):   
    from subprocess import Popen, PIPE
    import ctypes
    
    with disable_file_system_redirection():
        obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    ret=obj.returncode
    return ret,out,err

t_ret,t_out,t_err = ecmd(r"takeown /f C:\Windows\ServiceProfiles\LocalService\AppData\Local\Microsoft\NGC /r /d y")
if t_ret==0:
    if t_out:
        print(t_out)
    a_ret,a_out,a_err = ecmd(r"icacls C:\Windows\ServiceProfiles\LocalService\AppData\Local\Microsoft\NGC /grant administrators:F /t")
    if a_ret==0:
        if a_out:
            print(a_out)
        d_ret,d_out,d_err = ecmd(r"RD /S /Q C:\Windows\ServiceProfiles\LocalService\AppData\Local\Microsoft\NGC")
        if d_ret==0:
            if d_out:
                print(d_out)
            print("successfully remove NGC folder which contains all the Windows Hello PIN for all the users.")
            print("please restart the system now")
        else:
            print("couldn't remove NGC folder")
    else:
        if a_err:
            print(a_err)
        print("couldn't grant administrative rights to NGC folder")
else:
    if t_err:
        print(t_err)
    print("couldn't take ownership of the NGC folder")