我已將放置在遠程服務器上的所有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
看起來比以前好多了,謝謝你!我的大腦似乎並不適用於腳本。經過多次編輯,這就是我所擁有的。 http://pastebin.com/X0vN832f – user4317867 2014-12-03 01:18:07
這對你有用嗎? – xXhRQ8sD2L7Z 2014-12-03 21:03:06
我改變了一些東西。上週我在這裏發佈了這個問題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