Check Service, if stopped prompt to start or take no action

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Tadcrazio Tadcrazio 10 months ago.

  • Author
    Posts
  • #34797
    Profile photo of Tadcrazio
    Tadcrazio
    Participant

    Hi guys,
    I'm creating a little tool and i'm having issues getting it to do what I need. I'd like to for users to use this to check for stopped services. If the services are stopped, then prompt if they want to start..

    Here is what i'm working with.. May be time to blow it out of the water completely..

    I know there is quite a few errors.. I can't even get $services to show properly.. If i do not set it to a variable it shows fine..

    $credential = XXX
    $Server= MyService
    
    do{
    
        write-host "    1) Check for stopped Services on Servers"  
    	
    	   $selectedOption = read-host
    
        switch ($selectedOption)
        {
    
    1{
    	Write-Host "Checking for stopped Services...
            "
        $Services = Invoke-Command $Server {Get-WmiObject win32_service -Filter "startmode = 'auto' AND state = 'running' and Name like 'MY%'"} -Credential $credential
             
            if ($Services -ne $null)
                {Clear-host
                    $Services
    				## So if Services are stopped then Prompt
    				#Write-Host "What would you like to do?" 
                    #Write-Host "1.) Attempt to start the Stopped Services"
                    #Write-Host "2.) Take no Action"
    					$UserAction = read-host
                    switch ($UserAction)
                    {
                        1{ 
                               ForEach ( $svc IN $services) {
                        invoke-command {Start-Service $svc} -credential $credential
                           }
                        }
    
                        2{
                           Write-Host "No Action Taken"
                            }
    
                }
                else 
                    {
                    Clear-host
                    Write-Host ("No stopped services found!")
                    }
    			
    }
    
  • #34800
    Profile photo of Yuan Li
    Yuan Li
    Participant

    Hi Mate,

    Nice to meet you. It's my first reply on this forum.

    Why do you want to use invoke-command ? You can directly use
    get-wmiobject -computername computer -class win32_service -filter ""

    or even easier use

    get-service -computername computer | where-object {$_.name -like "xx" -and $_.starttype -like "automatic"} | select name, status, starttype

  • #34827
    Profile photo of Tadcrazio
    Tadcrazio
    Participant

    Interesting.. I suppose I am always used to it.. just how i learned. I've had trouble with Get-Service doing that way with my credentials.. But I can do it with WMI Object.

    Get-WmiObject Win32_Service -Computer $Server -Credential $Credential -Filter "startmode = 'auto' AND state != 'running' AND DisplayName LIKE '%XX%' "

You must be logged in to reply to this topic.