Solved

Combine 2 scripts into 1 for identifying stale AD objects

Posted on 2013-06-12
5
401 Views
Last Modified: 2013-06-12
Hey Experts.  I'm on the 3rd leg of this project and would like to now merge the two scripts I have (thanks to EE) into a single, formidable script (either a batch or powershell).

Script I have for stale computer accounts:
@echo off
setlocal enabledelayedexpansion
set LogFile=C:\Tools\staleAdPc.log
if not exist "%LogFile%" goto SkipRename
for %%f in ("%LogFile%") do (
	for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do (set TimeStamp=%%c%%a%%b)
	set NewName=%%~nf.old.!TimeStamp!%%~xf
	echo Renaming "%LogFile%" to "!NewName!" ...
	ren "%LogFile%" "!NewName!"
	ECHO File date: %%~tf
)
:SkipRename

dsquery computer OU=Depts,DC=x -inactive 8 -limit 300 > %logfile%
(for /f "tokens=2,3* delims=,=" %%i in (%logfile%) do @echo %%~i) > c:\tools\staleADPc1.log

Open in new window

Code for stale user accounts:
@echo off
setlocal
set LogFile=C:\tools\staleADUser1-N.log
if exist "%LogFile%" del "%LogFile%"
for /f "delims=" %%a in ('dsquery.exe user "OU=Depts,DC=x" -o samid -limit 0 -inactive 8') do (
	echo %%~a
	>>"%LogFile%" echo %%~a
)

Open in new window


The two output files can remain as a way of keeping computer and user accounts separate.  I've tried doing the merge on my own and the variables have given me enough of a struggle that I'm ready to turn it over to the real experts and learn a little.  Thanks for your help with this and providing the necessary script.
0
Comment
Question by:samiam41
  • 3
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39241597
Try this then; I changed the code for the computer accounts to match the one for the user accounts, and renaming the log now happens in a function:
@echo off
setlocal enabledelayedexpansion
set LogFileComputer=C:\Tools\staleAdPc.log
set LogFileUser=C:\Tools\staleADUser.log
if exist "%LogFileComputer%" call :RenameLog "%LogFileComputer%"
if exist "%LogFileUser%" call :RenameLog "%LogFileUser%"
echo.
echo Stale computer accounts:
for /f "delims=" %%a in ('dsquery.exe computer -o rdn -limit 300 -inactive 8') do (
	echo %%~a
	>>"%LogFileComputer%" echo %%~a
)
echo.
echo Stale User accounts:
for /f "delims=" %%a in ('dsquery.exe user -o samid -limit 0 -inactive 8') do (
	echo %%~a
	>>"%LogFileUser%" echo %%~a
)
goto :eof

:RenameLog
set LogFile=%~1
for %%f in ("%LogFile%") do (
	for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do (set TimeStamp=%%c%%a%%b)
	set NewName=%%~nf.old.!TimeStamp!%%~xf
	echo Renaming "%LogFile%" to "!NewName!" ...
	ren "%LogFile%" "!NewName!"
)
goto :eof

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 39242432
Hey oBdA!  Thanks for the suggestion.

When I run the script, the existing output file is renamed as expected but a new log output file is not created.  The renaming happened only the first time I ran the script as the output file existed then but it hasn't been created since that initial running.  Does there need to be any sort of:

if not exist mkdir
0
 
LVL 83

Expert Comment

by:oBdA
ID: 39242488
If you started the script by doubleclicking, please start it again from an open command prompt to see possible error output.
It works just fine here. Could it just be that there are no stale accounts?
If you enter the DN of the OU, make sure you put them in double quotes.
Note that you can copy the complete dsquery command between the single quotes in the "for /f" loop and paste it into a command prompt to check if the command itself works.
0
 
LVL 9

Author Comment

by:samiam41
ID: 39242931
Yeah.....  So, evidentally the script works like magic as I just discovered 200+ stale computer accounts have been moved to the INACTIVE OU.  Since there weren't any computers to move, no output log.  Good logic!  Thanks again for your help.
0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 39242934
Brilliant work as always.  Amazing script, clean coding and I appreciate your time and attention.

Regards,
Aaron
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now