JSI Tip 7508. How can I generate a .CSV file containing all the computer accounts in my domain that have been inactive for nn weeks?

Jerold Schulman

December 2, 2003

1 Min Read
ITPro Today logo in a gray background | ITPro Today

Using the DSQUERY and DSGET Active Directory command-line tools, I have scripted CompInactive.bat to generate a CompInactive.csv file, in the current folder, of all the computer accounts in your domain that have been inactive for nn weeks. The file contains the following attributes:

Distinguished NameDisabledDescloc

with all commas (,) replaced with semi-colons (;), and might look like:

"CN=JSI001;OU=Domain Controllers;DC=JSIINC;DC=COM","no","DC; PDC","Corportate; Computer room""CN=JSI003;CN=Computers;DC=JSIINC;DC=COM","no","Windows XP; Laptop","CN=JSI005;CN=Computers;DC=JSIINC;DC=COM","yes",,"West Coast"

The syntax for using CompInactive.bat is:

CompInactive weeks

where weeks is the number of weeks that the account has been unused. If weeks is 0, the CompInactive.csv file will contain all computer accounts.

CompInactive.bat contains:

@echo offif {%1}=={} @echo syntax: CompInactive weeks&goto :EOFsetlocalset /a weeks=1000%1%%1000if exist CompInactive.csv del /q CompInactive.csvdsquery computer -inactive %weeks% -limit 0 >%TEMP%CompInactive1.tmpsort %TEMP%CompInactive1.tmp /o %TEMP%CompInactive2.tmpfor /f "Tokens=*" %%c in ('type %TEMP%CompInactive2.tmp') do ( set dn=%%c for /f "Tokens=1*" %%r in ('dsget computer %%c -desc -loc -disabled -c -l') do (  set p1=%%r  set p2=%%s  call :report ))if exist %TEMP%CompInactive1.tmp del /q %TEMP%CompInactive1.tmpif exist %TEMP%CompInactive2.tmp del /q %TEMP%CompInactive2.tmpendlocalgoto :EOF:reportif /i "%p1%" EQU "dsget" if /i "%p2%" EQU "succeeded" goto :outif /i "%p1%" EQU "desc:" set desc=%p2%&goto :EOFif /i "%p1%" EQU "loc:" set loc=%p2%&goto :EOFif /i "%p1%" EQU "disabled:" set disabled=%p2%goto :EOF:outset dn=%dn:,=;%if "%desc%" NEQ "" set desc="%desc:,=;%"if "%loc%" NEQ "" set loc="%loc:,=;%"@echo %dn%,"%disabled%",%desc%,%loc%>>CompInactive.csv



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like