A Datetime object string value coming from MSGraph is corrupted with an extra ending space, what is causing this ?

BOUCHARD, David (MTL) 0 Reputation points
2025-02-20T22:03:53.8533333+00:00

I am retrieving lastsigninDateTime of users with MSGraph, and I came upon this error with one of the users. It seems like the string value has an added space or null character in this case for some reason, and it can't properly convert this to a Datetime value. So the Get-Type and the Get-Member functions don't return the same type of object for this particular variable ! What is happening here and how can I workaround this ?

Thanks in advance for your help !

PS C:\> $dt = $SignInActivity.lastsigninDateTime
PS C:\> $dt
September 15, 2022 4:59:56 
PS C:\>if($lastLogonTS -lt $dt){echo "Anterior"}
Could not compare "09/15/2022 16:59:19" to "09/15/2022 16:59:56 ". Error: "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.DateTime"."
PS C:\> $dt.GetType()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
PS C:\> $dt | Get-Member
   TypeName: System.DateTime
Name                 MemberType     Definition
----                 ----------     ----------
Add                  Method         datetime Add(timespan value)
AddDays              Method         datetime AddDays(double value)
...
PS C:\> $dt.ToString()
System.Object[]
PS C:\> $dt.ToFileTimeUTC()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $dt.ToFileTimeUTC()
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,823 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.