PowerShell:按可执行文件名称重新启动服务

发布于 2024-11-30 01:39:36 字数 285 浏览 0 评论 0原文

我实现

了我的第一个 PowerShell 脚本,它执行一些设置、设置注册表项,最后需要重新启动服务。问题是我只有可执行文件的名称,但没有服务名称。 Restart-Service 只能与服务名称一起使用。谷歌搜索(还有必应)并没有给我带来太多结果。

我想知道是否有办法通过可执行文件名称重新启动服务?

我知道我可以通过可执行文件名称获取进程,但仅仅终止进程并重新启动它不是一个好的选择,因为服务启动/Stop 函数未被调用,并且可能无法正常工作。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你怎么敢 2024-12-07 01:39:36

您可以尝试使用 wmi 并执行以下操作:

(gwmi win32_service | ?{$_.pathname -match "\\executable.exe "}) | Restart-Service

You can try using wmi and do something like this:

(gwmi win32_service | ?{$_.pathname -match "\\executable.exe "}) | Restart-Service
无名指的心愿 2024-12-07 01:39:36
Get-WmiObject -Class Win32_Service -Filter "PathName LIKE '%PartOfTheName%'" -ComputerName PC1 | Foreach-Object{
    $_.StopService()
    $_.StartService()   
}
Get-WmiObject -Class Win32_Service -Filter "PathName LIKE '%PartOfTheName%'" -ComputerName PC1 | Foreach-Object{
    $_.StopService()
    $_.StartService()   
}
街角迷惘 2024-12-07 01:39:36

您可以使用 WMI 执行此操作:

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

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

编辑:更改脚本以重新启动服务,而不仅仅是停止它。

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.

爱你是孤单的心事 2024-12-07 01:39:36
#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"))
#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"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文