Powershell Progress Bar without knowing estimated time ?!

touqeeranjum 80 Reputation points
2023-02-19T19:31:29.23+00:00

Hi,

I'm using a simple Powershell script to update windows from an update package silently, the issue is it takes time and there is no feedback until after the update is over.

I'm looking to use the inbuilt progress bar in Powershell, what I'm not able to understand is how to determine the estimated time in this case, or what login to use.

I have gone through a lot of examples on progress bars in Powershell. All of them know the end values (for example counting the number of files in a folder, and then passing it to the progress bar but there is no example showing progress activity when we don't know how much time it will take.

Any that I can understand this from ?

https://www.google.com/search?q=progress+br+without+knowing+activity+end+time+powershll&oq=progress+br+without+knowing+activity+end+time+powershll&aqs=edge..69i57.8994j0j4&sourceid=chrome&ie=UTF-8

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Fabricio Godoy 2,611 Reputation points
    2023-02-19T23:11:30.0133333+00:00

    Hello @touqeeranjum

    Did u try PoshProgressBar ?

    # Install the PoshProgressBar module
    Install-Module -Name PoshProgressBar -Scope CurrentUser -Force
    
    # Import the PoshProgressBar module
    Import-Module PoshProgressBar
    
    # Set the path to the update package
    $updatePackage = "C:\path\to\update_package.msu"
    
    # Get the size of the update package
    $totalSize = (Get-Item $updatePackage).Length
    
    # Calculate the download speed by measuring the time it takes to download a test file
    $testFile = "https://example.com/testfile.txt"
    $testFileSize = (Invoke-WebRequest $testFile).Content.Length
    $testTime = Measure-Command { Invoke-WebRequest $testFile -OutFile "testfile.txt" }
    $downloadSpeed = $testFileSize / $testTime.TotalSeconds
    
    # Create a progress bar object
    $progressBar = New-ProgressBar -Title "Updating Windows" -Maximum 100 -Width 50
    
    # Download the update package silently
    Start-BitsTransfer -Source $updatePackage -Destination $env:TEMP
    $downloadSize = 0
    $remainingSize = $totalSize - $downloadSize
    
    # Update the progress bar and estimated time remaining
    while ($downloadSize -lt $totalSize) {
        $progressBar.Value = ($downloadSize / $totalSize) * 100
        $remainingTime = New-TimeSpan -Seconds ($remainingSize / $downloadSpeed)
        $remainingTimeString = $remainingTime.ToString("hh\:mm\:ss")
        $progressBar.Message = "Time remaining: $remainingTimeString"
        Update-ProgressBar $progressBar
    
        # Wait for a short period to avoid excessive updates to the progress bar
        Start-Sleep -Seconds 1
    
        # Get the size of the downloaded file and update the progress bar and estimated time remaining
        $downloadedFile = Join-Path $env:TEMP (Split-Path $updatePackage -Leaf)
        $downloadedSize = (Get-Item $downloadedFile).Length
        $downloadSize = $downloadedSize
        $remainingSize = $totalSize - $downloadSize
    }
    
    # Install the update package silently
    wusa.exe $downloadedFile /quiet /norestart
    
    # Verify that the update was installed successfully
    $progressBar.Value = 100
    $progressBar.Message = "Verifying update..."
    Update-ProgressBar $progressBar
    # TODO: Add code to verify the update
    
    # Complete the progress bar and exit the script
    $progressBar.Message = "Update complete!"
    Complete-ProgressBar $progressBar
    
    

    I hope this is work for u.

    Regards


  2. touqeeranjum 80 Reputation points
    2023-02-20T10:51:06.7633333+00:00

    So I changed

    Write-Host -NoNewLine "`rInstalling Update KB5022291 ($progressValue`%)"
    

    to

    Write-Host -NoNewLine "`rInstalling Update KB5022291 ($startValue`%)"
    

    My question is how do I stop the progress to reach 100 percent while the update is running in the background ?!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.