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

This topic contains 2 replies, has 2 voices, and was last updated by  Tadcrazio 2 years, 5 months ago.

  • Author
  • #34797


    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
        write-host "    1) Check for stopped Services on Servers"  
    	   $selectedOption = read-host
        switch ($selectedOption)
    	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)
    				## 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)
                               ForEach ( $svc IN $services) {
                        invoke-command {Start-Service $svc} -credential $credential
                           Write-Host "No Action Taken"
                    Write-Host ("No stopped services found!")
  • #34800

    Yuan Li

    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 {$ -like "xx" -and $_.starttype -like "automatic"} | select name, status, starttype

  • #34827


    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.