none
Powershel Get-Service sometimes no replying RRS feed

  • Question

  • We are using a recurring supervision tool checking the presence of 93 services distributed on 60+ machines.

    This works very fine each quarter of an hour.

    However sometimes (typically once a week) we got some no_reply, even after a retry.

    The syntax is simple (embedded in a batch):

    powershell get-service -ComputerName ^%1 -Name ^'%2' | findstr Running

    Any idea why a get-service command could fail to reply ?

    Regards

    • Moved by Bill_Stewart Friday, March 15, 2019 6:15 PM This is not "train me in basics step-by-step in a forum" forum
    Tuesday, January 8, 2019 11:16 AM

All replies

  • why are you not running this wholesole command in PowerShell only and schedule it using Windows Scheduler.

    $computers = get-content C:\temp\computerlist.txt
    $services = get-content C:\temp\servicelist.txt
    $array = @()
    FOREACH( $COMPUTER IN $computers ) { 
    $services = get-service -computername $computername -name $services | where where { $_.status -eq "stopped" }
    
    $SERVICES | ADD-MEMBER -MemberType NoteProperty -Name COMPUTERNAME -Value $COMPUTER 
    $array += $services
    }
    
    $array | export-csv C:\temp\result.csv -notypeinformation
    
    

    Tuesday, January 8, 2019 12:07 PM
  • I have a script on my site that you could use also. Please have a look. Source File Provided Link on Site

    http://www.scriptsbyscott.com/Get_Alerts.html

            
            
    $Computername = @()
    $Service = @()
     
    Import-Csv C:\Temp\ServiceChecker\Services_Review.csv |`
     
    ForEach-Object {
     
        $Computername = $_.Computername.Trim()
     
        $Service = $_.Service.Trim()
     
        Try { 
                $Result=Get-Service -ComputerName $ComputerName -Name "$Service" -ErrorAction Stop  | select -ExpandProperty Status
     
                if ($Result -ne "running")
                    {     
                        #write-host "Server $Computername Did Not Resond to Service $Service Check Correctly - Not Equal to Running"  
                      
                        #----------------------------
                        # Configure Mail Variables
                        #---------------------------
                        $SMTPServer = "YourMailServer"
                        $SMTPFrom = "Admin@YourDomain.com"
                        $SMTPTo = "XXXXXXXXXX@vtext.com"
                        $SMTPSubject = "$Computername Error $Service Not Running"
                        $SMTPBody = "$Computername Service $Service Not Running"    
     
                        #----------------------------------------------
                        #Send Mail and Attach Custom Named Log File
                        #---------------------------------------------
                        Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody 
                    }             
            }
     
         Catch [Exception]
            {
       
                #write-host "Server $Computername Did Not Resond to Service $Service Check Correctly - Script Error on Connection $_" 
               
                #----------------------------
                # Configure Mail Variables
                #---------------------------
                $SMTPServer = "YourMailServer"
                $SMTPFrom = "Admin@YourDomain.com"
                $SMTPTo = "XXXXXXXXXX@vtext.com"
                $SMTPSubject = "$Computername Error Connection | Service Name"
                $SMTPBody = "$Computername Service $Service Service not Found or Server Not Reachable - $_"  
     
                #----------------------------------------------
                #Send Mail and Attach Custom Named Log File
                #---------------------------------------------
                Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody
     
            } 
    }         
            

    Tuesday, January 8, 2019 8:43 PM
  • Thanks for your contributions. I agree it could had been written fully in Powershell but I inherited a previous script already in Win Batch. My question is more about the Get-Service command behaviour: are there known cases where it doesn't return anything ?
    Wednesday, January 9, 2019 8:28 AM
  • There is no issue with Get-Service.  Post your issue in a WinBatch forum.

    \_(ツ)_/

    Wednesday, January 9, 2019 1:58 PM
  • How can you state there is "no issue" ? We observe this behaviour regularly. However, there are also netork issues in the DC, maybe a cause ?
    Monday, January 21, 2019 2:40 PM
  • However, there are also netork issues in the DC, maybe a cause ?

    We can't help you if you don't tell us about things like this. What are the "issues"?  

    I developed a similar monitor using VB script code at my last job. What I found was that sometimes the remote servers were in some kind of hung state and did not respond. My solution was to launch a separate cscript.exe for each computer to be analyzed. That way a problem with one server would not impact the analysis of the others. My processes executed asynchronously which was faster that analyzing servers one by one. 

    Another design that would minimize network traffic (and RPC calls) would be to schedule the service monitor task to run on each server every 15 minutes. That task would just create a file that the monitoring script would gather to generate the report.  

       

    Monday, January 21, 2019 3:33 PM