farag
farag

Reputation: 375

Check if service is running after using start-sleep

How do I check if service is running again after Start-Sleep -m 120?

Maybe there is a situation when even after 120 minutes wuauserv can be running.

$getservice = Get-Service -Name wuauserv
If($getservice.Status -eq 'Running')
{
    Start-Sleep -m 120
    Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
}
Else
{
    Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
}

Upvotes: 0

Views: 558

Answers (3)

farag
farag

Reputation: 375

$getservice = Get-Service -Name wuauserv
while($getservice.Status -eq 'Running')
{
    Start-Sleep -s 1800
$getservice = Get-Service -Name wuauserv
}
Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

Upvotes: 0

David Brabant
David Brabant

Reputation: 43499

It seems that you only want to call Get-ChildItem once your service is stopped. One option is:

$getservice = Get-Service -Name wuauserv
$getservice.WaitForStatus('Stopped')

Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | 
    Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

You can optionally specify a timeout:

$getservice.WaitForStatus('Stopped', '02:00:00')

Note that the WaitForStatus method waits approximately 250 milliseconds between each status check. If that's too heavy, you can use a while loop.

$getservice = Get-Service wuauserv
while($getservice.State -ne 'Stopped')
{
   Start-Sleep -m 10
   $getservice = Get-Service wuauserv
}

Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | 
    Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

Upvotes: 1

E235
E235

Reputation: 13440

You can run a script until the service is stopped and then run Get-ChilItem:

while($true){

    if(-not isWuauservRunning){
        Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
        break
    }
}
  • If this is part of a larger script, you can us Start-Job to run this activity on the background because of the use while($true).

You can also do it like that (I don't like this method, I think it better to check the case when the service is stopped):

function isWuauservRunning(){
    $isRunning = $false
    $service = Get-Service -Name wuauserv
    if(($service -ne $null) -and ($service.Status -eq 'Running')){
        $isRunning = $true
    }

    return $isRunning
}

function getAndRemoveItems(){
    Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
}


if(isWuauservRunning()){
    Start-Sleep -m 120
    if(-not isWuauservRunning()){
        getAndRemoveItems
    }

}else{
    getAndRemoveItems
}

Upvotes: 0

Related Questions