Solved

Powershell script to find password expiration date

Posted on 2015-01-12
8
4,069 Views
Last Modified: 2015-01-15
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 225 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).
mport-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 81

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 225 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 70

Expert Comment

by:Qlemo
ID: 40545744
Yes, the "never expires" might have caused the issue.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 40

Assisted Solution

by:footech
footech earned 50 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 70

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 40

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains the steps required to use the default Photos screensaver to display branding/corporate images
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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