Synchronization service powershell write to datetime attribute error

Hello,

I have been trying to script getting a datetime from MS Graph and output this to a virtual attribute (configured as general time) in Active Roles, but every time I receive the same error, no matter what I do:

"The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."

The variable I have is definitely a datetime, but is refusing to write out to AR. Does anyone know what format I should use, perhaps a particular string?

Script below - I have tried returning each version val, val2 and val3.

Connect-MgGraph -TenantId $tenantId -ClientId $clientId -CertificateThumbprint $certThumbprint
$val = (Get-MgBetaUser -Filter "userPrincipalName eq '$($srcObj['userPrincipalName'])'" -Property signInActivity).SignInActivity.LastSignInDateTime
Disconnect-MgGraph
$val2 = $val.ToString('yyyy-MM-dd HH:mm:ss')
$val3 = [datetime]::ParseExact($val2, 'yyyy-MM-dd HH:mm:ss', $null)
$val3