none
How to get the particular service is running or not- if particular service not exist display the results RRS feed

  • Question

  • Hi Team,

    I am in the process of querying the particular services is running or not.My requirement is to pull the particular service

    at the same time if that service not exist I should the results like that services not exists. I have written the function

    but its is not working.can some one please help on this.

     sname = "some service name"
    function servicesinfo{  
    param(  
     $computername =$env:computername  
     )  
      
      
    $infserv =Get-WmiObject   -Class win32_service    -ComputerName $computername   -Filter "Name Like '%$sname%'"   
    if($infserv) {
    
    $name = $infserv.Name$
    
    $startmode= $infserv.StartMode
    $state= $infserv.State
    
    
     $sresults =new-object psobject  
     $sresults |Add-Member noteproperty ComputerName $computername 
     $sresults |Add-Member noteproperty Name $name
     $sresults |Add-Member noteproperty StartMode  $startmode
    
    
    
      #Display the results  
      
     $sresults |  Select-Object PSComputerName,Name,StartMode,State  
      
     } 
    
    
    
     else
    
     {
     
      
     $sresults =new-object psobject  
     $sresults |Add-Member noteproperty ComputerName $computername 
     $sresults |Add-Member noteproperty Name "Na" 
     $sresults |Add-Member noteproperty StartMode "Na"  
     
     }
    }
    
    ####################################
     $allservers = "server location"
     $infoser =@()  
      
      
     foreach($allserver in $allservers){  
      
     
     $infoser+= servicesinfo $allserver  
     }  
      
     $infoser  
    ###################################



    
    
    Monday, December 10, 2018 3:22 PM

All replies

  • Whats not working? what error messages are you getting?
    Monday, December 10, 2018 3:34 PM
  • issue with the Parameters you defined,

    param(  
     $computername  
     )  
      
      
    $infserv =Get-WmiObject   -Class win32_service    -ComputerName $computername   -Filter "Name Like '%$sname

    try the above, as in the $computername variable you have defined the local computer and you havn't defined $computer at all in the script.

    As WMI can accept computer name as multi valued variable.

    Also in the end , change the function call.

    Monday, December 10, 2018 4:16 PM
  • I out together something a while ago, have a look, should help.

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

    • Proposed as answer by ComputerScott Tuesday, December 11, 2018 5:28 AM
    Monday, December 10, 2018 11:49 PM