两个相同的属性与预期不匹配

时间:2019-01-16 17:55:17

标签: powershell

从下面的示例中可以看到,我期望两个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

1 个答案:

答案 0 :(得分:0)

问题在于日期时间是通过保存为JSON然后从JSON检索而修改的,我们可以在下面看到。似乎JSON会将所有内容都舍入到小数点后的毫秒数内:

 var initialCollection = new bool[] { false, true };
  var projectedCollection = initialCollection.Select(o => (initialObj: o, Counter: 0)).ToArray();
  (bool initialObj, int Counter) d = new ValueTuple<bool,int>();
  for (int i = 0; i < 10; i++)
   {
     d = projectedCollection.First(o => o.initialObj == (i % 2 == 0));
     Console.WriteLine($"For initial obj {d.initialObj}, counter is now {++d.Counter}");
     }