locked
start-service -WarningAction : how to skip the warning and move on RRS feed

  • Question

  • hello,

    I am wishing to get some ideas on how to resolve the following issue.
    I need to scan the server OU and then check to see if the server has the healthservices server running.
    If it is not running then try and start the serverices.

    It all works great apart from when it tries to start the services and I then get stuck in a 
    "WARNING: Waiting for service 'Microsoft Monitoring Agent (HealthService)' to start..."

    I have tried everything that I can think of but when this warning occurs I can do the following

    let it just loop for ever saying the warning message
    or stop the script 

    #Search all server in the DOMIAN and check the HealthServices is running.  if it is not try and start it


    $OUServices = Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=domian,DC=dc" | Select-Object -ExpandProperty DNSHostName


    Foreach ($servers in $OUServices) {

        #servers with test in it to be skipped
                if (($servers -like "*test.FQDN"))  {
                  }
                Else {
                 #Test connection
                 if (Test-Connection $servers -Count 1 -Quiet) 
                 { 
                    Write-host "$Servers is ONLINE" 
                    $isok = Get-Service -Name HealthService -computername $servers -ErrorAction Ignore | Select-Object -ExpandProperty status
                    if ($isok -eq "Running") 
                        {  write-host "$Servers MOM Service it is running" }
                    Else {
                    write-host "$Servers MOM Service is not running; Trying to START the SERVICE"
                     Try
                    #the below line is causing me the issues
      { Get-Service -Name HealthService -computername $servers | Start-Service -WarningAction Ignore }
                     Catch
                     {Write-Host "unable to start MOm on $Servers"
                     Break}
               
                   
                    
                    }
                    }
                    Else {
                    Write-Host "$Servers is OFFLINE"
                    }
                }
        }
    Tuesday, February 27, 2018 2:35 AM

All replies

  • I don't see how we can help you with fixing services that don't behave properly.

    Have you tries "-WarningAction SilentlyContinue"?  If this doesn't work you will have to post in the "HealthServices" or "DirectoryServices" forum for help.


    \_(ツ)_/

    Tuesday, February 27, 2018 2:44 AM
  • I fix it. instead of using 

    Get-Service -Name HealthService -computername $servers | Start-Service -WarningAction Ignore

    I am using 

    Get-Service -Name HealthService -computername $servers | ReStart-Service -WarningAction Ignore

    Why is it working with Restart-service ?

    Tuesday, February 27, 2018 2:56 AM
  • Use "SilentlyContinue" as I noted above.  "Ignore" does not prevent the message.


    \_(ツ)_/

    Tuesday, February 27, 2018 8:34 AM
  • That worked for me, also I learned if you don't need to retrieve the status 2 times if its a small service, try using: 
    Script:$SERVICE_NAME=xxxx
    Script:$SERVICE_NAME | Start-Service -pass  -WarningAction SilentlyContinue | Select-Object -ExpandProperty Status
    Running
    Wednesday, July 15, 2020 12:24 AM
  • In addition to JRV's advice, your placing the Get-Service and Start-Service in a Try/Catch won't work as you'd expect unless you add "-ErrorAction Stop" to the cmdlets in the Try block.

    FYI, instead of "Get-Service | Start-Service" you can use just "Set-Service -ComputerName $servers -Name HealthService -Status Running -ErrorAction Stop"


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Wednesday, July 15, 2020 1:43 AM