n00b n00b - 3 months ago 34
PowerShell Question

show progress of running scripts using powershell

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()

Answer

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()