Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4849
  • Last Modified:

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
0
freymish
Asked:
freymish
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
QlemoC++ DeveloperCommented:
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
 
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
 
QlemoC++ DeveloperCommented:
Yes, the "never expires" might have caused the issue.
0
Technology Partners: 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!

 
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
 
QlemoC++ DeveloperCommented:
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

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now