2011-01-20 31 views

回答

3

取決於。 MSI可以使用WMI進行安裝。對於exes和其他方法,您可以使用Start-Process並檢查Process ExitCode。

3

微星也可以使用msiexec.exe的,MSU的可使用wusa.exe安裝安裝,都有一個/quiet開關,/norestart/forcerestart開關和/log選項用於記錄(指定文件名)。

你可以閱讀更多有關的選項,如果你給他們打電話與/?

注:WUSA默默地失敗時,他們失敗了,所以你必須檢查日誌文件或事件日誌,以確定是否成功。

0

我已經實現了你正在尋找我目前的項目。我們需要在多個環境和數據中心內自動部署和灌輸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答案表中提取我的值。但是這個代碼應該像我提供的那樣工作。

如果您想讓我討論更多關於我的實現,請告訴我,我可以做出更詳細的解釋,並添加更多我已經實現的支持功能。

16

這些答案似乎都過於複雜或不夠完整。在PowerShell控制檯中運行安裝程序有幾個問題。 MSI運行在Windows subsystem中,因此您不能僅調用它們(Invoke-Expression&)。有些人聲稱通過管道輸入Out-NullOut-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)" 
}