Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

Combine 2 scripts into 1 for identifying stale AD objects

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
samiam41
Asked:
samiam41
  • 3
  • 2
1 Solution
 
oBdACommented:
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
 
samiam41Author Commented:
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
 
oBdACommented:
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
 
samiam41Author Commented:
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
 
samiam41Author Commented:
Brilliant work as always.  Amazing script, clean coding and I appreciate your time and attention.

Regards,
Aaron
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now