以格式转换 WMI 创建日期-Table

Convert WMI Creationdate in Format-Table

我正在尝试将所有 svchost 进程从我的机器中获取到格式良好的 table,其中包含格式化的日期时间,但到目前为止未能这样做。

这就是我将所有进程放入数组的方法

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

然后按照我的意愿打印日期时间但作为列表

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

同时按照我的意愿打印语句 table 但没有日期时间的格式

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

我这辈子都想不出如何把它打印成

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

使用 Select-Object 而不是 ForEach-Object。这使您可以 select 对象的特定属性,还可以添加 calculated properties:

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

如果您想使用 ForEach-Object 执行此操作,您必须创建新对象:

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