none
SearchBase in multiple OUs

    Question

  • Hello I've problem with getting output to csv. I have output to PS. please help

    import-module activedirectory  
    $domain = "consto.com"  
    $DaysInactive = 30  
    $time = (Get-Date).Adddays(-($DaysInactive))
     
    $OUs = "OU=Servers,OU=location1,DC=consto,DC=com","OU=Servers,OU=location2,DC=consto,DC=com"
    ForEach($OU in $OUs) {Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp -SearchBase ($OU)}
     
    select-object Name,@{Name="Stamp";Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv G:\ISO\OLD_Computer.csv -notypeinformation


    • Edited by Konrad G Thursday, May 17, 2018 9:15 PM
    Thursday, May 17, 2018 9:15 PM

All replies

  • Your Select-Object does not get any input ...

    This should work actually:

    $domain = "consto.com"  
    $DaysInactive = -30  
    $time = (Get-Date).Adddays($DaysInactive)
     
    $OUs = "OU=Servers,OU=location1,DC=consto,DC=com","OU=Servers,OU=location2,DC=consto,DC=com"
    $Result = ForEach($OU in $OUs){
        Get-ADComputer -Filter "LastLogonTimeStamp -lt $time" -Properties LastLogonTimeStamp -SearchBase $OU |
            Select-Object -Property Name,@{Name="Stamp";Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}
    } 
    $Result
    $Result | Export-Csv -Path G:\ISO\OLD_Computer.csv -NoTypeInformation


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Thursday, May 17, 2018 9:36 PM
  • Thank you,

    works as should

    Friday, May 18, 2018 2:39 PM