在脚本开始时,我检查了一些参数,尤其是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/