#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name

product_key = itsm.getParameter('ProductKey') ##Provide the product key like xxxxx-xxxxx-xxxxx-xxxxx-xxxxx

import os
URL=r'https://script-downloads.itarian.com/windows/win10_64.zip'
src_path=os.environ['PROGRAMDATA']

import zipfile
import ctypes
import sys
import platform
import _winreg
import ssl
import shutil
import urllib 
import subprocess
ssl._create_default_https_context = ssl._create_unverified_context


obj=subprocess.Popen("wmic os get osarchitecture",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
result,error=obj.communicate()
if error:
    print(error)
else:
    operating_system = result.split()

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 Download(src_path, URL):
    import urllib2
    import os
    print "Download started"
    fileName = 'Windows10-1809.zip'
    fp = os.path.join(src_path, fileName)
    request = urllib2.Request(URL, headers={'User-Agent' : "Magic Browser"})
    parsed = urllib2.urlopen(request)
    if os.path.exists(src_path):
        print "Path already exists"
    if not os.path.exists(src_path):
        os.makedirs(src_path)
        print "Path created"
    with open(fp, 'wb') as f:
        while True:
            chunk=parsed.read(100*1000*1000)
            if chunk:
                f.write(chunk)
            else:
                break
    print "The file downloaded successfully in specified path"
    return fp

zip_path=Download(src_path, URL)
print zip_path
if '64-bit' in operating_system:
    url = r'https://script-downloads.itarian.com/7zip-msi/7z2107-x64.msi'
    destination = "C:\\Users\\7zip.msi"
    gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    download = urllib.urlretrieve(url,destination,context = gcontext)
    cmd = subprocess.Popen('msiexec /i C:\\Users\\7zip.msi /quiet /qn ',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    result_1,error_1=cmd.communicate()
    if error_1:
        print(error_1)
    else:
        os.chdir("C:\\Program Files\\7-Zip")
        cmd_2 = subprocess.Popen('7z x C:\\ProgramData\\Windows10-1809.zip -y -oC:\\Windows\\Temp',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        result_2,error_2=cmd_2.communicate()
        if error_2:
            print(error_2)
        else:
            print ("File unzipped")
            
import ctypes
import os
from subprocess import PIPE, Popen
def ecmd(path):
    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)

    with disable_file_system_redirection():
        command = '"'+path+'"'+' /auto DataOnly /showoobe none /DiagnosticPrompt enable /dynamicupdate disable /PKey '+product_key
        obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    ret=obj.returncode
    if ret==0:
        if out:
            return out.strip()
        else:
            return ret
    else:
        if err:
            return err.strip()
        else:
            return ret
setup_file = "C:\\Windows\\Temp\\New folder\\setup.exe"
if os.path.isfile(setup_file):
    print ecmd(setup_file)
else:
    print "Setup FIle not Found"







