#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
#The network share file path
Folderpath=itsm.getParameter('Folderpath')
#provide filename with extension
filename=itsm.getParameter('filename')
# Provide the user name for the shared system
share_user=itsm.getParameter('share_user')
# Provide the password for the shared system
share_pass=itsm.getParameter('share_pass')

Filepath=r""+Folderpath+r'\\'+filename
import os
import shutil
import ctypes
import subprocess
import zipfile
import ctypes
import sys
import platform
import _winreg


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)

workdir=os.environ['PROGRAMDATA']+r'\temp'
def login(cmd,Filepath):    
    
    if not os.path.exists(workdir):
        os.mkdir(workdir)
    with disable_file_system_redirection():        
        print 'Login to network share'
        print os.popen(cmd).read()
        print 'Copying files to local machine....'
        shutil.copy(Filepath,workdir)
        print "copied successfully"
        path=workdir+"\\"+filename
        return path

cmd= 'NET USE "'+Folderpath+'" /USER:'+share_user+'  "'+share_pass+'"'
zip_path=login(cmd,Filepath)
print zip_path
file_zip=os.environ['PROGRAMDATA']
file_zip1=file_zip

def filezip(zip_path,file_zip):
    with disable_file_system_redirection():
        with zipfile.ZipFile(zip_path,"r") as zip_ref:
            zip_ref.extractall(file_zip1)
            print 'file unzipped to ' +file_zip1

filezip(zip_path,file_zip1)

fil="""
<Configuration Product="ProPlus">
 <Display Level="none" CompletionNotice="no" SuppressModal="yes" AcceptEula="yes" />
</Configuration>
   """

temp_path=os.environ['PROGRAMDATA']+r'\temp'
configuration_file="config-group2-SACT.xml"
config_path=temp_path+'\\'+configuration_file
with open(config_path,"w") as f:
    f.write(fil)


import ctypes
from subprocess import PIPE, Popen
def ecmd(command):
    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():
        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

configuration_command = file_zip1 + r'\office2016\Setup.exe' + ' /config ' + config_path
print ecmd(configuration_command)
