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?