Link to home
Start Free TrialLog in
Avatar of freymish
freymishFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, the "never expires" might have caused the issue.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of freymish

ASKER

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.)
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.
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.
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.