• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3851
  • Last Modified:

Batch Script to Get Computer's OU

Can someone tell me how to return the OU a computer is in using batch? When someone logs on to their computer, I want a logon script to run that can determine what OU the computer is. I assume I could run a command that searches Active Directory and pass it %computername%, but I'm not sure how. Also, the OU should be set to a variable like %ou% or something.

Thanks in advance.
0
ProUAdmin
Asked:
ProUAdmin
  • 2
  • 2
2 Solutions
 
LearnctxEngineerCommented:
Try this, it 'works'..to a fashion as long as dsquery is available on the machine its running from.
for /f "delims=* usebackq" %a in (`@dsquery computer -name %Computername%`) do @set computer=%a
set ou=%computer:"=%
set ou=%ou:*,OU=OU%
echo %ou%

Open in new window

0
 
ProUAdminAuthor Commented:
I got the error below. Any thoughts?
computernamea was unexpected at this time.

Open in new window

0
 
LearnctxEngineerCommented:
If you're running it from a batch file you're need to replace %a with %%a.
Also add @ECHO OFF to the top of the batch file to avoid lots of feedback.
0
 
ProUAdminAuthor Commented:
That worked perfect. Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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