繁体   English   中英

如何使用 Get-Service 命令拉取 Windows 服务的物理路径

[英]How to pull physical path of a Windows Service using Get-Service command

我需要在 Win 2k8 上运行的一组服务器上提取所有 Windows 服务的物理执行路径。 由于此操作系统附带的 powershell 版本是 2.0,因此我想使用 Get-service 命令而不是 Get-WmiObject。 我知道我可以使用下面给出的命令拉出物理路径

$QueryApp = "Select * from Win32_Service Where Name='AxInstSV'"
$Path = (Get-WmiObject -ComputerName MyServer -Query $QueryApp).PathName

我不希望此命令拉物理路径,但想使用 PS 2.0 版附带的 Get-Service 命令。

任何帮助将非常感激。

即使使用 PowerShell 3,我也看不到通过 Get-Service 获得它的方法。

这个 1-liner 将为您提供路径名,尽管首选的“过滤器左侧”行为少了一点:

gwmi win32_service|?{$_.name -eq "AxInstSV"}|select pathname

或者,如果您只想要字符串本身:

(gwmi win32_service|?{$_.name -eq "AxInstSV"}).pathname

我想做类似的事情,但是基于搜索/匹配服务下运行的进程的路径,所以我使用了经典的WMI Query语法,然后通过格式表传递结果:

$pathWildSearch = "orton";
gwmi -Query "select * from win32_service where pathname like '%$pathWildSearch%' and state='Running'" | Format-Table -Property Name, State, PathName -AutoSize -Wrap

欢迎您通过跳过定义和传递 $pathWildSearch 将其转换为单行,或者您可以将 gwmi 语句备份到分号后继续。

@alroc 做得很好,但没有理由过滤所有服务。 查询 WMI 就像查询一个数据库,你可以让 WMI 为你做过滤:

(Get-CimInstance Win32_Service -Filter 'Name = "AxInstSV"').PathName

要探索可用于该服务的所有元数据:

Get-CimInstance Win32_Service -Filter 'Name = "AxInstSV"' | Select-Object *

也许不那么冗长,

wmic service where "name='AxInstSV'" get PathName

这也应该适用于命令提示符,而不仅仅是 powershell。


否则,如果您有进程名称本身,您可以执行以下操作:

wmic process where "name='AxInstSV.exe'" get ExecutablePath

要读取进程路径,您需要获得许可,因此大多数情况下我对服务名称的运气更好。

我永远无法通过 Get-Service 命令执行此操作,但是如果您的服务作为自己的进程运行,那么您可以通过以下代码使用 Get-Process 命令:

(Get-Process -Name AxInstSV).path

来源: https : //blogs.technet.microsoft.com/heyscriptingguy/2014/09/15/powertip-use-powershell-to-find-path-for-processes/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM