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?

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 Name
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 off
if \{%1\}==\{\} @echo syntax: CompInactive weeks&goto :EOF
set /a weeks=1000%1%%1000
if exist CompInactive.csv del /q CompInactive.csv
dsquery computer -inactive %weeks% -limit 0 >%TEMP%\CompInactive1.tmp
sort %TEMP%\CompInactive1.tmp /o %TEMP%\CompInactive2.tmp
for /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.tmp
if exist %TEMP%\CompInactive2.tmp del /q %TEMP%\CompInactive2.tmp
goto :EOF
if /i "%p1%" EQU "dsget" if /i "%p2%" EQU "succeeded" goto :out
if /i "%p1%" EQU "desc:" set desc=%p2%&goto :EOF
if /i "%p1%" EQU "loc:" set loc=%p2%&goto :EOF
if /i "%p1%" EQU "disabled:" set disabled=%p2%
goto :EOF
set dn=%dn:,=;%
if "%desc%" NEQ "" set desc="%desc:,=;%"
if "%loc%" NEQ "" set loc="%loc:,=;%"
@echo %dn%,"%disabled%",%desc%,%loc%>>CompInactive.csv

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.