【问题标题】:Powershell Call MSI with Arguments带参数的 Powershell 调用 MSI
【发布时间】:2017-07-20 18:51:47
【问题描述】:

我正在编写一个用于安装 Autodesk 产品的 powershell 脚本,但遇到了一些问题。

我尝试了很多不同的方法,但一直出错。

使用双引号

(Start-Process "msiexec.exe" -ArgumentList ""/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!"" -NoNewWindow -Wait -PassThru).ExitCode

错误无法验证参数“ArgumentList”的参数参数为空或为空。

使用变量定义InstallDir

$RevitInstallDir = "C:\Program Files\Autodesk\"
    (Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR=$RevitInstallDir ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

这样做我得到 msiexec /option 必需参数错误。

也试过这个,单引号和路径上的引号

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

我收到一个位置参数找不到接受参数 C:\Program

在 InstallDir 上使用单引号

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR='C:\Program Files\Autodesk\' ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

这样做我得到 msiexec /option 必需参数错误。

在外面使用单引号

(Start-Process "msiexec.exe" -ArgumentList '/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!' -NoNewWindow -Wait -PassThru).ExitCode

如果我这样做,它会阻止 $dirFiles 变量工作。

我使用 Start-Process 的原因是因为我有很多安装程序一个接一个,我希望一个安装程序等到一个安装程序完成。任何帮助,将不胜感激!谢谢

编辑:没关系,我想通了。

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR=""C:\Program Files\Autodesk\"" ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

安装目录的双引号

从这里得到想法。 https://blogs.technet.microsoft.com/heyscriptingguy/2015/06/20/weekend-scripter-understanding-quotation-marks-in-powershell/

谢谢

【问题讨论】:

    标签: powershell


    【解决方案1】:

    除非您需要以提升的权限运行进程,否则不要打扰 Start-Process。请改用call operator 和splatting。命令的退出码存放在automatic variable$LastExitCode中。

    $params = '/i', "$dirFiles\ABDS2017\Img\x64\RVT\RVT.msi",
              'INSTALLDIR="C:\Program Files\Autodesk"', 'ADSK_SETUP_EXE=1',
              '/qb!'
    & msiexec.exe @params
    $LastExitCode
    

    不幸的是,您不能告诉msiexec.exe 等待安装完成,并且调用运算符也不会强制同步执行。如果您需要等待安装完成后再继续,您需要类似 CMD 内置的start 命令或Start-Process。不过,我仍然建议将参数定义为数组。

    $params = '/i', "$dirFiles\ABDS2017\Img\x64\RVT\RVT.msi",
              'INSTALLDIR="C:\Program Files\Autodesk"', 'ADSK_SETUP_EXE=1',
              '/qb!'
    $p = Start-Process 'msiexec.exe' -ArgumentList $params -NoNewWindow -Wait -PassThru
    $p.ExitCode
    

    【讨论】:

    • 这可能在您发布时有效,但我今天无法完成。
    • @phydeauxman 然后请发布一个新问题。参考这个问题,显示你正在运行的代码的minimal reproducible example,并尽可能详细地描述你是如何“无法完成这项工作”的。
    • 我不相信用 splatting 定义的方法等待 msi 完成安装...
    【解决方案2】:

    这对我有用,有同样的问题

    $exeScriptDir = $PSScriptRoot
    
    Function installMSI {
        $msifile = "$exeScriptDir\3D Access\OSNME.msi"
        $msiargs = @(
            "/i"
            "`"$msifile`""
            'INSTALLDIR="C:\PRGS\PTC\Creo Elements\Direct 3D Access 20.1\"'
            "MELS=LOCALHOST"
            "/qn"
        )
        $installcommand = (start-process msiexec.exe -ArgumentList $msiargs -wait -PassThru)
    }   
    
    installMSI 
    

    【讨论】:

      【解决方案3】:

      我想出了答案。 installdir 路径周围的双引号为我修复了它。

      谢谢

      (Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR=""C:\Program Files\Autodesk\"" ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode
      

      【讨论】:

      • 你不需要Start-Process。
      • 这也对我有用.. Start-Process msiexec.exe -Wait -ArgumentList "/qb /i ""MyMSI.msi"""
      【解决方案4】:

      将整个参数存储在一个变量中然后将其传递给启动进程有什么运气吗?

      示例:

      $argument = "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR='C:\Program Files\Autodesk\' ADSK_SETUP_EXE=1 /qb!"
      (start-process msiexec.exe -argumentlist $argument).ExitCode
      

      【讨论】:

        【解决方案5】:

        这是我对您提供的第一个代码 sn-p 的建议,在双引号内使用单引号...

        (Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR='C:\Program Files\Autodesk\' ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode
        

        如果这收到错误,则您缺少一个重要参数。

        【讨论】:

          【解决方案6】:

          为了了解如何“引用”启动进程的参数 这是一个启动vmtools无人值守安装的代码示例

          我想在 powershell 中运行的命令:

          setup.exe  /l /v"/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs,Unity,BootCamp,SVGA"
          

          powershell 示例:

          Start-Process -FilePath "$vmtoolspath\setup.exe" -ArgumentList "/l /v ""/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs,Unity,BootCamp,SVGA"" " -Wait
          

          【讨论】:

            猜你喜欢
            • 2020-04-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-19
            • 1970-01-01
            • 1970-01-01
            • 2019-03-24
            • 1970-01-01
            相关资源
            最近更新 更多