0

My objective is to create a script (or a Jenkins job) which remotely uninstalls, installs(.msi) on Windows VMs from a Linux machine.

I'm new to windows stuff so here I am,

I'm currently ssh'ing to Windows VMs using paramiko in python and executing commands in Win command prompt (cmd).

For uninstall - msiexec /q /x "App.msi" /qn. It doesn't work sometimes.

Opening a program(as administator) - I'm scheduling a task using Windows Task scheduler which doesn't open the program as admistrator but does show UI. (Will run at the correct time if VMs timezone changes)

Alternative was "C:\PathtoProgram.exe" but it the program only shows up in Task manager without any UI which is necessary.

ip = device['hostname']
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,DEFAULT_WINDOWS_LOGIN,DEFAULT_WINDOWS_PWD)

#Uninstall Current Build
cmd = 'msiexec /x "setup.msi" /qn'
helptext = "Uninstalled %s on %s" % (current_version, ip)
uninstall_current_build(ssh, cmd)
time.sleep(3)

#Downloads new build from URL
download_url = json.loads(build_version).get("downloadUrl")
cmd = 'curl -o setup.msi %s' % (download_url)
helptext = "Downloaded %s on %s" % (current_version, ip)
execute_ssh_command(ssh, cmd, helptext)

#Install new Build
cmd = 'msiexec /i "setup.msi" /qn'
helptext = "Installed %s on %s" % (current_version, ip)
execute_ssh_command(ssh, cmd, helptext)

#Open Program
vm_time = (datetime.utcnow() + timedelta(minutes=1)).strftime("%H:%M")
cmd = 'SCHTASKS /CREATE /SC ONCE /TN "OpenTask" /F /TR "\Path\To\program.exe"" /ST %s' % (vm_time)
    stdin, stdout, stderr = ssh.exec_command(cmd)


def execute_ssh_command(client, command, helptext):
    stdin, stdout, stderr = client.exec_command(command, get_pty=True)
    for line in iter(stdout.readline, ""):
        print(line)
    print(helptext)

Is there a better or cleaner way to do this?

  • [Maybe try to have a look in section 3 here](https://stackoverflow.com/questions/450027/uninstalling-an-msi-file-from-the-command-line-without-using-msiexec/1055933#1055933). Feeding the MSI file to msiexec.exe will probably not work if the MSI is not the same one that was used to install the software - the product code inside the file could be different. – Stein Åsmul May 20 '20 at 23:55
  • See [Applications executed using SSH don't show on remote machine](https://superuser.com/q/1297211/213663). – Martin Prikryl May 21 '20 at 05:59

0 Answers0