DateTime class Parse method 'oddity'

Can you please tell me why one of these lines of code works and the other does not?

I am passing an ADUser object with LastLogonTimeStamp property added, to the DateTime Parse method. If I pass it directly, if errors, saying the 'input string was not in correct format'. If I use a ForEach 'loop' (even though in this test case, I am only passing one object not a collection of them--but I will be doing that), the method performs as expected.

In both cases I'm passing the same object, with same property value referenced. Works:

get-aduser user001 -property lastlogontimestamp | foreach {Get-Date Date ([DateTime]::FromFileTime([Int64]::Parse($_.LastLogonTimeStamp)))}

Sunday, August 16, 2015 8:01:27 PM

Fails:

get-aduser 9aubyt001 -property lastlogontimestamp | Get-Date Date ([DateTime]::FromFileTime([Int64]::Parse($_.LastLogonTimeStamp)))

Exception calling "Parse" with "1" argument(s): "Input string was not in a correct format."At line:1 char:1
Thanks.

August 24th, 2015 10:13am

Can you please tell me why one of these lines of code works and the other does not?

I am passing an ADUser object with LastLogonTimeStamp property added, to the DateTime Parse method. If I pass it directly, if errors, saying the 'input string was not in correct format'. If I use a ForEach 'loop' (even though in this test case, I am only passing one object not a collection of them--but I will be doing that), the method performs as expected.

In both cases I'm passing the same object, with same property value referenced. Works:

get-aduser user001 -property lastlogontimestamp | foreach {Get-Date Date ([DateTime]::FromFileTime([Int64]::Parse($_.LastLogonTimeStamp)))}

Sunday, August 16, 2015 8:01:27 PM

Fails:

get-aduser 9aubyt001 -property lastlogontimestamp | Get-Date Date ([DateTime]::FromFileTime([Int64]::Parse($_.LastLogonTimeStamp)))

Exception calling "Parse" with "1" argument(s): "Input string was not in a correct format."At line:1 char:1
Thanks.

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 10:27am

It's because get-date can't accept what is being passed to it from the pipeline. Inside of the foreach block in the first example it is no longer being directly passed to the get-date commandlet, rather it is processing the received information inside of it's own code block via the foreach (which does accept the pipeline) so that it can properly handle it.
August 24th, 2015 10:30am

We normally don't use that property because AD gives us the converted property..

Get-AdUser user001 -property LastLogonDate | select LastLogonDate

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 10:50am

It's because get-date can't accept what is being passed to it from the pipeline. Inside of the foreach block in the first example it is no longer being directly passed to the get-date commandlet, rather it is processing the received information inside of it's own code block via the foreach (which does accept the pipeline) so that it can properly handle it.
  • Edited by Noah Sparks Monday, August 24, 2015 2:30 PM
August 24th, 2015 2:28pm

Thanks jrv (Danny and Noah). I thought, dang, I keep asking questions about outdated methods, props, etc.. So I looked up LastLogonDate and sure enough. By the way, the converted property isn't a value AD provides, but Powershell, which is what I think you meant to say.

"Powershell was nice enough to give us a third option to query by.  LastLogonDate is a converted version of LastLogontimestamp.  Its not a [AD] replicated attribute.  Instead, its a locally calculated value of the replicated value.  Most importantly, it gives us the ability to query using human friendly date formats!!"

http://social.technet.microsoft.com/wiki/contents/articles/22461.understanding-the-ad-account-attributes-lastlogon-lastlogontimestamp-and-lastlogondate.aspx

Gotta love the goog!

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 8:17pm

Well I thought I could get away with it.  Too lazy to change what I typed.  Good call for catching.

Yes- PowerShell adds to most things in useful

August 24th, 2015 8:51pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics