4
votes

I have the following piece of code that works fine to output the user's displayname and the accountExpires attribute from AD. I am running them on commandline because of restricted mode at work:

$objSearch.findall() | %{" " + $_.properties.displayname + "| " + $_.properties.accountexpires}

Now I need to convert this output, specifically the accountExpires attribute to a humanly readable date. After googling I figured that I can use something like the below to convert between the accountExpires and a datetime.

[datetime]::fromfiletime(129138320987173880)

But I am having issues combining the two. I tried the following:

$objSearch.findall() | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime($_.properties.accountexpires)}

Cannot convert argument "0", with value: "System.DirectoryServices.ResultPropertyValueCollection", for "FromFileTime" to type "System.Int64": "Cannot convert the "System.DirectoryServices.ResultPropertyValueCollection" value of type "System.DirectoryServices.ResultPropertyValueCollection" to type "System.Int64"." At line:1 char:96 + $objSearch.findall() | %{" "+ $.properties.displayname + " " + [datetime]::fromfiletime <<<< ($.properties.accountexpires)} + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

$objSearch.findall() | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime $_.properties.accountexpires}

Unexpected token '' in expression or statement. At line:1 char:99 + $objSearch.findall() | %{"This is "+ $.properties.displayname + " " + [datetime]::fromfiletime $_ <<<< .properties.accountexpires} + CategoryInfo : ParserError: (_:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

How can I convert the accountExpires to a humanly readable date?

1

1 Answers

3
votes

You just miss that the underlying ADSI COM object presents properties as arrays here is a way to get the accountexpires property, just use $_.properties.accountexpires[0].

$search = [ADSISearcher]"OU=MonOu,DC=dom,DC=fr"
$search.Filter = "(cn=Jean Paul Blanc)"
$user = $search.FindOne()
$user | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime($_.properties.accountexpires[0])}

This give for me :

 Jean Paul Blanc 12/07/2012 00:00:00