1

I've make a powershell script with running multiple powershell. I would like to do a GUI progressbar that will advandce according to the content of a log.txt file in real time. (All my script write in this file when they finish).

For now I've made this, it's works but my logs need to be finish and didn't work in real time.

# Pour bloquer le resize du form et supprimer les icones Minimize and Maximize
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form.MaximizeBox = $False
$form.MinimizeBox = $False
# Choix de la taille
$form.Size = New-Object System.Drawing.Size(400,370)

# Barre de progression
$progress = New-Object System.Windows.Forms.ProgressBar
$progress.Location = New-Object System.Drawing.Point(20,185)
$progress.Name = 'progressBar'
$progress.Step = 1
$progress.Size = New-Object System.Drawing.Size(350,23)
$form.Controls.Add($progress)

# Bouton OK
$button_ok = New-Object System.Windows.Forms.Button
$button_ok.Text = "OK"
$button_ok.Size = New-Object System.Drawing.Size(355,40)
$button_ok.Location = New-Object System.Drawing.Size(20,230)
$form.Controls.Add($button_ok)

$CheminLOGS = "c:\logs.txt"


$button_ok.Add_Click(
{

    Do {
    $Check1 = Select-String -Path $CheminLOGS -Pattern "FIN SCRIPT-PHASE1"
    Write-Host "La Phase1 n'est pas terminee"
    TIMEOUT 5 /nobreak
}Until($Check1 -Like "$CheminLOGS*")

Write-Host "PHASE 1 TERMINEE"
$progress.Value = 25

Do {
    $Check2 = Select-String -Path $CheminLOGS -Pattern "FIN SCRIPT-PHASE2"
    Write-Host "La Phase2 n'est pas terminee"
    TIMEOUT 5 /nobreak
}Until($Check2 -Like "$CheminLOGS*")
   
   
Write-host PHASE 2 Terminée

$progress.Value = 100

})


$form.Topmost = $true
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

Can you help me ?

3
  • You could use Get-Content $CheminLOGS -Wait | Select-String -Pattern … | ForEach-Object { … } to process log file in real time. Commented Jan 9, 2023 at 16:01
  • To break from ForEach-Object "loop", use Where-Object or Select-Object -First 1. Commented Jan 9, 2023 at 16:08
  • Sorry my english is not good, i need help for the progressbar part. that i wich is for exemple : Each time a new line is add in the log file, the progressbar advance of 10%. Even if the log file change after the command '$Form.ShowDialog()' Commented Jan 10, 2023 at 7:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.