Solved

Combine 2 scripts into 1 for identifying stale AD objects

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

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 84

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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