【发布时间】:2019-01-17 02:38:10
【问题描述】:
正如您从下面的示例中看到的,我期望两个 StartTime 属性在相互比较时返回 true。当我比较 StartTime 属性时,它返回 false,但当我比较任何其他属性时,它返回 true,如使用 Name 属性所示。这是日期时间的怪癖吗?
$MeterLog = Get-Content -Raw -Path (Join-Path $Path $File) | ConvertFrom-JSON
$OpenApplications = get-process | where-object {$_.mainwindowhandle -ne 0 -and $_.Product -ne "Microsoft® Windows® Operating System"} | select-object Name, Description, Product, ProductVersion, Path, Company, StartTime
PS C:\ProgramData\agent> $OpenApplications[0] | gm
TypeName: Selected.System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Company NoteProperty System.String Company=Google Inc.
Description NoteProperty System.String Description=Google Chrome
Name NoteProperty string Name=chrome
Path NoteProperty System.String Path=C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Product NoteProperty System.String Product=Google Chrome
ProductVersion NoteProperty System.String ProductVersion=71.0.3578.98
StartTime NoteProperty datetime StartTime=11/01/2019 13:17:19
PS C:\ProgramData\agent> $MeterLog.data[0] | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Company NoteProperty string Company=Google Inc.
Description NoteProperty string Description=Google Chrome
Name NoteProperty string Name=chrome
Path NoteProperty string Path=C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Product NoteProperty string Product=Google Chrome
ProductVersion NoteProperty string ProductVersion=71.0.3578.98
StartTime NoteProperty datetime StartTime=11/01/2019 13:17:19
日期比较返回 false:
PS C:\ProgramData\agent> $MeterLog.data[0].StartTime -eq $OpenApplications[0].StartTime
False
任何其他返回 true 的属性示例:
PS C:\ProgramData\agent> $MeterLog.data[0].Name -eq $OpenApplications[0].Name
True
编辑:
PS C:\ProgramData\agent> $MeterLog.data[0].StartTime
11 January 2019 13:17:19
PS C:\ProgramData\agent> $OpenApplications[0].StartTime
11 January 2019 13:17:19
【问题讨论】:
-
你是如何在两个变量中获取这些信息的?那就是 $OpenApplications 和 $MeterLog。
-
datetime 对象比默认显示的内容更详细。例如,您的两个道具显示秒为最少部分。还有毫秒...
-
@Persistent13 已更新以显示来源。
$Open...直接来自运行get-process。$Meter...是从 JSON 转换而来的。 -
@Lee_Dailey 我似乎无法证明这是真的——你能建议吗?
-
@Arbiter - 尝试
.Ticks、.TimeOfDay或.Milliseconds属性
标签: powershell