Author Posts

October 19, 2017 at 5:15 pm

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

October 19, 2017 at 6:16 pm

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

October 20, 2017 at 2:17 pm

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}"

October 20, 2017 at 3:54 pm

This works for me:

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

October 20, 2017 at 5:00 pm

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?

October 20, 2017 at 5:07 pm

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

October 20, 2017 at 6:12 pm

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

October 20, 2017 at 6:39 pm

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.

October 20, 2017 at 9:48 pm

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

Cheers!