powershell - 等待使用Powershell安装MSI

标签 powershell installation invoke

在脚本开始时,我检查了一些参数,尤其是powershell的版本。
如果Powershell的版本是lt 4,我想安装新版本(Windows6.1-KB2819745-x64-MultiPkg.msu)。
该脚本将通过Powershell Studio在.exe中转换。
我的问题是我可以静默启动Powershell的安装,但是我想等待安装结束和返回代码来检查安装是否正确完成,并且在继续执行脚本之前我需要完成此安装。

我尝试这样做,但无法正常工作(脚本不等待安装结束):

Invoke-Expression "C:\PowershellSources\PS4_Windows6.1-KB2819745-x64-MultiPkg.msu /quiet / norestart /wait" 
    #verification of the powershell version
    $PowershellVersion = $PSVersionTable.PSVersion.Major

    If ($PowershellVersion -lt "4")
    {
        $PowershellInstalled = $False
    }
    Else
    {
        $PowershellInstalled = $true
    }

    If (($LastexitCode -eq 0) -and ($PowershellInstalled -eq $true))
    {
        ([System.Windows.Forms.MessageBox]::Show("Installation Done Properly
Can Continue", "Success", [System.Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information) -eq "OK")
    }

    Else
    {
        ([System.Windows.Forms.MessageBox]::Show("Error during installation of Powershell`
please contact system_admin@xxx.com", "ERROR", [System.Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Error) -eq "OK")
    Exit    
    }

我也尝试这样做:
$StartInstall = (& wusa "C:\PowershellSources\PS4_Windows6.1-KB2819745-x64-MultiPkg.msu" /quiet /norestart)

        ---------------------------------------
    #Wait for end of Powershell Installation
    #---------------------------------------

    #Define the process to wait in $processtargettowait
    $ProcesstargetToWait = "wusa"
    #Get the complet name of the process
    $ProcessToWait = Get-Process | Where-Object { $_.ProcessName -like $ProcesstargetToWait }
    #Tant que le process est lancé (ie : while the process is running = while ($process) = $true
    While ($ProcessToWait)
    {
        #on recheck le process
        $ProcessToWait = Get-Process | Where-Object { $_.ProcessName -like $ProcesstargetToWait }
        #message indiquant que l'installation est toujours en cours
        ([System.Windows.Forms.MessageBox]::Show("Please Relax and wait the end of installation of POWERSHELL`nI'm Working for you.", "Please Wait", [System.Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information) -eq "OK")
        #on attend Xsec avant le prochain check
        start-sleep -Seconds 2
    }#end While loop

    #----------------------------------------------
    #END of Wait for end of Powershell Installation
    #----------------------------------------------

有人有解决的办法吗?

非常感谢脚本专家!

问候
朱利安

最佳答案

Start-Process具有-wait开关,您可以使用它来等待进程终止后再继续。

关于powershell - 等待使用Powershell安装MSI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25728016/

相关文章:

c# - 有没有办法通过 C# 中的 Type.InvokeMember 或消息调用窗体或类的私有(private)函数?

powershell - 为什么枚举后比较运算符不起作用?

windows - 如何修复 Windows 8.1 中的 visual studio 错误 "cannot find one or more components .Please re-install the application"?

vb6 - 无需安装 VB6 即可运行 VB6 应用程序

php - 如何获取magento表名

c# - Unity c# 触发另一个 GameObject 的方法。更好的方法?

c# - 如何使用 ref struct 参数和未知返回类型调用内部方法

azure - 尝试清理我制作的 PS 脚本

command-line - PowerShell 2.0 - 运行命令行调用与从 ISE 调用的脚本

Powershell 开放端口配置文件,全部(私有(private)公共(public)域)