问题
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