Why won't the ToDateTime() method work?

Отвечено Why won't the ToDateTime() method work?

  • Thursday, January 24, 2013 9:43 PM
     
     

    Okay, I'm using 

    $var = ((Get-ADUser –Identity username -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed")

    to pull the time left before a password expires in AD.  This results in an Int64 value.  When I pipe it to gm, one of the methods is ToDateTime.  I should just be able to say

    $var2 = $var.ToDateTime()

    and end up with a DateTime, right?  It should just work the same way that ToUpper() works on a string, but what I get back is this:

    PS C:\users\sonny\Desktop> $var.todatetime()
    Cannot find an overload for "todatetime" and the argument count: "0".
    At line:1 char:1
    + $var.todatetime()
    + ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
     

    When I call it without the parenthesis, I get this:

    PS C:\users\sonny\Desktop> $var.todatetime

    OverloadDefinitions                                                                                                                          
    -------------------                                                                                                                          
    datetime IConvertible.ToDateTime(System.IFormatProvider provider)                                                                            

    Any ideas about where I'm going wrong here?

All Replies

  • Thursday, January 24, 2013 9:51 PM
    Moderator
     
      Has Code

    Hi,

    You can convert it to a DateTime thus:


    [DateTime]::ToFileTime($var)

    Bill

  • Thursday, January 24, 2013 10:11 PM
     
     

    This is what I got back.

    PS C:\users\sonny\Desktop> $var2 = [DateTime]::ToFileTime($var)
    Method invocation failed because [System.DateTime] doesn't contain a method named 'ToFileTime'.
    At line:1 char:1
    + $var2 = [DateTime]::ToFileTime($var)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

  • Thursday, January 24, 2013 10:14 PM
    Moderator
     
      Has Code

    Sorry; typo on my part. It should be


    [DateTime]::FromFileTime($var)

    Bill

  • Thursday, January 24, 2013 10:27 PM
     
     

    Now I get this:

    PS C:\users\sonny\Desktop> $var2 = [DateTime]::fromfileTime($var)
    Exception calling "FromFileTime" with "1" argument(s): "Not a valid Win32 FileTime.
    Parameter name: fileTime"
    At line:1 char:1
    + $var2 = [DateTime]::fromfileTime($var)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentOutOfRangeException

    Currently, the password is set not to expire.  Could that be the reason for the "not a valid win32 FileTime"?

    Thanks for your quick responses, by the way!  };-)

  • Thursday, January 24, 2013 10:40 PM
    Moderator
     
     

    What's in $var and what is its type?

    Bill

  • Thursday, January 24, 2013 11:06 PM
     
      Has Code

    The return attribute is a lar4ge integer.  It is not a datatime.  You need to convert it.

    Large integer

    2.5.5.16

    65

    A 64-bit number

    Here is the attribute:

    cn: ms-DS-User-Password-Expiry-Time-Computed
    ldapDisplayName: msDS-UserPasswordExpiryTimeComputed
    attributeId: 1.2.840.113556.1.4.1996
    attributeSyntax: 2.5.5.16
    omSyntax: 65
    isSingleValued: TRUE
    schemaIdGuid: add5cf10-7b09-4449-9ae6-2534148f8a72
    systemOnly: FALSE
    searchFlags: 0
    attributeSecurityGuid: 4c164200-20c0-11d0-a768-00aa006e0529
    systemFlags: FLAG_SCHEMA_BASE_OBJECT | FLAG_ATTR_IS_CONSTRUCTED
    schemaFlagsEx: FLAG_ATTR_IS_CRITICAL

    ¯\_(ツ)_/¯

  • Thursday, January 24, 2013 11:11 PM
    Moderator
     
     Answered Has Code

    Mine is returned as an Int64:


    PS C:\> $var = (Get-ADUser -Identity username -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"
    PS C:\> $var.GetType()
    
    IsPublic IsSerial Name  BaseType
    -------- -------- ----  --------
    True     True     Int64 System.ValueType
    
    PS C:\> [DateTime]::FromFileTime($var)
    
    Tuesday, February 19, 2013 9:18:14 AM
    

    Bill

  • Friday, January 25, 2013 1:40 PM
     
     
    I set a password to expire and then ran the [datetime]::fromfiletime($var) again.  I got back a date this time.  I think that running the Get-ADUser command on a password that doesn't expire yields a result that will not convert to a date.   Problem solved.
  • Friday, January 25, 2013 1:40 PM
     
     
    Thanks for the help!  }:-)