Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Combine 2 scripts into 1 for identifying stale AD objects

Posted on 2013-06-12
5
Medium Priority
?
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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 85

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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