我已将放置在远程服务器上的所有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