While loop

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 6 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

While loop

Post by sekou2331 »

I was trying to do a while loop on a service to make an action when the state of the service 'Stopped' and I noticed that loop cant see when the state changes for the service. I wanted to make sure that I am writing this correctly. I am looking at the state of the services and while state does not equal 'Stopped' keep doing the below. When I set the service to 'Running' the while loop never breaks.


  1. $serviceState = gwmi Win32_service | ?{ $_.Name -eq "WinRM" }
  2.  
  3.  
  4.  
  5. While ($serviceState.State -ne 'Stopped')
  6. {
  7.     $serviceState.State
  8.    
  9.     Start-Sleep -seconds 5
  10.    
  11.     Write-host "Process is Running"
  12.    
  13.    
  14. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: While loop

Post by jvierra »

  1. $p = Stop-Service WinRM -PassThru
  2. $p.Status
When the command returns the service will be stopped.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: While loop

Post by sekou2331 »

So is this best practice for this situation? I shouldn't use the while loop? Lastly was the while loop written correctly? I am trying to understand what I did wrong in this situation. It is a simple while loop trying to figure out why it did not work.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: While loop

Post by jvierra »

A while loop won't work without re-querying the service. The service.
  1. $service = Get-WmiObject Win32_service -Filter "Name = 'WinRM'"
  2. if($service.StopService().ReturnValue -eq 0) {
  3.     Write-Host 'Service stopped'
  4. }else{
  5.     Write-Host 'Service failed to stop'
  6. }
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: While loop

Post by sekou2331 »

I thought I was querying the service. My issue is the service that I am really querying takes a long time to go down and stays in a Stopping state. I though I could use the while loop to check and if it doesn't change I will just kill the process attached to the service.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: While loop

Post by jvierra »

stop-Service waits.
Win32_Service waits.

I don't see why you need to think you have to check. It is only in the GUI that yuo can exit the dialog while the service is still stopping.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: While loop

Post by sekou2331 »

I understand they both wait. I am trying to find a way to set a time to kill if the service takes more then 10 minutes to come down.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: While loop

Post by jvierra »

Use:
  1. $service = Stop-Service WinRM -NoWait -PassThru
  2. $i = 0
  3. While ($service.State -ne 'Stopped'){
  4.     if($i++ -gt 60){
  5.         Write-Host 'Service not stopped!'
  6.         break
  7.     }
  8.     sleep 1
  9.     $service.Refresh()
  10. }
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: While loop

Post by sekou2331 »

Thanks! I see now.
This topic is 6 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked