【问题标题】:PowerShell install SCCM updates and display Write-ProgressPowerShell 安装 SCCM 更新并显示写入进度
【发布时间】:2014-12-02 23:35:55
【问题描述】:

我整理了一些(借来的)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

【问题讨论】:

    标签: powershell updates sccm


    【解决方案1】:

    我想我已经解决了进度条的完成百分比问题。正如您所提到的,使用了 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."
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多