1

I need to show the progress of , a bunch of running batch files in a specific sequence using a powershell script. Lets say there are 10 batch files:- 1.bat, 2.bat and so on upto 10.bat. The progress of the bar should increment when 1.bat is executed , then it should increment when 2.bat gets executed and progress bar should reach 100% when 10.bat gets executed.So far i have been able to create a progress bar using powershell. How to link the progress of those batch files with this progress bar? any pointers in this direction will be helpful. Thanks in advance.

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# Init Form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 200
$Form.Text = "**OSP Installation in Progress**"

# Init ProgressBar
$pbrTest = New-Object System.Windows.Forms.ProgressBar
$pbrTest.Maximum = 100
$pbrTest.Minimum = 0
$pbrTest.Location = new-object System.Drawing.Size(10,70)
$pbrTest.size = new-object System.Drawing.Size(967,10)
$i = 0
$Form.Controls.Add($pbrTest)

# Show Form
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
3
  • 1
    Any reason for using forms.progress bar? technet.microsoft.com/en-us/library/hh849902.aspx sounds like a good solution Commented Aug 16, 2016 at 11:05
  • If you want to show progress in a form you need to create some action (i.e. OnLoad) and increment progress bar there within foreach loop. OR (as a much better solution) not use a form and use Write-Progress as suggested above. Commented Aug 16, 2016 at 11:17
  • @Deptor To show a GUI based progress bar to the user.I had tried Write_Progress initially but felt forms.progress bar shows a better GUI for user. Commented Aug 16, 2016 at 11:20

1 Answer 1

3

Something like this:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
$batches = Get-Item 'D:\Soft\Scripts\testbat\*.bat'

# Init Form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 200
$Form.Text = "**OSP Installation in Progress**"

# Init ProgressBar
$pbrTest = New-Object System.Windows.Forms.ProgressBar
$pbrTest.Maximum = $batches.Count
$pbrTest.Minimum = 0
$pbrTest.Location = new-object System.Drawing.Size(10,70)
$pbrTest.size = new-object System.Drawing.Size(967,10)
$Form.Controls.Add($pbrTest)

# Add_Shown action    
$ShownFormAction = {
    $Form.Activate()

    foreach ($b in $batches) {
        $pbrTest.Increment(1)
        Start-Process $b.FullName -Wait -WindowStyle Hidden
    }
    $Form.Dispose()
}
$Form.Add_Shown($ShownFormAction)

# Show Form
$Form.ShowDialog()
Sign up to request clarification or add additional context in comments.

2 Comments

Why not just use Write-Progress?
According to the OP, the sequence matters for these files. Get-Item should be Get-ChildItem with an appropriate sort-object

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.