User Status Lookup Batch Script Assistance

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
elsrik
Posts: 1
Joined: 13 Jul 2017 12:56

User Status Lookup Batch Script Assistance

#1 Post by elsrik » 13 Jul 2017 13:05

I have been trying to work on a script to inquire about domain account status from local user data, which would then query AD with net use /domain. I'm also trying to stay away from Powershell, as it's not implemented on some of the PCs.

My goal is to have A message displayed for Active, Inactive, and Disabled accounts would have their user directory deleted if Not found or Disabled.
This is not focusing on the user profiles, just user file directories left behind by others named with their domain credentials.

So far I'm getting ERRORLEVEL 0 on *everything*, even on strings were the domain user query is returned as "Active: No".

Code: Select all

@ECHO OFF
Dir c:\Users\ /B  > C:\temp\Users_data.txt

for /F "tokens=*" %%A in (C:\temp\Users_data.txt) do  (
   ECHO Processing %%A....
   net user /domain  %%A > C:\temp\User_status.txt

@findstr -sip "Account active               Yes"   C:\temp\User_status.txt >NULL
 ECHO ERRORLEVEL :%ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 echo User %%A is Active!!

@findstr -sip "Account active               No"   C:\temp\User_status.txt >NULL
 ECHO ERRORLEVEL :%ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 echo User %%A is NOT Active!! User might be on LOA! Delete?

@findstr  -sip "The user name could not be found."  C:\temp\User_status.txt >NULL
ECHO ERRORLEVEL :%ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 echo User could not be Found, data will be deleted!

REM rd d:\LocalData\%%A /s /q
ECHO .
)

REM del C:\temp\Users_data.txt
REM del C:\temp\User_status.txt

Pause


Any help would be appreciated.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: User Status Lookup Batch Script Assistance

#2 Post by penpen » 13 Jul 2017 15:48

I would guess you are using a wrong findstr command:
You may delete the ">NULL" part at the end (which is probably wrong anyway and should have been ">nul"). to see the search result of that command(s) you are using.

You may try:

Code: Select all

>nul findstr /L /C:"Account active               No"   "C:\temp\User_status.txt"
(Similiar for the other commands: Just change the search string.)

If the above doesn't help, then note that i'm not familiar with the content of "User_status.txt":
Could you please provide an anonymized sample file for each case?
Maybe reduced to the important parts only - but ensure that this issue still there on the shortened files.


penpen

Post Reply