none
Search for active computers with last 90 days RRS feed

  • Question

  • Hi everyone,

    I would like to search entire domain for XP and Win 7 Pro computers that active within last 90 days.  I have script below but it appear to return all computers back to 2010.  What is wrong with this script?  Thanks in advance.

    $DaysInactive = 90 
    $time = (Get-Date).Adddays(-($DaysInactive))
     
    # Get all AD computers with lastLogonTimestamp less than our time
    Get-ADComputer -Filter {LastLogonTimeStamp -gt $time -and OperatingSystem -like "Windows XP Professional" -or OperatingSystem -like "Windows 7 Professional"} -Properties LastLogonTimeStamp, OperatingSystem, description |
     
    # Output hostname and lastLogonTimestamp into CSV
    select-object Name,OperatingSystem,description,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv 'C:\WinXP_Pro_Active_Computer.csv' -notypeinformation

    • Edited by phathuynh27 Sunday, June 29, 2014 1:40 PM Update to more readeable script
    Sunday, June 29, 2014 1:05 PM

Answers

  • Hi,

    This works just fine for me:

    $cutoff = (Get-Date).AddDays(-90)
    
    $filter = "LastLogonDate -gt '$cutoff' -and (OperatingSystem -eq 'Windows 7 Professional' -or OperatingSystem -eq 'Windows XP Professional')"
    
    Get-ADComputer -Filter $filter -Properties OperatingSystem,Description,LastLogonDate |
        Select Name,OperatingSystem,Description,LastLogonDate |
            Sort Name |
                Export-Csv .\CurrentMachines.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Marked as answer by phathuynh27 Monday, June 30, 2014 5:19 AM
    Sunday, June 29, 2014 7:39 PM
  • Try to clarify this mess:

    Here are all computers active within the last 30 days.

    PS C:\scripts> $time=[datetime]::Today.AddDays(-30)
    PS C:\scripts> Get-ADComputer -Filter {LastLogonDate -gt $time } -prop *|select name,LastLogonDate |ft -auto
    
    name  LastLogonDate
    ----  -------------
    WS703 6/19/2014 6:53:51 PM
    SBS01 6/21/2014 1:30:20 AM
    WS701 6/22/2014 10:34:06 AM
    WS702 6/28/2014 7:17:07 PM
    

    Note we have to use LogonDate because timestamp is not a date.

    If you want inactive then you need to use "-lt" instead of "-gt".

    You first need to be sure of what you mean when you save active or inactive.  A computer that is turned on is active.  If it has been turned on once in the interval it will be seen as active.

    I actually missed that in the first pass.


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, June 29, 2014 8:00 PM
    • Marked as answer by phathuynh27 Monday, June 30, 2014 5:19 AM
    Sunday, June 29, 2014 7:58 PM

All replies

  • Please start by formatting it correctly and posting script using the code block.  Posting colorized script causes it to break when displayed in a forum.

    What errors are you getting?


    ¯\_(ツ)_/¯

    Sunday, June 29, 2014 1:15 PM
  • sorry, never used code block before.  I got no errors, the output displays all active computers dating back to year 2010.  I just want computers that are active within last 90 days. 
    Sunday, June 29, 2014 1:37 PM
  • LastLogonTimeStamp -gt $time

    That is what you asked for in the script. YOu want to look for dates that are older than the time you are asking about not newer.


    ¯\_(ツ)_/¯

    Sunday, June 29, 2014 2:41 PM
  • Thank you. How do I modify my script above to only display active XP and Win 7 Pro computers within last 90 days?  Thanks in advance.
    Sunday, June 29, 2014 2:44 PM
  • It could also help if you formatted the script so it is more readable:

    $DaysInactive = 90
    $time=[datetime]::Today.AddDays(-$DaysInactive)
     
    Get-ADComputer -Filter {
              LastLogonDate -lt $time 
              -and OperatingSystem -like "Windows XP Professional" 
              -or OperatingSystem -like "Windows 7 Professional"
         } -Properties LastLogonTimeStamp, OperatingSystem, description |
         Select-Object Name,
                                  OperatingSystem,
                                  Description,
                                 @{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} |
         Export-Csv 'C:\WinXP_Pro_Active_Computer.csv' -notypeinformation


    ¯\_(ツ)_/¯




    • Edited by jrv Sunday, June 29, 2014 7:59 PM
    Sunday, June 29, 2014 2:46 PM
  • The provided script showed both active and inactive computers back from 2006 to date.  I just need to show ACTIVE computers within last 3 months. How do I show only computers that authenticate to the domain within last 3 months?  Thanks in advance.
    Sunday, June 29, 2014 6:28 PM
  • Hi,

    This works just fine for me:

    $cutoff = (Get-Date).AddDays(-90)
    
    $filter = "LastLogonDate -gt '$cutoff' -and (OperatingSystem -eq 'Windows 7 Professional' -or OperatingSystem -eq 'Windows XP Professional')"
    
    Get-ADComputer -Filter $filter -Properties OperatingSystem,Description,LastLogonDate |
        Select Name,OperatingSystem,Description,LastLogonDate |
            Sort Name |
                Export-Csv .\CurrentMachines.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Marked as answer by phathuynh27 Monday, June 30, 2014 5:19 AM
    Sunday, June 29, 2014 7:39 PM
  • Try to clarify this mess:

    Here are all computers active within the last 30 days.

    PS C:\scripts> $time=[datetime]::Today.AddDays(-30)
    PS C:\scripts> Get-ADComputer -Filter {LastLogonDate -gt $time } -prop *|select name,LastLogonDate |ft -auto
    
    name  LastLogonDate
    ----  -------------
    WS703 6/19/2014 6:53:51 PM
    SBS01 6/21/2014 1:30:20 AM
    WS701 6/22/2014 10:34:06 AM
    WS702 6/28/2014 7:17:07 PM
    

    Note we have to use LogonDate because timestamp is not a date.

    If you want inactive then you need to use "-lt" instead of "-gt".

    You first need to be sure of what you mean when you save active or inactive.  A computer that is turned on is active.  If it has been turned on once in the interval it will be seen as active.

    I actually missed that in the first pass.


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, June 29, 2014 8:00 PM
    • Marked as answer by phathuynh27 Monday, June 30, 2014 5:19 AM
    Sunday, June 29, 2014 7:58 PM
  • Thanks everyone for your responses.  It worked now :).  Thanks.
    Monday, June 30, 2014 5:18 AM
  • Monday, June 30, 2014 5:45 AM