我已经读过您可以将参数传递给.msi文件,但我不知道如何正确执行。我尝试了以下操作,其中$ArgumentList
是一个数组。
$ArgumentList = @("/i .\NSClient v67.msi", "/norestart", "/quiet", "/l*v '$directory'", "token=$token", "host=$_host", "mode=$mode")
Start-Process "msiexec" -ArgumentList $ArgumentList -Wait -NoNewWindow
这是我脚本的一部分,我试图通过执行命令在计算机上安装NetSkope。理论上,命令应类似于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
我也建议您使用Powershell MSI Module关于Start-Process:-Argumentlist
期望将字符串作为一种类型。我认为您不能只传递数组。您还需要用转义双引号将需要空格的参数括起来。转义字符是powershell是graving-accent(`)。另一个问题是变量$ directory永远不会被扩展,因为它被单引号引起来。您需要删除那些。下面的示例适用于您的示例,但是我个人会删除文件名中的空格,因为您无需进行转义就可以处理奇怪的事情。
没有转义:
$ArgumentList = "/i .\NSClientv67.msi /norestart /quiet /l*v $directory token=$token host=$_host mode=$mode"
[转义:
$ArgumentList = "/i `".\NSClient v67.msi`" /norestart /quiet /l*v $directory token=$token host=$_host mode=$mode"
这里的语法略有不同:
$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