Powershell Stop Service

I am trying to completely stop this service. But when I try to stop it since its in a stopping state I get the error Below

Stop-Service : Service 'Adaptive Log Exporter (ALE) (AdaptiveLogExporterService)' cannot be stopped due to the following error: Cannot stop AdaptiveLogExporterService 
service on computer 'ServerNAme'.
At line:14 char:56
+ Get-Service -ComputerName $Server -Name $StopService | Stop-Service -Verbose -Fo ...
+                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Stop-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand

Open in new window



Here is the code I am using
#Test
$ServerList = "ServerName"

$StopService = "AdaptiveLogExporterService"

$ServerArray = $ServerList.split(',');


Foreach( $Server in $ServerArray ){

Get-Service -ComputerName $Server -Name $StopService | Stop-Service -Verbose -Force

}

Open in new window

LVL 8
Leo TorresSQL DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
ComputerName $Server

Shouldn't that be "ComputerName $ServerList"
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
@spraytek: No. But the basic thought is correct, it should be sufficient to just provide a list of servers, without the loop.
#Test
$ServerList = "ServerName,OtherServerName"
$StopService = "AdaptiveLogExporterService"

$ServerArray = $ServerList.split(',');
Get-Service -ComputerName $ServerList.split(',') -Name $StopService | Stop-Service -Verbose -Force -ea SilentlyContinue

Open in new window

However, I'm unclear about your exact issue. You can't stop a stopping or stopped service. You can stop the cmdlet to spit out an error in those cases, though.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Zephyr ICTCloud ArchitectCommented:
@spraytek: No. But the basic thought is correct, it should be sufficient to just provide a list of servers, without the loop.

Granted, I was thinking too simplistic :)
Leo TorresSQL DeveloperAuthor Commented:
Thanks Qlemo.

You cant do stop Service it didn't work. So I am killing the process.

This worked for me
CLS
$ProcessName = "Q1WindowsAgent.exe"
$ServerList = "ServerName"
$ServerArray = $ServerList.split(',');

 
foreach ($ComputerName in $ServerArray) {

$Process = Get-WmiObject -Class Win32_Process -ComputerName $ComputerName -Filter "name='$ProcessName'"

    IF($Process -ne $null) {

        $returnval = $process.terminate()
        $processid = $process.handle
 
            if($returnval.returnvalue -eq 0) {
                   write-host "The process $ProcessName `($processid`) terminated successfully on Server $ComputerName" 
            }
            
            else {
                    write-host "The process $ProcessName `($processid`) termination has some problems on Server $ComputerName"
            }
    }

    Else {
            Write-Host "Process Not Running On Server $ComputerName"

    }
}    

Open in new window

Leo TorresSQL DeveloperAuthor Commented:
You always provide great help. So I figured I give you points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.