locked
AccountExpires Conversion RRS feed

  • Question

  • I am new to PowerShell and have attempted converting the accountExpires date. However, when it encounters an accountExpires that is set to 'Never', the report displays 12/31/1600 5:00:00 PM.  I would really like it to display 'Never'.  Any help with this is appreciated.  This is what I have so far:

    $StuDate = ((Get-Date).AddDays(-9)).Date 
    
    
    Get-ADUser -Filter {(sAMAccountName -like "stu_*") -and (whenCreated -gt $StuDate)} -Properties * |
    	Select-Object sn,givenName,sAMAccountName,AccountExpirationDate,manager,department,title,description,employeeType,extensionAttribute15,@{N='departmentNumber';E=
    
    	{$_.departmentNumber[0]}} |
    	Sort-Object employeeType,extensionAttribute1




    • Edited by Arasnap Thursday, February 1, 2018 5:28 PM
    Thursday, February 1, 2018 3:41 PM

Answers

  • Yes.  Accounts that have been set to expire and then changed will show that.

    @{n='ExpireDate';n=(if($_.AccountExpirationDate){$_.AccountExpirationDate}else{'NEVER'}}}

    on WS2003 and earlier this may not work.

    You can also do this:

    Search-ADAccount -AccountExpiring -TimeSpan 9.00:00:00 |
       
    Get-ADUser -Properties  givenName,sn,userprincipalname,accountExpirationdate,mail,title,department,manager


    \_(ツ)_/

    • Marked as answer by Arasnap Thursday, February 1, 2018 8:18 PM
    Thursday, February 1, 2018 8:00 PM

All replies

  • That is correct.  A null date will show as that date.  You need to filter out all accounts set to not expire.

    When posting code please format and post correctly.  What you have posted is unreadable.


    \_(ツ)_/


    • Edited by jrv Thursday, February 1, 2018 3:48 PM
    Thursday, February 1, 2018 3:47 PM
  • The report is correct in that it contains all student accounts created within a specific timeframe.  The only problem with the report is the way an accountExpires set to 'Never' displays.
    Thursday, February 1, 2018 4:14 PM
  • Please fix you post so that we can actually read it.

    The null dates will show as described.  Either filter tem out or change them as needed.

    We cannot fix code you have copied from the Internet.


    \_(ツ)_/

    Thursday, February 1, 2018 4:25 PM
  • I've been working on this report for a couple days. I've tried many variations on it and have completely failed at getting the accountExpires to display 'Never'. It's the only reason that I'm attempting to receive help.  Since I am new to PowerShell, you have to understand that you telling me to 'format' something doesn't make much sense to me.  What I posted originally was broken by the pipes.  I'm not sure what you need, but I broke down each attribute to a separate line if that helps.
    • Edited by Arasnap Thursday, February 1, 2018 4:40 PM
    Thursday, February 1, 2018 4:40 PM
  • Why is this so hard.  The forum and nearly all technical forums have code posting tools to format and syntax highlight the code.  Just use the code posting tool.  Not posting correctly makes the code unreadable and it cannot be copied and pasted without errors.  Copy and paste the code the way you have posted it to see what I mean.

    In your code just check the date for zero and display NEVER when it is zero.

    Also use AccountExpirationDate and not the AD attribute.  PowerSHell/Net convert that for you and handle blank dates correctly.

    Replace this:

    @{Name='accountExpires';Expression={[DateTime]::FromFileTime($_.accountExpires)}},

    With: AccountExpirationDate


    \_(ツ)_/

    • Proposed as answer by jrv Thursday, February 1, 2018 5:38 PM
    Thursday, February 1, 2018 4:56 PM
  • Directing me to the little 'Insert Code Block' icon at the top of the post would have been helpful.  It's not hard, I'm just unfamiliar with the process. I've tried to include an If statement and assign the output to a variable, but I don't know the syntax to get it to display. 

    • Edited by Arasnap Thursday, February 1, 2018 5:35 PM
    Thursday, February 1, 2018 5:29 PM
  • Sorry for my omission.  It is not the HTML ICON.  You want the code icon <>

    On the Internet icons come with "hover" text called tool tips.  This is how you discover the purpose for the icons.

    Do not post HTML.  Post code with the code posting tool.

    Whenever you use any forum you need to first look at the provided posting tools to understand how to use the forum.  It is just like using Facebook or Twitter.  Tools for doing common things.


    \_(ツ)_/


    • Edited by jrv Thursday, February 1, 2018 5:37 PM
    Thursday, February 1, 2018 5:36 PM
  • I'm one of the few that does not use Facebook or Twitter, but that's aside from the point.  I've tried to do as you suggested "In your code just check the date for zero and display NEVER when it is zero." and am unable to come up with the syntax that will display the output in the report.  Again, I'm a total newb.  I don't know the syntax and can't figure it out which is why I posted in the first place.
    Thursday, February 1, 2018 7:14 PM
  • This is what I've tried, which still displays 12/31/1600 5:00:00 PM within the accountExpires column of the report:

    $StuDate = ((Get-Date).AddDays(-9)).Date 
    
    $AE = $_.accountExpires
    
    
    	If(($AE -like "*/1600*") -or ($AE -eq 0) -or ($AE -eq "Never") -or ($AE -gt [DateTime]::MaxValue.Ticks))
    	{
    		$AcctExpires = "<Never>"
    	}
    	Else
    	{
    		$AcctExpires = $AE
    	}
    
    Get-ADUser -Filter {(sAMAccountName -like "stu_*") -and (whenCreated -gt $StuDate)} -Properties * |
    	Select-Object sn,givenName,sAMAccountName,$_.AcctExpires,manager,department,title,description,employeeType,extensionAttribute15,@{N='departmentNumber';E={$_.departmentNumber[0]}} |
    	Sort-Object employeeType,extensionAttribute15,departmentNumber,sn,givenName | export-csv "H:\PowerShell\NewStudents.csv"

    I tried this a couple ways - one using your suggestion of using AccountExpirationDate ($AE = AccountExpirationDate) and the other (posted in this reply) using accountExpires.

    Thursday, February 1, 2018 7:31 PM
  • I just changed the ($AE -like "*/1600*") to ($AE -like "12/31/1600*") and now the report does not contain 12/31/1600 5:00:00 PM.  It is blank.  

    This is better than displaying the date, but I'm curious if you can tell me what is wrong with my code that it would not display 'Never'?


    • Edited by Arasnap Thursday, February 1, 2018 7:42 PM
    Thursday, February 1, 2018 7:41 PM
  • If the date I blank the account never expires.  That is why I asked you to use that property.

    Blank means null.  Only you original version return a 0 or a very high value and I of minimal use, 


    \_(ツ)_/

    Thursday, February 1, 2018 7:47 PM
  • This is what I've tried, which still displays 12/31/1600 5:00:00 PM within the accountExpires column of the report:

    $StuDate = ((Get-Date).AddDays(-9)).Date 
    
    $AE = $_.accountExpires
    
    
    	If(($AE -like "*/1600*") -or ($AE -eq 0) -or ($AE -eq "Never") -or ($AE -gt [DateTime]::MaxValue.Ticks))
    	{
    		$AcctExpires = "<Never>"
    	}
    	Else
    	{
    		$AcctExpires = $AE
    	}
    
    Get-ADUser -Filter {(sAMAccountName -like "stu_*") -and (whenCreated -gt $StuDate)} -Properties * |
    	Select-Object sn,givenName,sAMAccountName,$_.AcctExpires,manager,department,title,description,employeeType,extensionAttribute15,@{N='departmentNumber';E={$_.departmentNumber[0]}} |
    	Sort-Object employeeType,extensionAttribute15,departmentNumber,sn,givenName | export-csv "H:\PowerShell\NewStudents.csv"

    I tried this a couple ways - one using your suggestion of using AccountExpirationDate ($AE = AccountExpirationDate) and the other (posted in this reply) using accountExpires.

    This is unnecessary.  If the date is blank (null) then the account never expires.

    \_(ツ)_/

    Thursday, February 1, 2018 7:49 PM
  • The property you directed me to use still returned 12/31/1600 5:00:00 PM.  Telling me that displaying "Never" is unnecessary doesn't actually answer the question of why it won't do it.

    Just for clarification purposes, the report is not an accountExpires report.  It is a report of all student accounts that were recently created.  The report needs to include the accountExpires value because it is verified by teams other than the one that created the account to ensure a value exists.  I can live with the blank value that is currently displayed, but only wanted to know why I could not get it to say 'Never' if that is what accountExpires is set to. 

    • Edited by Arasnap Thursday, February 1, 2018 7:55 PM
    Thursday, February 1, 2018 7:52 PM
  • Yes.  Accounts that have been set to expire and then changed will show that.

    @{n='ExpireDate';n=(if($_.AccountExpirationDate){$_.AccountExpirationDate}else{'NEVER'}}}

    on WS2003 and earlier this may not work.

    You can also do this:

    Search-ADAccount -AccountExpiring -TimeSpan 9.00:00:00 |
       
    Get-ADUser -Properties  givenName,sn,userprincipalname,accountExpirationdate,mail,title,department,manager


    \_(ツ)_/

    • Marked as answer by Arasnap Thursday, February 1, 2018 8:18 PM
    Thursday, February 1, 2018 8:00 PM
  • It did work with just a small modification to what you have posted: 

    @{n='accountExpires';e={if($_.AccountExpirationDate){$_.AccountExpirationDate}else{"Never"}}}

    Thank you!

    Thursday, February 1, 2018 8:17 PM
  • Why the double quotes.  They add nothing useful.  Do not get into the habit of using double quotes everywhere.  In PowerShell they have a special meaning.

    I prefer "ExpireDate" but you can have anything you want in that field.


    \_(ツ)_/


    • Edited by jrv Thursday, February 1, 2018 8:27 PM
    Thursday, February 1, 2018 8:26 PM
  • I'll change the double quotes to single quotes.  I know that n is just the column heading.  I can change that depending on what the teams want to see.  Thanks again for the help.
    Thursday, February 1, 2018 8:32 PM
  • The quote won't hurt here but it is a good idea to not get in the habit.  Use double quote only when you want that functionality.  It's safer.

    How about making the header "Account Self Destructs On"


    \_(ツ)_/

    Thursday, February 1, 2018 9:56 PM