等待使用PowerShell安装MSI

时间:2014-09-08 15:41:51

标签: powershell install 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
    #----------------------------------------------

有人有想法解决这个问题吗?

非常感谢脚本专家!

此致 于连

1 个答案:

答案 0 :(得分:1)

Start-Process有一个-wait开关,可用于等待进程在继续之前终止。