2014-12-02 64 views
1

我已將放置在遠程服務器上的所有SCCM缺失更新的一些(借用)PowerShell放在一起。現在我試圖讓Write-Progress工作,並沒有取得太大的成功。下面的代碼顯示了一個進度條,但當PercentComplete的參數遞增100時會拋出錯誤。PowerShell安裝SCCM更新並顯示寫入進度

我知道這可以使用ForEach循環解決,並增加一個計數器,但我甚至不知道如何嘗試,因爲我使用InstallUpdates方法。我知道有一個InstallUpdate方法,但不知道如何包裝所有東西。

# Get the number of missing updates 
[System.Management.ManagementObject[]] $CMMissingUpdates = @(GWMI -ComputerName $server -query "SELECT * FROM CCM_SoftwareUpdate WHERE ComplianceState = '0'" -namespace "ROOT\ccm\ClientSDK") #End Get update count. 
$result.UpdateCountBefore = "The number of missing updates is $($CMMissingUpdates.count)" 
#Install missing updates. 
If ($CMMissingUpdates.count) { 
#$result.UpdateCountBefore = "The number of missing updates is $($CMMissingUpdates.count)" 
$CMInstallMissingUpdates = (GWMI -ComputerName $server -Namespace "root\ccm\clientsdk" -Class "CCM_SoftwareUpdatesManager" -List).InstallUpdates($CMMissingUpdates) 
#Set the missing updates to variable for progress indicator. 
$updates = $CMMissingUpdates.Count 
$Increment = 100/$updates 
$Percent = 0 
Do { 
Start-Sleep -Seconds 15 
[array]$CMInstallPendingUpdates = @(GWMI -ComputerName $server -query "SELECT * FROM CCM_SoftwareUpdate WHERE EvaluationState = 6 or EvaluationState = 7" -namespace "ROOT\ccm\ClientSDK") 
#Not 100% sure $result.UpdateCountBefore is needed below. 
$result.UpdateCountBefore = "The number of pending updates for installation is: $($CMInstallPendingUpdates.count)" 
Write-Progress -Activity "Updates are installing..." -PercentComplete $Percent -Status "Working..." 
$Percent = $Percent + $Increment 
} 
While(($CMInstallPendingUpdates.count -ne 0) -and ((New-TimeSpan -Start $StartTime -End $(Get-Date)) -lt "00:55:00")) 
Write-Progress -Activity "Updates Installed" -Status "Done" -Completed 

回答

2

我想我已經固定了進度條的完成百分比的問題。如您所述,使用For循環。有很多事情我不能像WMI查詢和SCCM方法那樣測試,但Write-Progress應該沒問題。

您可以在如果你想在for循環條件pendingupdates試驗和測試的時間跨度,但是這是對眼睛更容易一些。

更新:我檢查了鏈接中的代碼。進度條永遠不會更新,直到內部循環完成 - 這意味着進度將從0%變爲100%。所以我刪除了外For循環,打破支票和睡眠命令和移動Write-ProgressDo循環內:

# Get the number of missing updates 
[System.Management.ManagementObject[]] $CMMissingUpdates = @(GWMI -ComputerName $server -query "SELECT * FROM CCM_SoftwareUpdate WHERE ComplianceState = '0'" -namespace "ROOT\ccm\ClientSDK") #End Get update count. 
$result.UpdateCountBefore = "The number of missing updates is $($CMMissingUpdates.count)" 

$updates = $CMMissingUpdates.count 

If ($updates) { 
    #Install the missing updates. 
    $CMInstallMissingUpdates = (GWMI -ComputerName $server -Namespace "root\ccm\clientsdk" -Class "CCM_SoftwareUpdatesManager" -List).InstallUpdates($CMMissingUpdates) 
    Do { 
     Start-Sleep -Seconds 15 
     [array]$CMInstallPendingUpdates = @(GWMI -ComputerName $server -query "SELECT * FROM CCM_SoftwareUpdate WHERE EvaluationState = 6 or EvaluationState = 7" -namespace "ROOT\ccm\ClientSDK") 
     Write-Progress -Activity "Updates are installing..." -PercentComplete (($CMInstallPendingUpdates.count/$updates)*100) 
    } While (($CMInstallPendingUpdates.count -gt 0) -and ((New-TimeSpan -Start $StartTime -End $(Get-Date)) -lt "00:55:00")) 
} Else { 
    $result.UpdateCountAfter = "There are no missing updates." 
} 
+0

看起來比以前好多了,謝謝你!我的大腦似乎並不適用於腳本。經過多次編輯,這就是我所擁有的。 http://pastebin.com/X0vN832f – user4317867 2014-12-03 01:18:07

+0

這對你有用嗎? – xXhRQ8sD2L7Z 2014-12-03 21:03:06

+0

我改變了一些東西。上週我在這裏發佈了這個問題https://social.technet.microsoft.com/Forums/en-US/016c4127-4c54-4212-954d-11bca967e6d3/writeprogress-in-powershell-script-for-installing-missing -updates?論壇= configmanagersdk – user4317867 2014-12-03 21:40:46

相關問題