import os
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)
           
class ExecutionPolicy:
    def __enter__(self):
        with disable_file_system_redirection():
            #getting current executionpolicy
            self.old_policy = os.popen('powershell "Get-ExecutionPolicy"').read().strip()
            #setting execution policy to RemoteSigned
            os.popen('powershell "Set-ExecutionPolicy RemoteSigned"').read()
    def __exit__(self, type, value, traceback):
        with disable_file_system_redirection():
            #setting execution policy back to previous policy
            os.popen('powershell "Set-ExecutionPolicy %s"'%(self.old_policy)).read()

with ExecutionPolicy():
    with disable_file_system_redirection():
        TT_CPU_CONSUMPTION = os.popen(r'powershell "Get-Process | Sort-Object -Property CPU -Descending | Select-Object -Property ProcessName,Id,CPU -First 10"').read()
        TT_RAM_CONSUMPTION = os.popen(r'powershell "Get-Process | Sort-Object -Property WorkingSet -Descending | Select-Object -Property ProcessName,Id,WorkingSet -First 10"').read()

print("TOP 10 CPU CONSUMIMG PROCESSES ARE:")
print(TT_CPU_CONSUMPTION)
print("TOP 10 RAM CONSUMIMG PROCESSES ARE:")
print(TT_RAM_CONSUMPTION)

