Custom Code - Progress bar for stopping/starting/restarting.

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
Locked
User avatar
Alcasczar
Posts: 31
Joined: Thu Oct 08, 2015 5:21 pm

Custom Code - Progress bar for stopping/starting/restarting.

Post by Alcasczar »

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

User avatar
SAPIEN Support Forums
Posts: 945
Joined: Wed Dec 03, 2014 2:26 pm

Custom Code - Progress bar for stopping/starting/restarting.

Post by SAPIEN Support Forums »

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: If you asked in the wrong forum, just copy your question to the right forum.

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
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Custom Code - Progress bar for stopping/starting/restart

Post by jvierra »

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/

User avatar
Alcasczar
Posts: 31
Joined: Thu Oct 08, 2015 5:21 pm

Re: Custom Code - Progress bar for stopping/starting/restart

Post by Alcasczar »

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

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Custom Code - Progress bar for stopping/starting/restart

Post by jvierra »

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.

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Custom Code - Progress bar for stopping/starting/restart

Post by jvierra »

some services ccan provide a pcing which can be monitored like this:
PowerShell Code
Double-click the code block to select all.
get-service MSSQl$* -ComputerName OMEGA| 
     RESTART-service -Verbose
This 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...

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Custom Code - Progress bar for stopping/starting/restart

Post by jvierra »

There is also one simple method I failed to note.

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

User avatar
Alcasczar
Posts: 31
Joined: Thu Oct 08, 2015 5:21 pm

Re: Custom Code - Progress bar for stopping/starting/restart

Post by Alcasczar »

Thanks, I will give those a try !

Cheers
Alc

User avatar
Alcasczar
Posts: 31
Joined: Thu Oct 08, 2015 5:21 pm

Re: Custom Code - Progress bar for stopping/starting/restart

Post by Alcasczar »

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
I seemed to get syntax error on this, I think it was because of a missing closing parens.

Code: Select all

while((Get-Job $job).Status -eq 'Running') {
     $progressbar1.Value+=1
     sleep -mil 500
Once I did this no more syntax errors but getting this logic to actually show anything is another challenge. Do I change the restart-service with this logic?

Currently for the restart I have it as

Code: Select all

$button_add_click({restart-service $service})
Thanks for your help

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Custom Code - Progress bar for stopping/starting/restart

Post by jvierra »

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.

Locked