Hello Group,
I've written a nice Gui interface to help out with services that my team at work uses. One small problem is that the interface I created doesn't display a progress bar when I stop/start/restart services.
I know the typical Microsoft services does so I was looking to incorporate something like that into my script that will display one. I am just not sure how to code this, I've written other progress bars before so I understand the concepts just not sure how to code one based on stopping/starting/restarting a service.
Any advice would be greatly appreciated.
Cheers
Alc
Custom Code - Progress bar for stopping/starting/restarting.
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.
- SAPIEN Support Forums
- Posts: 945
- Last visit: Thu Oct 22, 2015 1:10 pm
Custom Code - Progress bar for stopping/starting/restarting.
This is an automated post. A real person will respond soon.
Thank you for posting, Alcasczar.
Here are some hints to help you get an accurate and complete answer to your question.
Ask in the best forum:
Anticipate follow-up questions!
Did you remember to include the following?
*** Make sure you do not post any licensing information ***
Thank you for posting, Alcasczar.
Here are some hints to help you get an accurate and complete answer to your question.
Ask in the best forum:
- - Scripting question? For questions about Windows PowerShell or other scripting languages, use Scripting Answers.
- Software question? For questions about PowerShell Studio, PrimalScript, or other SAPIEN products, use Product Support Forums for Registered Customers.
- Trial question? For questions about trial versions of any SAPIEN product, use Trial Software Questions.
Anticipate follow-up questions!
Did you remember to include the following?
- 1. Product, version and build
2. 32 or 64 bit product
3. Operating system, e.g. Windows 7 64 bit.
4. Attach a screenshot, if applicable
5. Attach logs, crash reports, etc., in a ZIP file
*** Make sure you do not post any licensing information ***
Re: Custom Code - Progress bar for stopping/starting/restart
Unfortunately there is not direct way to add a progress bar to the start-stop routine (Restart-Service)
You can use the Job Tracker Control and run the script as a job. You will have to guess at the total time.
See: https://www.sapien.com/blog/2012/05/16/ ... ive-forms/
You can use the Job Tracker Control and run the script as a job. You will have to guess at the total time.
See: https://www.sapien.com/blog/2012/05/16/ ... ive-forms/
Re: Custom Code - Progress bar for stopping/starting/restart
Thank you so much for your quick reply Jvierra, I am really bummed out by this but I think since I have puchased Powershell Studio there may be some other methods I can use to at least show some display of things when they click the button. I'll see what I can do , thank you again.
Cheers
Alc
Cheers
Alc
Re: Custom Code - Progress bar for stopping/starting/restart
In Windows Forms your calls to run CmdLet that is blocking make it impossible to execute any other code until the blocking event returns.
In the NT GUI (services.msc) the services are called via a low level API written in "C". The call is likely carried out on separate thread and the progress indicator is shown until the thread terminates.
We can write this in C or C# and include it as an extension and gain the same results. Using a PSJob is much easier.
In the NT GUI (services.msc) the services are called via a low level API written in "C". The call is likely carried out on separate thread and the progress indicator is shown until the thread terminates.
We can write this in C or C# and include it as an extension and gain the same results. Using a PSJob is much easier.
Re: Custom Code - Progress bar for stopping/starting/restart
some services ccan provide a pcing which can be monitored like this:
VERBOSE: Performing the operation "Restart-Service" on target "SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)".
WARNING: Waiting for service 'SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)' to stop...
WARNING: Waiting for service 'SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)' to stop...
WARNING: Waiting for service 'SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)' to stop...
PowerShell Code
Double-click the code block to select all.get-service MSSQl$* -ComputerName OMEGA| RESTART-service -VerboseThis will return an out-of-band message every second or two until the service is started, stopped or restarted.
VERBOSE: Performing the operation "Restart-Service" on target "SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)".
WARNING: Waiting for service 'SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)' to stop...
WARNING: Waiting for service 'SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)' to stop...
WARNING: Waiting for service 'SQL Server (SQLEXPRESS) (MSSQL$SQLEXPRESS)' to stop...
Re: Custom Code - Progress bar for stopping/starting/restart
There is also one simple method I failed to note.
Start-Job to execute restart code.
Start-Job to execute restart code.
PowerShell Code
Double-click the code block to select all.$job=Start-Job -Script {Stop-Service MyService} while((Get-Job $job).Status -eq 'Running'{ $progressbar1.Value+=1 sleep -mil 500 } $progressbar1.Value=$progressbar1.MaxValue
Re: Custom Code - Progress bar for stopping/starting/restart
Thanks, I will give those a try !
Cheers
Alc
Cheers
Alc
Re: Custom Code - Progress bar for stopping/starting/restart
I seemed to get syntax error on this, I think it was because of a missing closing parens.jvierra wrote:PowerShell Code
Double-click the code block to select all.$job=Start-Job -Script {Stop-Service MyService} while((Get-Job $job).Status -eq 'Running'{ $progressbar1.Value+=1 sleep -mil 500 } $progressbar1.Value=$progressbar1.MaxValue
Code: Select all
while((Get-Job $job).Status -eq 'Running') {
$progressbar1.Value+=1
sleep -mil 500
Currently for the restart I have it as
Code: Select all
$button_add_click({restart-service $service})
Re: Custom Code - Progress bar for stopping/starting/restart
Show what? You need to know how to use a progress bar. I set it up and it works correctly. In some cases the service stops soo fast you see nothing. If the service cannot be stopped you will get an error which you will have to code for.