Get-Service *sql* | sort DisplayName | out-file c:/servicelist.txt
I have a one line PowerShell script to extract list of all services running on my local machine, now, in addition to displaying "Status", "Name" and "DisplayName" I also want to display "Path to executable"
我有一个单行PowerShell脚本来提取我本地机器上运行的所有服务的列表,现在,除了显示“Status”,“Name”和“DisplayName”之外,我还想显示“可执行文件的路径”
2 个解决方案
#1
36
I think you'll need to resort to WMI:
我想你需要求助于WMI:
Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, State, PathName
Update If you want to perform some manipulation on the selected data, you can use calculated properties as described here.
更新如果要对所选数据执行某些操作,可以使用此处所述的计算属性。
For example if you just wanted the text within quotes for the Pathname, you could split on double quotes and take the array item 1:
例如,如果您只想在路径名的引号内使用文本,则可以拆分双引号并获取数组项1:
Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('"')[1]}} | Format-List
#2
4
A variant on the WMI Query that may be faster (I just had to do this for an SCCM Client)
WMI查询的变体可能更快(我只需要为SCCM客户端执行此操作)
$SQLService=(get-wmiobject -Query 'Select * from win32_service where Name like "*SQL*"') | Select-object Name, DisplayName, State, Pathname
The other trick is to trap for the multiple SQL results if you want the path names without the Double Quotes (so you can action upon them)
另一个技巧是如果你想要没有Double Quotes的路径名来捕获多个SQL结果(这样你就可以对它们采取行动)
$SQLService | Select-Object Name, DisplayName, State, @{Name='PathName';Expression=$_.Pathname.replace('"','')}
The big advantage to using -query
in the get-wmiobject
(or get-ciminstance
) is the speed of processing. The older example gets a full list and then filters, whilst the latter grabs a very direct list.
在get-wmiobject(或get-ciminstance)中使用-query的最大好处是处理速度。较旧的示例获取完整列表然后过滤,而后者获取非常直接的列表。
Just adding in two cents :)
只需加两美分:)
Cheers all! Sean The Energized Tech
干得好! Sean The Energized Tech
#1
36
I think you'll need to resort to WMI:
我想你需要求助于WMI:
Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, State, PathName
Update If you want to perform some manipulation on the selected data, you can use calculated properties as described here.
更新如果要对所选数据执行某些操作,可以使用此处所述的计算属性。
For example if you just wanted the text within quotes for the Pathname, you could split on double quotes and take the array item 1:
例如,如果您只想在路径名的引号内使用文本,则可以拆分双引号并获取数组项1:
Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('"')[1]}} | Format-List
#2
4
A variant on the WMI Query that may be faster (I just had to do this for an SCCM Client)
WMI查询的变体可能更快(我只需要为SCCM客户端执行此操作)
$SQLService=(get-wmiobject -Query 'Select * from win32_service where Name like "*SQL*"') | Select-object Name, DisplayName, State, Pathname
The other trick is to trap for the multiple SQL results if you want the path names without the Double Quotes (so you can action upon them)
另一个技巧是如果你想要没有Double Quotes的路径名来捕获多个SQL结果(这样你就可以对它们采取行动)
$SQLService | Select-Object Name, DisplayName, State, @{Name='PathName';Expression=$_.Pathname.replace('"','')}
The big advantage to using -query
in the get-wmiobject
(or get-ciminstance
) is the speed of processing. The older example gets a full list and then filters, whilst the latter grabs a very direct list.
在get-wmiobject(或get-ciminstance)中使用-query的最大好处是处理速度。较旧的示例获取完整列表然后过滤,而后者获取非常直接的列表。
Just adding in two cents :)
只需加两美分:)
Cheers all! Sean The Energized Tech
干得好! Sean The Energized Tech