AccountExpires n Get-AdComputert

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Wes Stahler Wes Stahler 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #40068
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    I am trying to convert the accountexpires property to something that is a date. I thought it might be the same as { [datetime]::fromFileTime($_.lastLogonTimestamp) } but it looks like this is not the case.

    can anyone please advise? Thank you. in advance

    $ADComputerProperties = @(# Hashtable for computer properties
    	"Name",
    	"SAMAccountName",
    	"DNSHostName",
    	"PasswordLastSet",
    	"Enabled",
    	"whenCreated",
    	"accountExpires",
    	"OperatingSystem",
    	"OperatingSystemServicePack",
    	"OperatingSystemVersion",
    	"UserAccountControl",
    	"LastLogonTimeStamp",
    	"DistinguishedName"
    )
    
    $ADComputerPropertiesShown = @(# Hashtable for select 
    	"Name",
    	"SAMAccountName",
    	"DNSHostName",
    	"PasswordLastSet",
    	"Enabled",
    	"whenCreated",
    	@{
    		name = 'accountExpires';
    		expression = { [datetime]::fromFileTime($_.accountExpires) }
    	},
    	"accountExpires",
    	"OperatingSystem",
    	"OperatingSystemServicePack",
    	"OperatingSystemVersion",
    	"UserAccountControl" 
    	@{
    		name = 'lastLogonTimestamp'; 
    		expression = { [datetime]::fromFileTime($_.lastLogonTimestamp) }
    	},
    	"DistinguishedName"
    )
    
    Get-ADComputer -Filter { OperatingSystem -NotLike "*server*" } -Properties $ADComputerProperties | select $ADComputerPropertiesShown | Export-Csv "c:\temp\Justice_Desktop_Report-$filenamebit.csv"
    
    
    #40073
    Profile photo of thom schumacher
    thom schumacher
    Participant

    If I perform this action on my local desktop:

    (get-adcomputer $env:computername -Properties accountexpires).accountexpires | gm

    ((get-adcomputer $env:computername -Properties accountexpires).accountexpires).gettype()

    I get a type of: TypeName: System.Int64

    I wonder if this int value is a tick:

    Looks like it is:
    C:\Windows\System32\WindowsPowerShell\v1.0> (get-date).ticks
    635997201401143677
    C:\Windows\System32\WindowsPowerShell\v1.0> (get-date).ticks.gettype()

    IsPublic IsSerial Name BaseType
    ——– ——– —- ——–
    True True Int64 System.ValueType

    $a = ((get-adcomputer $env:computername -Properties accountexpires).accountexpires)

    New-Object System.DateTime $a

    #40156
    Profile photo of Wes Stahler
    Wes Stahler
    Participant

    Unless the expiration date is explicitly set, I don't believe you will get any results. I successfully ran your script without issue for a test server where I explicitly set the AccountExpirationDate.

    Get-ADComputer TestServer-vt01 | Set-ADAccountExpiration -DateTime '01/01/2020' -WhatIf

    You could also just use AccountExpirationDate in your return set.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.