1

I have this command:

Get-WmiObject win32_service |
  ? {$_.Name -like '*Front*'} |
    ? {$_.PathName -like '*logdir*'} |
      select Name, PathName

Its ouput is like:

Frontapp (Frontapp1) D:\Application\Frontapp\Frontapp.exe -service  -dbType mssql -ORBSvcConf D:\Frontapp83\Frontapp\svc.conf -connections 5 -connectionPoolSize 64 -logdir D:\Frontapp\log1

Frontapp (Frontapp0) D:\Frontapp83\Frontapp\Frontapp.exe -service  -dbType ora -ORBSvcConf D:\Frontapp83\Frontapp\svc.conf -connections 35 -connectionPoolSize 64 -logdir D:\Frontapp\log0

How can I remove information from the output, just to obtain the -logdir path.

The output needed:

Frontapp (Frontapp1) D:\Frontapp\log1

Frontapp (Frontapp0) D:\Frontapp\log0
mklement0
  • 382,024
  • 64
  • 607
  • 775
J10se
  • 11
  • 1
  • 1
    As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell [Core] (version 6 and above), where all future effort will go, doesn't even _have_ them anymore. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 May 13 '20 at 22:45

2 Answers2

3

Use a calculated property:

Get-WmiObject win32_service |
  ? { $_.Name -like '*Front*' -and $_.PathName -like '*logdir*' } |
    select Name, @{ n='LogDir'; e={ $_.PathName -replace '^.+logdir\s+' } }
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

Split-Path can be helpful here...

Get-WmiObject win32_service |
? {$_.Name -like '*Front*'} |
? {$_.PathName -like '*logdir*'} |
Select Name, @{N="PathName";E={Split-Path $_.PathName}}
Avshalom
  • 8,657
  • 1
  • 25
  • 43