Solved

Combine 2 scripts into 1 for identifying stale AD objects

Posted on 2013-06-12
5
404 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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 …
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

13 Experts available now in Live!

Get 1:1 Help Now