Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

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

Avatar of Zephyr ICT
Zephyr ICT
Flag of Belgium image

ComputerName $Server

Shouldn't that be "ComputerName $ServerList"
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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 :)
Avatar of Leo Torres

ASKER

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

You always provide great help. So I figured I give you points.