Solved

Query local user database for date password last changed

Posted on 2006-11-06
17
376 Views
Last Modified: 2008-02-01
I have a handy VB script that shows the date users last changed their passwords in AD.  I can't find anything similar for a standalone machine.

I found this bit of code on Technet (http://www.microsoft.com/technet/scriptcenter/resources/qanda/jul05/hey0705.mspx)

strComputer = "atl-ws-01"

Set objUser = GetObject("WinNT://" & strComputer & "/kenmyer")

intPasswordAge = objUser.PasswordAge
intPasswordAge = intPasswordAge * -1
dtmChangeDate = DateAdd("s", intPasswordAge, Now)

WScript.Echo "Password last changed: " & dtmChangeDate

I'm completely not a VB guy and I'm posting it here because I'm hoping someone already has such a script in 'good ol' command line syntax, otherwise I was thinking of trying to make this bit of code more usable by passing it the contents of a 'net user'...
0
Comment
Question by:nummagumma2
  • 10
  • 7
17 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885189
Well the net user way would be :

@echo off
if "%1"=="" echo Enter %0 username to get last password change date
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do echo Date is %%a & echo Time is %%b
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885213
Your program worked fine for me too btw and returned the same date... strComputer is the computername and /kenmyer the (local) username on there.
0
 
LVL 1

Author Comment

by:nummagumma2
ID: 17885249
Wow.  Your code is so much shorter and simpler than the VB.

I didn't remember that this was a valid option with the net user command.  I was refering to the fact that if you use 'net user' without any other text or switches it returns a list of all users.  I was going to find a way to take the output of that and feed it into the .vb above... now I want to figure out how to take your sweet code and feed it the contents of 'net user' as the variables...   hmmm...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885322
What are you after it doing?  At the moment effectively it strips the line from "net user username" using find and pulls out the two fields using the for.  let me know and I might be able to do now, otherwise tomorrow.  Off to bed shortly!

Steve
0
 
LVL 1

Author Comment

by:nummagumma2
ID: 17885353
So this gives a list of all users (maybe there's another way I don't know?)

C:\Documents and Settings\ps>net user

User accounts for \\XP1275

-------------------------------------------------------------------------------
admin                    Administrator            ASPNET
Guest                    HelpAssistant            SUPPORT_388945a0
The command completed successfully.

I want to output all the users pw-last changed date to a file.

I imagine it would look like this in logic:

get net user output, and strip extra characters.
take each username and feed to your existing code
  (existing code outputs to text file)
loop until done.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885472
Ahh OK.  Making an assumption here that there are no spaces in the usernames...

@echo off
for /f "skip=4 tokens=1,2,3" %%a in ('net user ^| find /v "The command"') do call

:process %%a & call :process %%b & call :process %%a %%b %%c
goto end
:process
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do echo %1 last set %%a at %%b
:end
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885479
That wrapped a bit funny,

@echo off
for /f "skip=4 tokens=1,2,3" %%a in ('net user ^| find /v "The command"') do call :process %%a & call :process %%b & call :process %%a %%b %%c
goto end
:process
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do echo %1 last set %%a at %%b
:end
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 17885485
Or even:

@echo off
for /f "skip=4 tokens=1,2,3" %%a in ('net user ^| find /v "The command"') do call :process %%a & call :process %%b & call :process %%c
goto end
:process
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do echo %1 last set %%a at %%b
:end
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885607
I see you want it in a text file.  How about CSV with the computername too :-)

@echo off
REM Set output file. Could be writable share e.g. \\server\share\info\password.csv
set output=c:\output.csv
for /f "skip=4 tokens=1,2,3" %%a in ('net user ^| find /v "The command"') do call :process %%a & call :process %%b & call :process %%c
goto end
:process
REM
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do echo %computername%,%1,%%a,%%b >> %output%

:end
0
 
LVL 1

Author Comment

by:nummagumma2
ID: 17885651
Perfect.  Thank you!

Outputs to file:
------------------
@echo off
set logfile=results.csv
echo Username,Date,Time >>%logfile%
for /f "skip=4 tokens=1,2,3" %%a in ('net user ^| find /v "The command"') do call :process %%a & call :process %%b & call :process %%c
goto end
:process
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do echo %1,%%a,%%b >>%logfile%
:end
0
 
LVL 1

Author Comment

by:nummagumma2
ID: 17885658
That's great - you inferred I wanted a csv. =)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885681
No problem. Phew I thought you was going to ask for that to run on all your machines then and we'd have 3 nested for loops.... actually not to difficult, shove it in your login script or use psexec ;-)
0
 
LVL 1

Author Comment

by:nummagumma2
ID: 17885754
nah, I'd use psexec for that...

I do have one tweak now that I've used it on a couple of machines, it would be great if I could have it also pick up 2 more fields for the CSV... not sure how to incorporate that...

Account Active  and
Account expires

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17885880
Try these (untested as not on windows pc at the moment) to replace the current second for line.  Bit inefficient running the command three times.  I suppose the other way would be to check using a for loop and if against each line of the output of net user rather than three find commands:

for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do set pass1=%%a & set pass2=%%b

for /f "tokens=4,5" %%a in ('net user %1 ^| find /i "Account active"') do set pass3=%%a & set pass4=%%b

for /f "tokens=4,5" %%a in ('net user %1 ^| find /i "Account expires"') do set pass5=%%a & set pass6=%%b

echo %compuername%,%1,%pass1%,%pass2%,%pass3% ... etc. >>%logfile%
0
 
LVL 1

Author Comment

by:nummagumma2
ID: 17885956
a little tweaking on the tokens line, and it works....

@echo off
set logfile=results.csv
echo Username,Date,Time,Active?,Expires? >%logfile%
for /f "skip=4 tokens=1,2,3" %%a in ('net user ^| find /v "The command"') do call :process %%a & call :process %%b & call :process %%c
goto end
:process
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do set cdate=%%a & set ctime=%%b
for /f "tokens=3" %%a in ('net user %1 ^| find /i "Account active"') do set cActive=%%a
for /f "tokens=3" %%a in ('net user %1 ^| find /i "Account expires"') do set cExpires=%%a
echo  %1,%cdate%,%ctime%,%cActive%,%cExpires% >>%logfile%
:end



Thanks!
0
 
LVL 1

Author Comment

by:nummagumma2
ID: 17885966
one other fine tune... skips empty lines (no username in position 2,3 of net user command)

@echo off
set logfile=results.csv
echo Username,Date,Time,Active?,Expires? >%logfile%
for /f "skip=4 tokens=1,2,3" %%a in ('net user ^| find /v "The command"') do call :process %%a & call :process %%b & call :process %%c
goto end
:process
if "%1"=="" goto :end
for /f "tokens=4,5" %%a in ('net user %1 ^| find "Password last set"') do set cdate=%%a & set ctime=%%b
for /f "tokens=3" %%a in ('net user %1 ^| find /i "Account active"') do set cActive=%%a
for /f "tokens=3" %%a in ('net user %1 ^| find /i "Account expires"') do set cExpires=%%a
echo  %1,%cdate%,%ctime%,%cActive%,%cExpires% >>%logfile%
:end

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 17887172
Good point, was typing it on a pda at midnight so forgive me :-)

Looks like you can write rest yourself now anyway!

Good luck

Steve
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
robocopy for today files 14 180
old Toshiba laptops supporting both MS Dos and Win 95 22 190
Batch file output 20 92
call multiple sqlcmd's within one bat file - with logging 9 52
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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