Hi guys,
I have a script PowerShell Studio form with a script behind, which is searching the Active Directory for printer queues. So, when I hit the "Scan" button, it starts searching the AD and will then display the results in a datagridview.
My problem is that this scan might take some 10-15 seconds, maybe even more, depending on the connection speed.
Therefore I added a ProgressbarOverlay and set the "Visible" property to $false and the "Style" to Marquee
What I want to achieve: when I press the "Scan", the ProgressbarOverlay should appear and run until the command has finished. Then disappear again.
This is what I tried so far:
$buttonScan_click={
$progressbaroverlay1.visible = $true
Get-ADObject -filter {objectclass -eq "printQueue"} -Searchbase $Searchbase -Properties serverName,printerName,Location,Description,driverName | foreach-object {$datagridview1.Rows.Add($_.serverName,$_.printerName,$_.Location,$_.Description,$_.driverName)}
$progressbaroverlay1.visible = $false
}
In this case, the progressbaroverlay does not even appear (because the $true and $false commands are executed both at the end).
When I delete the last row, which is setting "Visible" to $false, then the progressbar appears, but only after the scan has finished and the datagridview has been populated. But it's too late. I need it to appear before the Get-ADObject command and disappear afterwards.
Does anyone have any ideas?
How to make a Progressbar run while a command is running
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: How to make a Progressbar run while a command is running
I figured it out!
It was because the form wasn't refreshing, so I had to do it like this:
$buttonScan_click={
$progressbaroverlay1.visible = $true
$MainForm.Refresh()
Get-ADObject -filter {objectclass -eq "printQueue"} -Searchbase $Searchbase -Properties serverName,printerName,Location,Description,driverName | foreach-object {$datagridview1.Rows.Add($_.serverName,$_.printerName,$_.Location,$_.Description,$_.driverName) | Foreach-Object {$MainForm.Refresh()}}
$progressbaroverlay1.visible = $false
}
But take care! The Refresh for each object will slow down the search a lot!! Instead of taking 5 seconds to search, it took me about 30-40 seconds.
It was because the form wasn't refreshing, so I had to do it like this:
$buttonScan_click={
$progressbaroverlay1.visible = $true
$MainForm.Refresh()
Get-ADObject -filter {objectclass -eq "printQueue"} -Searchbase $Searchbase -Properties serverName,printerName,Location,Description,driverName | foreach-object {$datagridview1.Rows.Add($_.serverName,$_.printerName,$_.Location,$_.Description,$_.driverName) | Foreach-Object {$MainForm.Refresh()}}
$progressbaroverlay1.visible = $false
}
But take care! The Refresh for each object will slow down the search a lot!! Instead of taking 5 seconds to search, it took me about 30-40 seconds.
Re: How to make a Progressbar run while a command is running
I just tried it and the search process is working really fast, but the refresh of the progressbar alone is making the progressbar blinking and lagging.
Nevertheless, thanks for your idea! Appreciate it!
Nevertheless, thanks for your idea! Appreciate it!
Re: How to make a Progressbar run while a command is running
This is likely due to your tying to refresh the control in a single even rather than on an event.
Use an event to set the pbar value. If you are updating the grid then pace the increment code in the grid change event and the form and controls should run more smoothly.
Use an event to set the pbar value. If you are updating the grid then pace the increment code in the grid change event and the form and controls should run more smoothly.
Re: How to make a Progressbar run while a command is running
I am not sure I can follow you.
I am still at my beginning in scripting.
Maybe a simple example from your side would enlighten me.
I don't know what you mean by bpar and pacing the increment code in the grid change event.
I am still at my beginning in scripting.
Maybe a simple example from your side would enlighten me.
I don't know what you mean by bpar and pacing the increment code in the grid change event.
Re: How to make a Progressbar run while a command is running
The following is all you can do with the button without causing trouble.
Start by reading the blog here on the new custom progressbaroverlay control. Once you are clear as to how it works I suspect you will see how to do it.
The datagridview is being updated a row at a time. The pbar(prograssbar) is not being updated. It will not display until it gets the first update message. That should happen on the update event of the datagridview.
YOu might also read the blogs on how to build forms. It will help you to understand how forms work. They are driven by events and not code. Code pretty much just sets the stage for how the form behaves.
PowerShell Code
Double-click the code block to select all.$buttonScan_click={ $progressbaroverlay1.visible = $true Get-ADObject -filter {objectclass -eq "printQueue"} -Searchbase $Searchbase -Properties serverName,printerName,Location,Description,driverName | foreach-object{ $datagridview1.Rows.Add($_.serverName,$_.printerName,$_.Location,$_.Description,$_.driverName) } $progressbaroverlay1.visible = $false }In the $datagridview nchaange" or "onupdate you might be able to update the progress bar.
Start by reading the blog here on the new custom progressbaroverlay control. Once you are clear as to how it works I suspect you will see how to do it.
The datagridview is being updated a row at a time. The pbar(prograssbar) is not being updated. It will not display until it gets the first update message. That should happen on the update event of the datagridview.
YOu might also read the blogs on how to build forms. It will help you to understand how forms work. They are driven by events and not code. Code pretty much just sets the stage for how the form behaves.
Re: How to make a Progressbar run while a command is running
Alright sir, thank you for your time and effort !
As I said, I am still at the beginning, but I ensure you that I will progress with Powershell and Powershell Studio!
Have a nice day!
As I said, I am still at the beginning, but I ensure you that I will progress with Powershell and Powershell Studio!
Have a nice day!
Re: How to make a Progressbar run while a command is running
Here is a complete working example. The ProgressBarOverlay is already fully evented and it runs very nicely. We just need to feed the grid a row at a time
I have places a timer in to synthesize waits so you can actually see the bar progress. The process is restartable and listens for events so the stop button will work.
Run it. Along with the links I posted you will be able to better understand how it works.
File attached:
I have places a timer in to synthesize waits so you can actually see the bar progress. The process is restartable and listens for events so the stop button will work.
Run it. Along with the links I posted you will be able to better understand how it works.
File attached:
- Attachments
-
- Demo-ProgressBar.zip
- Demo-ProgressBarOverlay
- (2.61 KiB) Downloaded 2592 times
Re: How to make a Progressbar run while a command is running
I recommend reading the following blog articles:
Creating Responsive Loops:
http://www.sapien.com/blog/2011/07/15/p ... ive-loops/
Creating Responsive Forms:
http://www.sapien.com/blog/2012/05/16/p ... ive-forms/
David
Creating Responsive Loops:
http://www.sapien.com/blog/2011/07/15/p ... ive-loops/
Creating Responsive Forms:
http://www.sapien.com/blog/2012/05/16/p ... ive-forms/
David
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.