Powershell script to find password expiration date

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
LVL 4
freymishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Yes, the "never expires" might have caused the issue.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

footechCommented:
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
freymishAuthor Commented:
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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
footechCommented:
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
freymishAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.