【问题标题】:How to pass arguments to MSI file with PowerShell如何使用 PowerShell 将参数传递给 MSI 文件
【发布时间】:2019-12-05 23:34:03
【问题描述】:

我了解到您可以将参数传递给 .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 和 secondary。请检查这两个基本答案中包含的不同链接,也有指向您直接询问的链接。直接链接到 github.com:github.com/heaths/psmsi
  • 我会使用 Stein 提到的 Heath 的模块。我还将重命名您的 MSI 并删除空格以避免又一个调试问题。一旦你得到它的工作,你总是可以把空间放回去。

标签: powershell arguments windows-installer


【解决方案1】:

我还建议使用Powershell MSI Module
关于 Start-Process:
-Argumentlist 期望字符串作为一种类型。我不认为你可以只传递一个数组。
您还需要用转义的双引号将需要空格的参数括起来。转义字符是 powershell 是重音符号(`)。
另一个问题是变量 $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"

【讨论】:

    【解决方案2】:

    这里的语法略有不同:

    $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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多