Adil Hindistan
Adil Hindistan

Reputation: 6615

for loop to get distinguished name does not return anything

The following code to harvest distinguished name works on all my computers but a few.

FOR /F "skip=2 tokens=3*" %%I in ('reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine" /v Distinguished-Name') DO @ECHO "%%I %%J"

I am looking into one XP machine that is exhibiting this, and tried alternatives like the one below to no avail:

FOR /F "tokens=3*" %%I in ('reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine" /v Distinguished-Name |findstr "CN="') DO ECHO "%%I %%J"

Interestingly, I get the results just fine if I do not include for /f like this:

reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine" /v Distinguished-Name |findstr "CN="
    Distinguished-Name  REG_SZ  CN=xyz123,OU=Test-5,DC=test,DC=com

or

reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine" /v Distinguished-Name

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine
        Distinguished-Name  REG_SZ  CN=xyz123,OU=Test-5,DC=test,DC=com

Any ideas what may be going on here?

Upvotes: 0

Views: 238

Answers (2)

Endoro
Endoro

Reputation: 37569

FOR /F "tokens=2*" %%I in ('reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine" /v Distinguished-Name^|find "REG_SZ"') DO ECHO "%%J"

This works with all Windows versions.

Upvotes: 1

Adil Hindistan
Adil Hindistan

Reputation: 6615

Duh! I took that part out of script and forgot to remove '%' so that it should have been %I instead of %%I, which is why nothing was returned.

Upvotes: 0

Related Questions