Retrieve the Computer lastlogintimestamp?

Author Message
marcussmith

  • Total Posts : 7
  • Scores: 0
  • Reward points : 0
  • Joined: 1/30/2006
  • Status: offline
Retrieve the Computer lastlogintimestamp? Tuesday, October 07, 2008 2:42 AM (permalink)
0
Hi all

How would I use powershell to..

Read a list computers from text file and retrieve the Computer lastlogintimestamp? and output to a text file.

I have the first part.

$file ="C:\Timestamps.txt"
$Computers = Gc "C:\List.txt"
ForEach($MachineName In $Computers)

Thanks
<message edited by marcussmith on Tuesday, October 07, 2008 2:47 AM>
 
#1
    SAPIENScripter

    • Total Posts : 283
    • Scores: 2
    • Reward points : 0
    • Joined: 11/1/2006
    • Location: SAPIEN Technologies
    • Status: offline
    RE: Retrieve the Computer lastlogintimestamp? Wednesday, October 08, 2008 3:03 AM (permalink)
    0
    You will need some method of getting the computer object from your domain and then a method of converting the lastlogon value to a meaningful date.  Overall your process can look like this:

    get-content c:\list.txt | foreach { My-Function $_ } | out-file $file

    If your list has the complete DN, that simplifies things.  Can you use the Quest AD cmdlets?  That too will simplify things.  The real challenge here is the My-Function piece.  If you use ADSI to get the object , you'll need to convert the lastlogon value.  This function should help with that:

    Function Get-UTCAge {
        #get date time of the last password change
            Param([int64]$Last=0)
            if ($Last -eq 0) {
                write 0
            } else {
                #clock starts counting from 1/1/1601.
                [datetime]$utc="1/1/1601"
                #calculate the number of days based on the int64 number
                $i=$Last/864000000000
               
                #Add the number of days to 1/1/1601
                #and write the result to the pipeline
                write ($utc.AddDays($i))
            }
        } # end Get-UTCAge function

    Jeffery Hicks
    Windows PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

    Follow Me: http://www.twitter.com/JeffHicks
     
    #2
      SAPIENScripter

      • Total Posts : 283
      • Scores: 2
      • Reward points : 0
      • Joined: 11/1/2006
      • Location: SAPIEN Technologies
      • Status: offline
      RE: Retrieve the Computer lastlogintimestamp? Wednesday, October 08, 2008 3:13 AM (permalink)
      0
      Here's a script you can use
       #Get-ComputerAccount.ps1
       #v1.0 October 8, 2008
       # Jeffery Hicks
       # jhicks@sapien.com
       # http://blog.sapien.com
       #---------------------------------------------------------------------------------------
       
       # Usage: get-computeraccount.ps SERVER02
       
       #sample output
       # Name                : XPDESK01
       # DN                  : CN=XPDESK01,OU=Enterprise Servers,DC=bigcompany,DC=local
       # Description         :
       # AccountCreated      : 2/25/2008 3:22:10 PM
       # AccountModified     : 9/30/2008 5:40:00 PM
       # LastLogon           : 10/8/2008 2:08:45 PM
       # PasswordLastChanged : 9/8/2008 1:36:17 PM
       # PasswordAge         : 29
       # 
       # IMPORTANT:
       # This script requires a Windows 2003 or later domain.
       
       #  ****************************************************************
       #  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
       #  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.       *                     
       #  ****************************************************************
       #---------------------------------------------------------------------------------------
         
       Param ([string]$computername=$env:computername)
         
       #define some functions used in the script
       #get the age of the password in days
       #a value of 0 means just set or needs to be set
       #the value of $LastSet will be a large integer
       #indicating the number seconds since 1/1/1601
       #since the password was set
       
       Function Get-UTCAge {
           #get date time of the last password change
               Param([int64]$Last=0)
               if ($Last -eq 0) {
                   write 0
               } else {
                   #clock starts counting from 1/1/1601.
                   [datetime]$utc="1/1/1601"
                   #calculate the number of days based on the int64 number
                   $i=$Last/864000000000
                   
                   #Add the number of days to 1/1/1601
                   #and write the result to the pipeline
                   write ($utc.AddDays($i))
               }
           } # end Get-UTCAge function
       
       Function Get-PwdAge {
       
         Param([int64]$LastSet=0) 
         
         
           if ($LastSet -eq 0) {
               write "0"
           } else {
               #get the date the password was last changed
               [datetime]$ChangeDate=Get-UTCAge $LastSet
               
               #get the current date and time
               [datetime]$RightNow=Get-Date
               
               #write the difference in days
               write $RightNow.Subtract($ChangeDate).Days
           }
       } #end Get-PwdAge function
               
       #main code
       #define some constants
       
       #define our searcher object
       $Searcher = New-Object DirectoryServices.DirectorySearcher
       
       $filter="(&(objectCategory=computer)(cn=$computername))"
       $searcher.filter=$filter
       
       $searcher.findall() | ForEach-Object {
       
           #create a custom object for the account and password properties
           $obj=New-Object PSObject
           
           #add properties to the object
           $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $_.properties.item("name")[0]
           $obj | Add-Member -MemberType NoteProperty -Name "DN" -Value $_.properties.item("distinguishedname")[0]
           $obj | Add-Member -MemberType NoteProperty -Name "Description" -Value $_.properties.item("description")[0]
           $obj | Add-Member -MemberType NoteProperty -Name "AccountCreated" -Value $_.properties.item("whencreated")[0]
           $obj | Add-Member -MemberType NoteProperty -Name "AccountModified" -Value $_.properties.item("WhenChanged")[0]
           $obj | Add-Member -MemberType NoteProperty -Name "LastLogon" -Value (Get-UTCAge $_.properties.item("lastlogon")[0])
           $obj | Add-Member -MemberType NoteProperty -Name "PasswordLastChanged" -Value (Get-UTCAge $_.properties.item("pwdlastset")[0])
           $obj | Add-Member -MemberType NoteProperty -Name "PasswordAge" -Value (Get-PwdAge $_.properties.item("pwdlastset")[0])
       
           #write object to the pipeline
           write $obj
           
        } #end foreach
        
       #end of script
       


      It writes a custom object to the pipeline so you should be able to do:

      get-content c:\list.txt | foreach { c:\scripts\get-computeraccount $_  | Select name,lastLogon} | out-file $file


      Jeffery Hicks
      Windows PowerShell MVP
      SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

      Follow Me: http://www.twitter.com/JeffHicks
       
      #3
        marcussmith

        • Total Posts : 7
        • Scores: 0
        • Reward points : 0
        • Joined: 1/30/2006
        • Status: offline
        RE: Retrieve the Computer lastlogintimestamp? Tuesday, October 21, 2008 1:54 AM (permalink)
        0
        Hi Sapien,

        Thanks for you reply I achieved my result the easy way in the end.  I installed the QuestAD extras and ran the below.


        $PingMachines = Gc "C:\CompList.txt"

        ForEach($MachineName In $PingMachines)

               {
               
             Get-QADComputer $MachineName -IncludeAllProperties | format-table name, lastlogon | out-file c:\Results.txt -append

            }

        Thanks for your help.
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9