2014-12-02 48 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