我想运行powershell命令,获取当前windows服务的信息。
例如Redis:
当前路径为:“C:\Program Files\Redis\redis-server.exe”
Get-WmiObject win32_service |?{$_.Name -like 'redis*'} | Select-Object Name, DisplayName, state, startmode, @{Name="Path"; Expression={$_.PathName.split('"')}}| convertto-json
{
"Name": "Redis",
"DisplayName": "Redis",
"state": "Running",
"startmode": "Auto",
"Path": "C:\\Program Files\\Redis\\redis-server.exe"
}
但我想用这条路:“C:\Program Files\Redis”
Get-WmiObject win32_service |?{$_.Name -like 'redis*'} | Select-Object Name, DisplayName, state, startmode, @{Name="Path"; Expression={$_.PathName.split('"')[1].split('\\')[0..3]}}| convertto-json
{
"Name": "Redis",
"DisplayName": "Redis",
"state": "Running",
"startmode": "Auto",
"Path": {
"value": [
"C:",
"Program Files",
"Redis",
"redis-server.exe"
],
"Count": 4
}
}
有没有办法像这样指挥
...split('\\')[0..length-1]}}
1条答案
按热度按时间mzsu5hc01#
使用
Select-Object -SkipLast 1
:虽然一个更简单的选择是使用
Split-Path
在拆分之前删除路径的叶子部分: