Powershell Script

Is it possible to write a Powershell script that would download the communication agent from the link and install it? So that I can then deploy the agent using the procedure in the console? Could someone help in the preparation of such a script?

You can use this Update Comodo Client Communication to latest version (itarian.com)

1 Like

I need help writing a powershell script to download and install the communication agent procedure I already have in the console

You can import this procedure and run it. Any specific reason you want powershell script and not the default python? This script helps you meet your objective.
Powershell script is not directly supported as of yet but maybe in the future. So if you really want PowerShell element in the procedure script then the following can be used as well. It will download the file in C:\Temp folder and then install it. It wont reboot it, so you can plan your reboot at desired time. The powershell file that downloads the file will be created on the target workstation where %TEMP% variable is pointing to and once it has done its job, it will delete that file. You can make changes to the script based on which folder you want to use for downloading the file.

ps_content=r'''
# Define URL
$version_url='http://cmdm.comodo.com/download/win/communication_client/latest/itsm_agent.msi'
# Define the target folder where file will be downloaded
if(!(Test-Path -Path 'C:\Temp')) {New-Item -ItemType directory -Path 'C:\Temp'  -Force}
# Download the file
Invoke-WebRequest -Uri $version_url -OutFile "C:\Temp\itsm_agent.msi"
# Install the agent if file is downloaded successfully.
if(Test-Path "C:\Temp\itsm_agent.msi)" -PathType Leaf) {
	Write-Host "The MSI installer file was downloaded succesfully"
	Start-Process -FilePath "c:\windows\system32\msiexec.exe" -ArgumentList "/i C:\Temp\itsm_agent.msi /q /norestart /l*v C:\Temp\XcitiumAgentInstallation.log" -Wait
}
else {
	Write-Host "The MSI installer file was not found. Please retry again"
}

'''

import os

def ecmd(command):
    import ctypes
    from subprocess import PIPE, Popen
    
    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

file_name='remexeccmd_file.ps1'
file_path=os.path.join(os.environ['TEMP'], file_name)
with open(file_path, 'wb') as wr:
    wr.write(ps_content)

ecmd('powershell "Set-ExecutionPolicy RemoteSigned"')
print ecmd('powershell "%s"'%file_path)
os.remove(file_path)
1 Like