我想安裝一組應用程序:.NET 4,管理單元,ASP.NET MVC 3等。如何獲取應用程序以安裝並返回確定安裝是否成功的值?如何運行Windows安裝程序並在PowerShell中獲取成功/失敗值?
回答
取決於。 MSI可以使用WMI進行安裝。對於exes和其他方法,您可以使用Start-Process並檢查Process ExitCode。
微星也可以使用msiexec.exe的,MSU的可使用wusa.exe安裝安裝,都有一個/quiet
開關,/norestart
和/forcerestart
開關和/log
選項用於記錄(指定文件名)。
你可以閱讀更多有關的選項,如果你給他們打電話與/?
注:WUSA默默地失敗時,他們失敗了,所以你必須檢查日誌文件或事件日誌,以確定是否成功。
我已經實現了你正在尋找我目前的項目。我們需要在多個環境和數據中心內自動部署和灌輸n個應用程序。爲了簡單起見,這些腳本從原始版本稍微修改,因爲我的完整代碼達到1000行,但核心功能完好無損。我希望這能做到你所要求的。
此PS函數從註冊表中提取所有應用程序(添加/刪除程序讀取的內容),然後搜索提供的應用程序名稱和顯示版本。在我的代碼(PSM1)中,我在安裝之前運行此函數,以確定它是否是它們,然後是驗證它是否已安裝。所有這些都可以封裝在一個主控功能中以管理流量控制。
function Confirm-AppInstall{
param($AppName,$AppVersion)
$Apps = Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*|?{$_.DisplayName -ne $Null}|?{$_.DisplayName -ne ""}
$Apps += Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*|?{$_.DisplayName -ne $Null}|?{$_.DisplayName -ne ""}
$Installed = $Apps|?{$_.DisplayName -eq ""}|?{$_.DisplayVersion -eq ""}|select -First 1
if($Installed -ne $null){return $true}else{return $false}
}
該PS函數將加載一個txt文件,該文件具有預填充的安裝命令(每行一個命令)。並且個別地運行每條線,等待安裝完成後再繼續下一步。
function Install-Application{
param($InstallList = "C:\Install_Apps_CMDS.txt")
$list = gc -Path $InstallList
foreach ($Command in $list){
Write-Output ("[{0}]{1}" -f (Get-Date -Format G),$call)
#Make install process wait for exit before continuing.
$p = [diagnostics.process]::Start("powershell.exe","-NoProfile -NoLogo -Command $Command")
$p.WaitForExit()
Start-Sleep -Seconds 2
#Searches for the installer exe or msi that was directly opened by powershell and gets the process id.
$ProcessID = (gwmi -Query ("select ProcessId from Win32_Process WHERE ParentProcessID = {0} AND Name = '{1}'" -f $p.Id,$ParentProcessFile)|select ProcessId).ProcessId
#waits for the exe or msi to finish installing
while ((Get-Process -Id $ProcessID -ea 0) -ne $null){
Start-Sleep -Seconds 2
$ElapsedTime = [int](New-TimeSpan -Start $P.StartTime -End (Get-Date)|select TotalSeconds).TotalSeconds
#install times out after 1000 seconds so it dosent just sit their forever this can be changed
if(2000 -lt $ElapsedTime){
Write-Output ('[{0}] The application "{1}" timed out during instilation and was forcfully exited after {2} seconds.' -f (Get-Date -Format G),$App.Name,(([int]$App.InstallTimeOut) * 60))
break
}
}
#clean up any old or hung install proccess that should not be running at this point.
Stop-Process -Name $ParentProcessName -ea 0 -Force
Stop-Process -Name msiexec -ea 0 -Force
}
}
TXT文件的格式應該是這樣的......您需要研究如何安裝每個應用程序。一個很好的資源是appdeploy.com
C:\Install.exe /q
C:\install.msi /qn TRANSFORMS='C:\transform.mst'
C:\install2.msi /qn /norestart
C:\install3.exe /quiet
讓我知道,如果有,我不得不改變我現有的代碼去除專有值,使這一點更簡單的任何錯誤。我從自定義XML答案表中提取我的值。但是這個代碼應該像我提供的那樣工作。
如果您想讓我討論更多關於我的實現,請告訴我,我可以做出更詳細的解釋,並添加更多我已經實現的支持功能。
這些答案似乎都過於複雜或不夠完整。在PowerShell控制檯中運行安裝程序有幾個問題。 MSI運行在Windows subsystem中,因此您不能僅調用它們(Invoke-Expression
或&
)。有些人聲稱通過管道輸入Out-Null
或Out-Host
來使這些命令正常工作,但我沒有注意到該工作。
適用於我的方法是Start-Process
,無提示安裝參數爲msiexec
。
$list =
@(
"/I `"$msi`"", # Install this MSI
"/QN", # Quietly, without a UI
"/L*V `"$ENV:TEMP\$name.log`"" # Verbose output to this log
)
Start-Process -FilePath "msiexec" -ArgumentList $list -Wait
您可以從Start-Process
命令得到exit code,檢查其是否合格/不合格值。 (這裏是exit code reference)
$p = Start-Process -FilePath "msiexec" -ArgumentList $list -Wait -PassThru
if($p.ExitCode -ne 0)
{
throw "Installation process returned error code: $($p.ExitCode)"
}
- 1. 如果Windows安裝程序成功或失敗,請查找
- 2. 運行CDS安裝程序時安裝CDK失敗
- 3. 模擬運行已安裝的應用程序失敗完成安裝後
- 4. 安裝Glassfish服務器與安裝程序失敗,在Windows 7
- 5. PyTables在Windows安裝失敗
- 6. PowerShell獲取已安裝的程序
- 7. VS2010安裝程序安裝失敗
- 8. 安裝成功後bcrypt失敗
- 9. 安裝成功,但Ansible報告失敗
- 10. RPM安裝成功,但rpm -q失敗
- 11. ./adb uinstall失敗後安裝成功
- 12. 運行「軟件包安裝」失敗並要求我運行「軟件包安裝」
- 13. 如何在運行偵聽器時獲取登錄成功和失敗次數
- 14. 如何在安裝過程中提取並運行文件
- 15. 從PowerShell調用exe並獲得成功或失敗的反饋
- 16. Windows 7:安裝SASS失敗
- 17. jekyll安裝失敗 - Windows
- 18. Mongodb Windows安裝失敗
- 19. Windows安裝PhantomJS失敗
- 20. Windows安裝vagrant up失敗
- 21. ElasticSearch Windows安裝失敗
- 22. 安裝APTANA失敗Windows 10
- 23. Windows 7 SDK安裝失敗
- 24. VSCode安裝失敗 - 未能提取安裝程序
- 25. 從Inno Setup安裝程序運行批處理文件失敗
- 26. 運行應用程序時安裝失敗,出現故障[INSTALL_FAILED_DEXOPT]
- 27. 如何使用powershell來運行安裝程序?
- 28. Wix安裝程序在安裝失敗後執行程序出現錯誤2896
- 29. Wix - 安裝並運行powershell腳本
- 30. CruiseControl:從命令行運行=成功,因爲Windows服務=失敗