Page 1 of 1

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

Posted: Sat Oct 17, 2015 11:48 am
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

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

Posted: Sat Oct 17, 2015 11:48 am
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 ***

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

Posted: Sat Oct 17, 2015 12:00 pm
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/

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

Posted: Sat Oct 17, 2015 2:07 pm
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

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

Posted: Sat Oct 17, 2015 2:16 pm
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.

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

Posted: Sat Oct 17, 2015 2:23 pm
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...

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

Posted: Sat Oct 17, 2015 3:00 pm
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

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

Posted: Sat Oct 17, 2015 7:06 pm
by Alcasczar
Thanks, I will give those a try !

Cheers
Alc

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

Posted: Mon Oct 19, 2015 5:12 pm
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

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

Posted: Mon Oct 19, 2015 5:26 pm
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.