我已经将一些(借用的)PowerShell放在一起,它将在远程服务器上安装所有SCCM缺失的更新。现在我试图让Write-Progress工作,并且没有取得多大成功。下面的代码显示一个进度条,但当PercentComplete的参数增加超过100时会抛出错误。
我知道这可以使用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
答案 0 :(得分:2)
我认为我已经修复了进度条的完成百分比问题。如您所述,使用For
循环。我无法像WMI查询和SCCM方法那样测试很多东西,但Write-Progress
应该没问题。
如果需要,您可以在for循环条件中包含pendingupdates测试和时间跨度测试,但这对眼睛来说更容易。
更新:我检查了您链接中的代码。在内循环完成之前,进度条永远不会更新 - 意味着进度将从0%变为100%。所以我删除了外部For
循环,中断检查和睡眠命令,并将Write-Progress
移到了Do
循环中:
# 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."
}