Reputation: 402
I am running an msi installer in silent mode and caching logs in the specific file. The following is the command i need to execute.
C:\Program Files\ My Installer\Setup.exe /s /v "/qn /lv %TEMP%\log_silent.log"
I used:
subprocess.Popen(['C:\Program Files\ My Installer\Setup.exe', '/s /v "/qn /lv %TEMP%\log_silent.log"'],stdout=subprocess.PIPE).communicate()[0]
to execute the command however it does not recognise the operation and gives error regarding wrong option selected. I have cross-verified and found that the command only works this way.
Upvotes: 4
Views: 6223
Reputation: 433
The problem is very subtle.
You're executing the program directly. It gets:
argv[0] = "C:\Program Files\ My Installer\Setup.exe"
argv[1] = /s /v "/qn /lv %TEMP%\log_silent.log"
Whereas it should be:
argv[1] = "/s"
argv[2] = "/v"
argv[3] = "/qn"
argv[4] = "/lv %TEMP%\log_silent.log"
In other words, it should receive 5 arguments, not 2 arguments.
Also, %TEMP%
is directly unknown to the program!
There are 2 ways to fix this problem:
Calling the shell.
p = subprocess.Popen('C:\Program Files\ My Installer\Setup.exe /s /v "/qn /lv %TEMP%\log_silent.log"', shell=True)
output = p.communicate()[0]
Directly call program (more safer)
s = ['C:\Program Files\ My Installer\Setup.exe', '/s /v "/qn /lv %TEMP%\log_silent.log"']
safes = [os.path.expandvars(p) for p in argument_string]
p = subprocess.Popen(safes[0], safes[1:])
output = p.communicate()[0]
Upvotes: 9
Reputation: 42183
You said:
subprocess.Popen(['C:\Program Files\ My Installer\Setup.exe', '/s /v "/qn /lv %TEMP%\log_silent.log"'],stdout=subprocess.PIPE).communicate()[0]
Is the directory name really " My Installer" (with a leading space)?
Also, as a general rule, you should use forward slashes in path specifications. Python should handle them seamlessly (even on Windows) and you avoid any problems with python interpreting backslashes as escape characters.
(for example:
>>> s = 'c:\program files\norton antivirus'
>>> print s
c:\program files
orton antivirus
)
Upvotes: 0
Reputation: 181740
Try putting each argument in its own string (reformatted for readability):
cmd = ['C:\Program Files\ My Installer\Setup.exe',
'/s',
'/v',
'"/qn',
'/lv',
'%TEMP%\log_silent.log"']
subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
I have to say though, those double quotes do not look in the right places to me.
Upvotes: 0
Reputation: 2484
The problem is that you effectively supply Setup.exe with only one argument. Don't think in terms of the shell, the string you hand over as an argument does not get splitted on spaces anymore, that's your duty!
So, if you are absolutely sure that "/qn /lv %TEMP%\log_silent.log" should be one argument, then use this:
subprocess.Popen(['C:\Program Files\ My Installer\Setup.exe', '/s', '/v', '/qn /lv %TEMP%\log_silent.log'],stdout=subprocess.PIPE).communicate()[0]
Otherwise (I guess this one will be correct), use this:
subprocess.Popen(['C:\Program Files\ My Installer\Setup.exe', '/s', '/v', '/qn', '/lv', '%TEMP%\log_silent.log'],stdout=subprocess.PIPE).communicate()[0]
Upvotes: 2