简体   繁体   中英

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

I need to pull Physical Execution paths of all the Windows Services on a Set of Servers, that run on Win 2k8. As, the powershell version that is shipped with this OS is 2.0, I wanted to use Get-service command instead of Get-WmiObject. I know that I can pull the physical path using the command given below

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

I donot want this command to pull the physical path but wanted to use Get-Service command that comes with PS Version 2.0.

Any help would be much appreciated.

Even with PowerShell 3, I don't see a way to get it with Get-Service.

This 1-liner will get you the pathname, albeit with a little less of the preferred "filter left" behavior:

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

Or, if you want just the string itself:

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

I wanted to do something similar, but based on searching / matching the path of the process running under the service, so I used the classic WMI Query syntax, then passed the results through 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

You're welcome to turn this into a one-liner by skipping defining and passing $pathWildSearch, or you could just back gwmi statement up to continue after the semi-colon.

@alroc did good, but there's no reason to filter all services. Querying WMI is like querying a DB, and you can just ask WMI to do the filtering for you:

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

To explore all of the meta available for that service:

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

Perhaps little less verbose,

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

This should work on command prompt as well, not just powershell.


Or else if you have process name itself you could do:

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

To read process path you would need permission, so mostly I have better luck with service name.

I was never able to do this through the Get-Service command but if your service runs as it's own process then you can use the Get-Process command for this via the following code:

(Get-Process -Name AxInstSV).path

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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