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

问题描述 投票:8回答:4

我需要在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命令。

任何帮助将非常感激。

windows powershell windows-services powershell-v2.0
4个回答
12
投票

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

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

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

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

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

2
投票

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

$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语句返回到继续。


0
投票

也许不那么冗长,

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

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


或者,如果您有进程名称本身,您可以这样做:

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

要阅读流程路径,您需要获得许可,因此大多数情况下我都有更好的服务名称。


0
投票

我无法通过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/

© www.soinside.com 2019 - 2024. All rights reserved.