Convert WMI Creationdate in Format-Table

泄露秘密 提交于 2020-01-06 03:36:07

问题


I'm trying to get all svchost processes from my machine into a nicely formatted table, containing a formatted datetime but so far have failed to do so.

This is how I get all the processes into an array

$processes = @(gwmi -cl Win32_Process -f "name='svchost.exe'")

and following prints the datetime as I'd like it them but as a list

$processes | % {$_.Caption, $_.ConvertToDateTime($_.CreationDate)} 
svchost.exe
vrijdag 4 september 2015 20:47:03
svchost.exe
vrijdag 4 september 2015 20:47:03
svchost.exe

while following prints the statements as I'd like them as a table but without the formatting of the datetime

$processes | ft Caption, CreationDate -a
Caption     CreationDate             
-------     ------------             
svchost.exe 20150904204703.429503+120
svchost.exe 20150904204703.861565+120

I can't for the life of me figure out how to print it as

Caption     CreationDate             
-------     ------------             
svchost.exe vrijdag 4 september 2015 20:47:03
svchost.exe vrijdag 4 september 2015 20:47:03

回答1:


Use Select-Object instead of ForEach-Object. That allows you to select specific properties of your objects and also to add calculated properties:

Get-WmiObject -Class Win32_Process -Filter "name='svchost.exe'" | 
  Select-Object Caption,
                @{n='CreationDate'; e={$_.ConvertToDateTime($_.CreationDate)}}

If you want to do this with ForEach-Object you'll have to create new objects:

Get-WmiObject -Class Win32_Process -Filter "name='svchost.exe'" | 
  ForEach-Object {
    New-Object -Type PSCustomObject -Property @{
      'Caption'      = $_.Caption
      'CreationDate' = $_.ConvertToDateTime($_.CreationDate)
    }
  }


来源:https://stackoverflow.com/questions/32548652/convert-wmi-creationdate-in-format-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!