Dumping out all the DCs in a domain to a txt file

Dumping out all the DCs in a domain to a txt file

  • Comments 2
  • Likes

Short and sweet way of dumping out the DCs to a txt file, in a script:

 

for /f "skip=1" %%a in ('netdom query dc /domain:YOURDOMAIN') do (if %%a == The (echo.) else echo %%a >> test.txt)

 

So what's the deal with all the syntax, and how would this be useful?  Well if you do simple admin scripting then this is pretty useful, say you want to look at all the DCs in the forest and check to make sure that they all have a certain reg key (sample below), well you could create a simple cmd script in about 5 minutes using the above line and the use reg query to do the dirty work.  Hope you can find a use for this, my next post will use this to demonstrate how to do some ghetto time skew monitoring on DCs.

 

Syntax breaks down like this:

 

/f is needed because we are using a command to pull the variable %a ('netdom query dc /domain:YOURDOMAIN')

"skip=1" We use this so that we skip the first line of the output from netdom query dc which looks like this:

C:\localbinx64>netdom query dc /domain:braddom
List of domain controllers with accounts in the domain:  <-- Skips this line.

BRAD-DC-20
BRAD-DC-22
BRAD-DC-26
BRAD-DC-15
The command completed successfully. <-- Don't want this either see below on how we get around this.

 

(if %%a == The (echo.) else echo %%a >> test.txt) And what's all this?  Well its my way of getting around the last line.

 

Here is a script that uses this technique and checks the strict replication key, I don't dump the servers to a txt file because hey I dont need to, just save this into a cmd file...  Play around to figure our what the findstr does.

 

@echo off

for /f "skip=1" %%a in ('netdom query dc /domain:Yourdomain') do (
if %%a == The (echo.)
echo %%a
reg query \\%%a\HKLM\system\currentcontrolset\services\ntds\parameters /v "strict replication consistency" |findstr /i strict)

 

Output looks like:

C:\>strict.cmd
BRAD-DC-20
strict replication consistency REG_DWORD 0x1
BRAD-DC-22
strict replication consistency REG_DWORD 0x1
BRAD-DC-26
strict replication consistency REG_DWORD 0x1
BRAD-DC-05
strict replication consistency REG_DWORD 0x1
BRAD-DC-27
strict replication consistency REG_DWORD 0x1
BRAD-DC-10
strict replication consistency REG_DWORD 0x1
BRAD-DC-11
strict replication consistency REG_DWORD 0x1
BRAD-DC-25
ERROR: The system was unable to find the specified registry key or value.
BRAD-DC-24
ERROR: The system was unable to find the specified registry key or value.
BRAD-DC-35
strict replication consistency REG_DWORD 0x1
BRAD-DC-04
strict replication consistency REG_DWORD 0x1
BRAD-DC-03
strict replication consistency REG_DWORD 0x1
BRAD-DC-23
strict replication consistency REG_DWORD 0x1
BRAD-DC-14
strict replication consistency REG_DWORD 0x1
BRAD-DC-08
strict replication consistency REG_DWORD 0x1
BRAD-DC-18
strict replication consistency REG_DWORD 0x1
BRAD-DC-15
strict replication consistency REG_DWORD 0x1

 

 

IceRocket tags:
Comments
  • I've used variations of below (like an addition pipe to sort).  This is from Win2K - which may not work for W2k3 or higher (upgrade being planned now) - and doesn't require IF statements (watch line wraps).

    for /f "tokens=1 usebackq delims=. " %a in (`nltest /dclist:DOMAIN ^| find /i "DOMAIN"`)

    do @echo %a

  • Oh drat...the 'find' should be looking for 'site:' - not the domain name - find /i "site:"

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment