How to pass arguments to MSI file with PowerShell

只愿长相守 提交于 2020-06-17 04:21:59

问题


I've read that you can pass arguments to a .msi file, but I have no idea how to do it correctly. I've tried the following, where $ArgumentList is an array.

$ArgumentList = @("/i .\NSClient v67.msi", "/norestart", "/quiet", "/l*v '$directory'", "token=$token", "host=$_host", "mode=$mode")
Start-Process "msiexec" -ArgumentList $ArgumentList -Wait -NoNewWindow

This is part of my script, where I'm trying to install NetSkope on my machine by executing a command. In theory, the command should look like msiexec /i "NSClient v67.msi" token=loremipsum host=bryan.goskope.com mode=peruserconfig /norestart /quiet /l*v "C:\Temp\NetskopeInstallation.log.

#Find file path
$rawPath = Invoke-Expression -Command 'C:\Windows\System32\WHERE /r C:\Users\ /f NSClient*.msi'

#Extract the directory
$filePath = Invoke-Expression -Command "cmd.exe --% /c FOR /f ""tokens=1"" %A IN ($rawPath) DO (ECHO 
'%~dpA')"

#Cast $filePath to work with string methods
$filePath = Out-String -InputObject $filePath
$filePath = $filePath.split("'")[1]

Invoke-Expression -Command "cmd.exe --% /c cd $filePath"


$ArgumentList = @("/i .\NSClient v67.msi", "/norestart", "/quiet", "/l*v '$directory'", 
"token=$token", "host=$_host", "mode=$mode")
Start-Process "msiexec" -ArgumentList $ArgumentList -Wait -NoNewWindow

回答1:


I would also recommend using the Powershell MSI Module
Concerning Start-Process:
-Argumentlist expects string as a type. I don't think you can just pass an array.
You also need to surround the parameters that require a space with escaped double quotes. The escape character is powershell is the grave-accent(`).
Another problem is that the variable $directory will never be expanded, because it is surrounded with single quotes. You need to remove those.
The following should work for your example, but I personally would just remove the space in the file name, since you don't need to do weird stuff with escaping.

Without escaping:

$ArgumentList = "/i .\NSClientv67.msi /norestart /quiet /l*v $directory token=$token host=$_host mode=$mode"

With escaping:

$ArgumentList = "/i `".\NSClient v67.msi`" /norestart /quiet /l*v $directory token=$token host=$_host mode=$mode"



回答2:


Here's a slightly different syntax:

$MSIArguments = @(
    "/x"
    "`"C:\path with spaces\test.msi`""
    "/qb"
    "/norestart"
    "/l*v"
    "`"C:\path with spaces\test.log`""
)

Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow 


来源:https://stackoverflow.com/questions/59204758/how-to-pass-arguments-to-msi-file-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!