1

all

I implemented my first PowerShell script, that does some setup, sets registry keys and at then end needs to restart services. The problem is that I have only have name of the executable, but not service name. Restart-Service can work only with name of the service. Googling (well Binging also) around didn't give me much result.

I was wondering whether there is a way to restart service by executable name?

I know that I can get process by executable name, but just killing the process and starting it again is NOT good choice, since service Start/Stop functions are not called and it may not work properly.

Thanks.

4 Answers 4

4

You can try using wmi and do something like this:

(gwmi win32_service | ?{$_.pathname -match "\\executable.exe "}) | Restart-Service
Sign up to request clarification or add additional context in comments.

8 Comments

You cannot pipe WMI service instances to the Restart-Service cmdlet. The objects are not of the same type.
@Shay: Try the piping.. Do a great job!
The comments I made on @Shay's solution about the matching also apply to this solution. This will match executables: "MyExecutable.exe", "AnotherExecutable.exe" "executable.exec.exe", etc.. "\\executable.exe\b" would be better, but "\executable.exe.new.exe" would also match it (I know, unlikely).
@Rynant - The OP probably knows the executable and its path or even the params etc. OP can specify the whole. And you don't seem to know how svchost.exe works. Services don't use that as executable path.
I know what svchost.exe is, and that you would not be restarting services by matching the svchost.exe process. There are services that use it as the executable path. I get 119 services if I run: gwmi win32_service -filter "PathName like '%svchost.exe%'". My point was that $chost.exe% will match svchost.exe
|
1
Get-WmiObject -Class Win32_Service -Filter "PathName LIKE '%PartOfTheName%'" -ComputerName PC1 | Foreach-Object{
    $_.StopService()
    $_.StartService()   
}

4 Comments

What if there are multiple services with the same .exe. I have multiple services that use sqlservr.exe. Also, what if there is a service that uses "AnotherExename.exe" (the "%" wildcard will cause it to match); unlikely, but possible.
You can extend the code to deal with multiple objects, I'll update it in a minute. If you want to match parts of the service name enclose the string in %.
I wasn't saying that the match should be less strict, but more strict. What if there was a service called "chost.exe"? '%chost.exe' would also match 'svchost.exe'. Also the PathName parameter includes aguments (eg. 'msiexec.exe /V'). '%\chost.exe%' would be better, but not 100% foolproof since it would match 'chost.exec.exe' I know I'm being a pain, but I'm just trying to handle all cases :-P
@Ryhant, I'm able to extract the image name with the following command: Get-WmiObject -Class Win32_Service | Foreach-Object { Split-Path -Leaf ($_.PathName -split('\s\-|/'))[0].Trim('"')} . It it works for you as well we can write a better solution.
0

You can do this using WMI:

$process = Get-Process sqlservr| select -ExpandProperty Id

Get-WmiObject win32_Service| 
    where {$process -contains $_.ProcessId}|
    foreach {Restart-Service $_.Name}

Edit: Changed script to restart service, not just stop it.

Comments

0
#set by logic to determine if the service will restart or not
 $global:ServerWillRestart=$true 

#can be found using the name column of Get-services cmdlet
 $serviceName="Set name of the service" 
if($global:ServerWillRestart){
     $service =Get-Service | where{ $_.Name -eq $serviceName}
do{
    Write-output "The service $ServiceName will is being stopped"
    Stop-Service $service
    Start-Sleep -s 2
}
while($service.WaitForStatus("Stopped"))

do{
    Write-Output "The service $ServiceName will is being started"
    Start-Service $service
    Start-Sleep -s 2
}
while($service.WaitForStatus("Running"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.