Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell script to find password expiration date

Posted on 2015-01-12
8
Medium Priority
?
4,690 Views
Last Modified: 2017-06-07
I created a script in the past to help keep some remote users, who don't log directly on to a company machine, on top of their password expirations.  The script would search through a specific OU in AD and find those users whose passwords were due to expire within X days and then send them an e-mail reminding them to change them.  This worked well until recently when MS released a newer version of PowerShell that has elements that appear to not be backwards compatible.
(The Foreach function comes to mind since I can no longer reference a specific dimension name to compare in an array.. that's another story though)

Anywho; the code below used to work and now it doesn't.  When I run this now I get an error that:

"Exception calling "FromFileTime" with "1" argument(s): "Not a valid Win32 FileTime.
Parameter name: fileTime"

The error comes on the line that sets the value for $Days and an example of the string it returns is "9223372036854775807"

import-module activedirectory
$msg = ""
$Users = Get-ADUser -SearchBase "OU=Users,OU=Outside,DC=MyCompany,DC=com" –filter {(Description -Like "description*")} -properties * | select msDS-UserPasswordExpiryTimeComputed,name, mail,samaccountname,GivenName,Surname
foreach ($user in $users)
{
    $Days =  (([datetime]::FromFileTime((get-ADUser -Identity $users.samaccountname -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed")) -(Get-Date)).Days
    $Recipient = $User.mail
    if ($days -lt 1)
    {
        $msg = ""
        $msg ="Your password will expire in $days days.  Please make arrangements to change it or have it reset.`r"
    }
}
 

Can anyone help me get this working again?

Thanks,

Jack
0
Comment
Question by:freymish
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 900 total points
ID: 40545723
There has been no change with PS releases, as this is ActiveDirectory and .NET only. So it should work still.

However, you are retrieving the AD user info twice, and hence should go with the following code.
And it is better to use the pipeline if the amount of objects to get is unknown and could be large.
Lastly, your code as shown only allows for sending to a single user (the last one found).
Import-module activedirectory
$msg = ""
Get-ADUser -SearchBase "OU=Users,OU=Outside,DC=MyCompany,DC=com" –filter {(Description -Like "description*")} -properties * |
  select msDS-UserPasswordExpiryTimeComputed,name, mail, samaccountname, GivenName, Surname | % {
    $Days =  ([datetime]::FromFileTime($_.{msDS-UserPasswordExpiryTimeComputed} - (Get-Date)).Days
    if ($days -lt 1)
    {
       Send-MailMessage -SmtpServer smtp.yourDomain.com -From You@yourDomain.com -To $_.Mail -Subject "Password Expiry Warning" -body "Your password will expire in $days days.  Please make arrangements to change it or have it reset.`r"
    }
  } 

Open in new window

0
 
LVL 84

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 900 total points
ID: 40545739
Generic
Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed"

|

Select-Object -Property "Displayname",@{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} 

Open in new window

Your code modified
$users= Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties DisplayName, msDS-UserPasswordExpiryTimeComputed, mail,samaccountname,GivenName,Surname 
foreach($user in $users){
$expirydate = [datetime]::FromFileTime($user.'msDS-UserPasswordExpiryTimeComputed')
$daysleft = ($expirydate - (Get-Date))
$days = $daysleft.days 
if ($days -le 1)
    {
        
        $msg = ""
        $msg ="Your password will expire in $days days.  Please make arrangements to change it or have it reset.`r"
        $user.SamAccountName
        $msg
        }
} 

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40545744
Yes, the "never expires" might have caused the issue.
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
LVL 41

Assisted Solution

by:footech
footech earned 200 total points
ID: 40546553
It does seem to be necessary to explicitly specify "msDS-UserPasswordExpiryTimeComputed" in the properties to be retrieved with Get-ADUser.  In my testing -properties * didn't work.  Interestingly (just to me), my own coding for this ended up somewhere between Qlemo's and David's, making use of a calculated property like in David's "generic" example.

Off-topic, I know, but I'm a bit curious about this reference.
The Foreach function comes to mind since I can no longer reference a specific dimension name to compare in an array...
What are you referring to?  I don't believe I've seen anything in newer versions of PS that isn't backwards compatible.
0
 
LVL 4

Author Closing Comment

by:freymish
ID: 40546958
Thanks all for the help.  I figured out a few things here.  It turns out that the users this query was returning all had their passwords set to not expire.  That did not used to be the case so I didn't bother to check until the "PasswordNeverExpires -eq $False" caused to code to return no results.  I think that was what was causing the "Not a valid Win32 FileTime. Parameter name: fileTime" error.


I didn't include everything in the script so I wasn't interested in the mail part working for all users returned, that part I can fix.

As for your question Footech..

I freely admit to knowing enough to be dangerous but not a lot else ;)

I had a number of scripts that I had written in the past that ran along these lines:

$foos = get-stuff -filter { description -eq "whatever"} -properties * | select first, last,  description #(assumes that these fields actually exist in the result set of course)
Foreach($foos.description in $foos)
    {
        Do-Something
    }

Scripts with syntax like this no longer run.  I have had to change them so that they don't reference a specific dimension within the array as in below:

Foreach($foo in $foos)

The $foo variable doesn't exist as a defined thing prior to this but PowerShell seems to know what I mean.  (I am left to conclude that it understands the difference between singular and plural in English.. however I suspect that's unlikely.)
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40547810
That Foreach($foos.description in $foos) does not result in a syntax error is a parsing bug in PS2 (just checked). It does not make any sense anyway - the foreach var needs to be a var, and no property. It has no effect, as this short sample shows:
foreach ($x.fullnamex in dir) { $x }

Open in new window

This just spits out the same as if you write (correctly)
foreach ($x in dir) { $x }

Open in new window

PS 4 will show a syntax error claiming the IN is missing - and that is the correct response.
0
 
LVL 41

Expert Comment

by:footech
ID: 40550943
Just wanted to explain that no, it doesn't understand the difference between singular and plural in English.  With the syntax of
foreach (A in B) { "do something" }
- B can be any collection, whether a variable or a command that produces a collection like in Qlemo's example (dir or Get-ChildItem or something more complex).  It could also be just a single item, but then using foreach is pretty meaningless.
- A can be any variable (the name is irrelevant).  If that variable already exists it will be overwritten, so best to choose a new name.
- Using a variable name that is plural for B, and singular for A, is just a convention that helps with understanding what's happening.
0
 
LVL 4

Author Comment

by:freymish
ID: 40551144
FTR, I didn't really believe the singular plural thing, just a bit of fun  ;)

My thinking, however erroneous, was that in referencing a specific property in the array I was in fact filtering the result set to contain only those records for which there was a value

So,
foreach ($x.fullnamex in dir) { $x }

Open in new window


would only perform it's operation on records that contained a $x.fullnamex value.

Anywho,  thanks for the education!  That clears up one of two annoying things with PowerShell that I've been wondering about. One of these days, when the question is there,  I'll get the answer for the other.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question