While trying to write a lastLogon date to DB from PS I am getting the output 10-10-2021 21:14:40 but when writing this to my DB (MS SQL) the date converts 1900-01-01 00:00:00.000 instead of 2021-10-10 21:14:40.000 and any attempts to convert the output using e.g. $DateStr = $lastLogon.ToString("yyyy-MM-dd HH:mm:ss.sss") has failed so far
My PS Script:
.....
Import-Module ActiveDirectory
Add-Type -AssemblyName System.Web
$EmploymentUser = Invoke-SQL -sqlCommand "select * from EmploymentUser where LastFlowEditDate Is Not NUll AND (EndDate Is Null OR EndDate > CURRENT_TIMESTAMP) AND StartDate < CURRENT_TIMESTAMP ORDER BY Initials;"
foreach($User in $EFP_EmploymentUser)
{
Get-ADUser $User.Initials -Properties lastLogon | Select @{Name="lastLogon";Expression={[datetime]::FromFileTime($_.'lastLogon')}}
Invoke-SQL -sqlCommand "UPDATE EFP_EmploymentUser SET lastLogonAD = '$($lastLogon)' WHERE ID like $($User.ID)"
}
Any one who can help?