Formatting output of lastlogondate

This topic contains 8 replies, has 4 voices, and was last updated by  Matt Howard 4 weeks ago.

  • Author
    Posts
  • #82523

    Edwards Michael J
    Participant

    Hi –

    I'm running an inventory of computer objects in several child OU's. I want to break the computers in each OU down by OS type and record the last time any computer of that OS type logged in within each OU. I'm using the "lastlogondate" attribute of the compauter, and I get a result. The problem is I'm getting the data back in the format of "@{lastlogondate=3/27/2017}". How do I remove the "@{lastlogondate=}" part and leave just the date. Here's the relevant code:

    $ous = Get-ADOrganizationalUnit -Filter {name -like "*"} -SearchBase "dc=contoso,dc=com" -SearchScope OneLevel
    Foreach($ou in $ous){
    $llts2008 = Get-ADComputer -filter {OperatingSystem -Like '*2008*'} -SearchBase $dn -SearchScope Subtree -Properties lastlogondate | select-object -property lastlogondate | sort lastlogondate -descending | select-object -first 1
    $llts2012 = Get-ADComputer -filter {OperatingSystem -Like '*2012*'} -SearchBase $dn -SearchScope Subtree -Properties lastlogondate | select-object -property lastlogondate | sort lastlogondate -descending | select-object -first 1
    $results = [pscustomobject][ordered]@{"OU" = $ouname";"Last Win2008 Computer Logon" = $llts2008;"Last Win2012 Computer Logon" = $llts2012 | Export-Csv -Path C:\DelegatedOUStats.csv -Append -NoTypeInformation
    

    I actually thought of strongly typing the $llts* variable as a string and then using trimstart() and trimend(), but that shouldn't be necessary.

    Any suggestions would be appreciated.

    Thanks

  • #82531

    Matt Howard
    Participant

    Change your first Select-Object to use -ExpandProperty instead of -Property and drop the property from your sort, just use Sort-Object -Descending.

  • #82627

    Edwards Michael J
    Participant

    Thanks, Matt – I tried your suggestion, but it still resulted in the output not being formatted properly. Here's the code I used:

    $llts2012 = Get-ADComputer -filter { OperatingSystem -Like '*2012*' } -SearchBase $dn -SearchScope Subtree -Properties lastlogondate | Select-Object -ExpandProperty lastlogondate | Sort-Object -Descending | Select-Object -First 1
    

    The output looked like this: "@{lastlogondate=10/15/2017 13:52:09}"

  • #82645

    Rick
    Participant

    This works for me:

    get-adcomputer -filter { OperatingSystem -Like '*2012*' } -Properties lastlogondate | Select-Object name, lastlogondate | Sort-Object -Descending | Select-Object -First 1
    
  • #82652

    Matt Howard
    Participant

    What version of PS are you using? I do not believe that would have an impact, but just curious.

    Also, how are you setting $dn?

  • #82655

    Matt Howard
    Participant

    What does your output look like if you return $llts2012 or without assigning it to a variable?

    • #82669

      Edwards Michael J
      Participant

      I'm not sure what to tell you, Matt. I tried it the first time and the results were as I explained. I waited a while and tried it again and it works beautifully. Not sure what changed or why but obviously something did.

      Anyway, thanks for the assist.

    • #82681

      Matt Howard
      Participant

      Glad to hear it worked. I was starting to scratch my head and call shenanigans.

      Cheers!

  • #82658

    Greg Tate
    Participant

    I'm using version 5.1 and I can't reproduce the formatting error so maybe there is a version issue.
    That said I came up with an possible alternate way to get you to the same end result.

    In the example below, I've derived the OU from the Distinguished name using Select-Object and RegEx. For quicker debugging I've piped it out to gridview since I find it does a good job of giving excel like results. You can change this to Export-CSV if you want.

    If you have a large AD environment (more than 5000 computer objects), you may want to tweak the Get-ADComputer filter from * to something with less results.

    $Computers = Get-ADComputer -filter * -Properties OperatingSystem,LastLogOnDate
    [regex]$sub =  "(?< =,).*"
    $Computers | Select-Object Name,OPeratingSystem,lastlogondate,@{Name="OU";Expression = {$sub.Match($_.DistinguishedName)}} | Out-GridView
    

You must be logged in to reply to this topic.