Powershell: Password report

Parity123
Parity123 used Ask the Experts™
on
Hello,

I have the following code to get the password expiration report. I need assistance in modifying the code to get just the total counts of users password expiring on a specific date. For instance: 01/01/2016

Import-Module ActiveDirectory
Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and physicalDeliveryOfficeName -like "*" } `
–Properties "SamAccountName","mail","pwdLastSet","physicaldeliveryofficename","msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property "SamAccountName","mail","physicaldeliveryofficename",@{Name="Password Last Set";`
Expression={[datetime]::FromFileTime($_."pwdLastSet")}}, @{Name="Password Expiry Date";`
Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} |
 Export-CSV "C:\PasswordExpirationReport.csv" -NoTypeInformation -Encoding UTF8
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
So you're going to specify the date when calling the function, and it should in turn return the number of days before the password expires based on the date you specify?

Author

Commented:
I can specify the date with a variable in the code.
$mydate="01/01/2016"

and I want the total count of users password expiring on this date.

Commented:
should that include all passwords that have already expired or just passwords expiring on that exact date?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Author

Commented:
just on that exact date.

Commented:
This is what I have so far.  It's late and I'm beat so I'll pick it up again tomorrow.  Feel free to modify as needed, but honestly I feel like I'm off track on this one:

[datetime]$date = Read-Host 'Enter date'
$expiry = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties 'DisplayName', 'msDS-UserPasswordExpiryTimeComputed' | Select-Object -Property 'DisplayName',@{n='ExpirationDate';e={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}}

foreach ($i in $expiry) {
    if ($i.ExpirationDate -eq $date) {
        Write-Host $i
    }
}

Open in new window

Author

Commented:
Thanks Jason. I tried.

For instance: The value for $i.ExpirationDate is 1/5/2016 9:50:54 AM.

I set the variable $mydate="1/5/2016" just to try

and I changed the code to if ($i.ExpirationDate -contains $mydate) and it does not return anything.

Commented:
Yea I tried to be as upfront as I could that it was an untested script.  I just wanted to put something out there we can use to build on.  I have three Exchange migrations I'm working on at the moment and I just couldn't look at my computer any longer last night.  I'll edit the script and have an updated version to you by the end of the day.
Commented:
Actually that looks pretty good minus a formatting change in the output.  If you modify the operator in the if statement to -ne it returns results.  Try using a known password expiration date

Import-Module activedirectory
[datetime]$date = Read-Host 'Enter date'
$expiry = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties 'DisplayName', 'msDS-UserPasswordExpiryTimeComputed' | Select-Object -Property 'DisplayName',@{n='ExpirationDate';e={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}}

foreach ($i in $expiry) {
    if ($i.ExpirationDate -eq $date) {
        Write-Host 'The password for' $i.DisplayName 'will expire on' $i.ExpirationDate
    }
}

Open in new window

Author

Commented:
I tried and does not return any values. some if the values are

12/22/2015 10:35:14 AM
1/9/2016 4:30:10 PM
12/26/2015 8:46:00 AM
2/7/2016 1:31:40 PM
2/5/2016 10:11:07 AM
12/26/2015 12:08:11 AM

I think the issue is comparison. I even hardcoded the date to $mydate="2/5/2016", and used -contains $mydate and does not return values.

Commented:
That's odd we're seeing different results.  I'll keep working on it today.  What PowerShell version are you using?  Just run this command to check:

$PSVersionTable.PSVersion

Author

Commented:
V4.0

Author

Commented:
Can someone please assist. Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial