转换格式表中的WMI创建日期 [英] Convert WMI Creationdate in Format-Table
问题描述
我正在尝试将我的计算机中的所有 svchost
进程放入格式良好的表中,该表包含格式化的日期时间,但到目前为止还没有这样做。
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
推荐答案
使用选择对象
代替 ForEach-Object
。这样一来,您可以选择对象的特定属性,还可以添加计算出的属性:
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)}}
如果您想这样做使用 ForEach-Object
,您必须创建新对象:
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)
}
}
这篇关于转换格式表中的WMI创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!