AD User Last Logon information

Welcome Forums General PowerShell Q&A AD User Last Logon information

  • This topic has 5 replies, 2 voices, and was last updated 2 months ago by
    Participant
    .
Viewing 5 reply threads
  • Author
    Posts
    • #212064
      Participant
      Topics: 5
      Replies: 7
      Points: 79
      Rank: Member

      Dear All,

      Is there any Power Shell script to generate last logon time stamp for a list of users. I got the below script and it is working fine however I sound not export to csv format. Any one can help to modify the script adding export-csv to the below script

      # Get a list of every domain controller's name
      $dcNames = Get-ADDomainController -Filter * |
      Select-Object -ExpandProperty Name |
      Sort-Object
      
      # Get list of users from Text File
      $users = Get-Content "C:\AD-Scripts\users.txt" | Get-ADUser -Properties *
      
      # Hashtable used for splatting for Get-ADUser in loop
      $params = @{
      "Properties" = "lastLogon"
      }
      
      foreach ( $user in $users ) {
      # Set LDAPFilter to find specific user
      $params.LDAPFilter = "(sAMAccountName=$($user.SamAccountName))"
      # Clear variables
      $latestLogonFT = $latestLogonServer = $latestLogon = $null
      # Iterate every DC name
      foreach ( $dcName in $dcNames ) {
      # Query specific DC
      $params.Server = $dcName
      # Get lastLogon attribute (a file time)
      $lastLogonFT = Get-ADUser @params |
      Select-Object -ExpandProperty lastLogon
      # Remember most recent file time and DC name
      if ( $lastLogonFT -and ($lastLogonFT -gt $latestLogonFT) ) {
      $latestLogonFT = $lastLogonFT
      $latestLogonServer = $dcName
      }
      }
      if ( $latestLogonFT -and ($latestLogonFT -gt 0) ) {
      # If user ever logged on, get DateTime from file time
      $latestLogon = [DateTime]::FromFileTime($latestLogonFT)
      }
      else {
      # User never logged on
      $latestLogon = $latestLogonServer = $null
      }
      # Output user
      $user | Select-Object `
      SamAccountName,
      @{Name = "LatestLogon"; Expression = {$latestLogon}},
      @{Name = "LatestLogonServer"; Expression = {$latestLogonServer}}
      }
      
    • #212094
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Could you please go back and fix/edit your post and format your code using the code tags “PRE“? Thanks.

      Read Me Before Posting! You’ll be Glad You Did!

      Did you try to pipe the output of that script to a Export-Csv?

    • #212193
      Participant
      Topics: 5
      Replies: 7
      Points: 79
      Rank: Member

      Fixed the code with PRE tag.

      Yes, I have added export-csv but I found only the last user information has been exported.

    • #212202
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Fixed the code with PRE tag.

      Great. Thanks.

      Yes, I have added export-csv but I found only the last user information has been exported.

      I did not say to add Export-Csv I asked if you tried already to pipe the output of that script to Export-CSV. What do you usually do with this script? Does it output what you expect? If the answer is yes you can simply pipe the output of that script to Export-CSV. You don’t need to modify it. You could create a new script run this one in the new script and pipe the ouput to Export-CSV in that new script.

      If you insist to modify the original script you’d need to collect the output of the foreach loop in a variable and later pipe that variable to Export-Csv.

    • #212499
      Participant
      Topics: 5
      Replies: 7
      Points: 79
      Rank: Member

      I tried to pipe the out to Export-CSV but getting below error

      At C:\AD-Scripts\Lastlogon.ps1:45 char:3
      + } | export-csv c:\temp\UserList.csv
      +   ~
      An empty pipe element is not allowed.
          + CategoryInfo          : ParserError: (:) [], ParseException
          + FullyQualifiedErrorId : EmptyPipeElement
      
    • #212511
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      You told that the script is working fine. How do you run this script? You code snippet still looks like you modified the script. Don’t do that. Just run the script and pipe the output to Export-Csv. What’s so hard to understand on that?

      PS C:\> 'Complete Path To Your Perfectly Fine Running Script.ps1' | export-csv c:\temp\UserList.csv 
Viewing 5 reply threads
  • You must be logged in to reply to this topic.