Failing to use [datetime]::FromFileTime with lastLogonTimestamp

Hello !

I’m trying to export the lastLogonTimestamp of my AD users and convert it but I get an error :

this is my full code :

# Load the System.Windows.Forms assembly to use the SaveFileDialog class
Add-Type -AssemblyName System.Windows.Forms
# Create a new SaveFileDialog object to choose the location and name of the CSV file
$FileBrowser = New-Object System.Windows.Forms.SaveFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop') 
    Filter = 'CSV files (*.CSV)|*.csv' 
}
# Show the SaveFileDialog window
$null = $FileBrowser.ShowDialog()

# Get all MSOL users
$users = Get-MsolUser -All
# Create an empty array to store the results
$results = @()
# Loop through each user
foreach ($user in $users) {
    # Get the Azure AD user extension for the current user
    $userID = Get-AzureADUserExtension -ObjectId $user.UserPrincipalName
    $ADUser = Get-ADUser -Properties * -Filter { UserPrincipalName -eq $user.UserPrincipalName }

    # Create a new object with the user's properties and add it to the results array
    $results += New-Object PSObject -Property @{
        AD_CanonicalName = $ADUser.CanonicalName
        AD_CN = $ADUser.CN
        AD_Company = $ADUser.Company
        AD_Created = $ADUser.Created
        AD_DisplayName = $ADUser.DisplayName
        AD_EmployeeID = $ADUser.employeeId
        AD_EmailAddress = $ADUser.EmailAddress
        AD_Enabled = $ADUser.Enabled
        AD_extensionAttribute10 = $ADUser.extensionAttribute10
        AD_GivenName = $ADUser.GivenName
        AD_LastBadPasswordAttempt = $ADUser.LastBadPasswordAttempt
        AD_LastLogonDate = $ADUser.LastLogonDate
        AD_Manager = $ADUser.Manager
        AD_Name = $ADUser.Name
        AD_SurName = $ADUser.Surname
        AD_lastLogonTimestamp = [datetime]::FromFileTime($ADUser.lastLogonTimestamp)
        O365_City = $user.City
        O365_Country = $user.Country
        O365_Department = $user.Department
        O365_EmployeeID = $userID.employeeId
        O365_IsLicensed = $user.IsLicensed
        O365_LastPasswordChangeTimestamp = $user.LastPasswordChangeTimestamp
        O365_Licenses = $user.Licenses
        O365_MobilePhone = $user.MobilePhone
        O365_Office = $user.Office
        O365_PhoneNumber = $user.PhoneNumber
        O365_PostalCode = $user.PostalCode
        O365_State = $user.State
        O365_StreetAddress = $user.StreetAddress
        O365_Title = $user.Title
        O365_UserPrincipalName = $user.UserPrincipalName
        O365_WhenCreated = $user.WhenCreated  
    }
}

# Check if a file name was chosen
if ($FileBrowser.FileName) {
    # Export the results to the chosen CSV file
    $results | Export-Csv -Path $FileBrowser.FileName -NoTypeInformation
}

I get this error :

`“Cannot convert argument ‘fileTime’ (value ‘System.Object[]’) of ‘FromFileTime’ to type ‘System.Int64’: 'Cannot convert value ‘System.Object[]’ of type ‘System.Object[]’ to type ‘System.Int64’.” 

What I don’t understand is that if i try it outside my script, I get the result i’m looking for :

$ADUser = Get-ADUser -Properties * -Filter { UserPrincipalName -eq "$UserEmail" }
$LLTS = [datetime]::FromFileTime($ADUser.lastLogonTimestamp)

the value of LLTS is a date as expected

Does someone knows how to get a date for “AD_lastLogonTimestamp” ?

If I remove all lines about “lastLogonTimestamp” then it works fine.

Thanks guys.

Edit : heavily edited to show my original and full code without the suggested fix from Chatgpt

Hei!

Imho You dont need to use lastlogontimestamp, as lastlogondate should work for your purpose
incase u are very sure u need it
i have provided what u need.

$userFilter = {(lastlogondate -ne “") -AND (lastlogontimestamp -ne "”)}

$hash_lastLogonTimestamp = @{Name=“LastLogonTimeStamp”;Expression={([datetime]::FromFileTime($_.LastLogonTimeStamp))}}
$users = Get-ADUser -Filter $userFilter -Properties lastlogontimestamp,lastlogondate | `
select samaccountname, $hash_lastLogonTimestamp,lastlogondate

$users | out-file c:\logs\users.csv -Append

Benoit,
Welcome to the forum. :wave:t3:

You code is hard to read and partially broken. Please go back, edit your question once again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Please go back, edit your reply once again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Hi !

Sorry, you are right, it was unreadable … I hope it’s better now.

1 Like

Tried to use your method but I failed …

YaY me !

Tried many things and ended up with this easy solution :slight_smile:

    $LLTS4 = $ADuser | Select-Object lastlogontimestamp
    $LLTS5 = ([datetime]::FromFileTime($LLTS4.lastlogontimestamp))

then

AD_lastLogonTimestamp = $LLTS5

it shows a proper date when it can and “01/01/1601 01:00:00” when it’s empty

1 Like