Url = r"https://script-downloads.itarian.com/HardwareReadiness.ps1"

import os
import ctypes
import urllib2
import ssl
import re

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()


Down_path=os.environ['TEMP']
fileName = Url.split('/')[-1]
DownTo = os.path.join(Down_path, fileName)

ps_content = """
cd %s
Set-ExecutionPolicy -ExecutionPolicy Bypass -Force
.\%s
"""%(Down_path,fileName)

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
            
def downloadFile(DownTo, fromURL):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
    context = ssl._create_unverified_context()
    request = urllib2.Request(fromURL, headers=headers)
    req = urllib2.urlopen(request,context=context)
    try:
        with open(DownTo, 'wb') as f:
            while True:
                chunk = req.read(100*1000*1000)
                if chunk:
                    f.write(chunk)
                else:
                    break
        if os.path.isfile(DownTo):
            return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1024)
		
    except:
        return 'Please Check URL or Download Path!'

def check():
    print(downloadFile(DownTo, Url))
    ps_name='powershell_file.ps1'
    ps_path=os.path.join(os.environ['TEMP'], ps_name)
    with open(ps_path, 'wb') as wr:
        wr.write(ps_content)  
        
    with ExecutionPolicy():
        ret,output,error = ecmd('powershell "%s"'%ps_path)
    if ret == 0:
        if output:
            compatability = re.findall('"returnResult":"(.*)"}',output)[0]
            if compatability=="NOT CAPABLE":
                print("THIS SYSTEM DOES NOT MEET THE REQUIREMENT FOR WINDOWS 11")
            elif compatability=="CAPABLE":
                print("THIS SYSTEM IS COMPATIBLE FOR WINDOWS 11")
            elif compatability=="UNDETERMINED":
                print("CAN'T DETERMINE WINDOWS 11 COMPATABILITY FOR THIS SYSTEM")
            elif compatability=="FAILED TO RUN":
                print("POWERSHELL SCRIPT FAILED TO RUN")
    else:
        print(error)
    os.remove(DownTo)
    os.remove(ps_path)

check() 