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