finding OU and deleting from domain Batch Script list COMPUTER and Another script move/delete them

Hello fellas,

Is there any way, anybody can help me to create  these three scripts below, I would appreciate a brilliant mind/s:

1) put in a computerlist.txt and will output.csv the computer name(column A) and current OU(column B) e.g.:
    PC NAME                 OU
workstation1           ACME

2)put in a computerlist.txt and it moves them to an specific OU

3)put in a computerlist.txt and deletes them from the domain
   
any thoughts?
LVL 3
ivan rosaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deroodeSystems AdministratorCommented:
Finding the OU could be done with DSQuery:

setlocal EnableDelayedExpansion

rem Header
echo PCNAME,OU > output.csv

for /F %%A in (computerlist.txt) do call :findou %%A 

exit /b

:findou
set OU=
for /F "tokens=2 delims=," %%F in ('dsquery computer -name %1') do set OU=%%F
echo %1,%OU% >> output.csv
exit /b

Open in new window

It will output the first OU of the computer is in (leaving out the rest of the DSquery result.
Is this what you are looking for?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deroodeSystems AdministratorCommented:
Moving a computer is quite similar, after finding the OU of the computer, use dsmove to move it to another OU:

setlocal EnableDelayedExpansion

for /F %%A in (computerlist.txt) do call :movecomputer %%A 

exit /b

:movecomputer
set OU=
for /F  %%F in ('dsquery computer -name %1') do dsmove %%F -newparent ou=Marketing,dc=microsoft,dc=com

exit /b

Open in new window


Deleting is done with dsrm:

setlocal EnableDelayedExpansion

for /F %%A in (computerlist.txt) do call :removecomputer %%A 

exit /b

:removecomputer
set OU=
for /F  %%F in ('dsquery computer -name %1') do dsrm %%F -noprompt

exit /b

Open in new window


As always, first test !
0
Will SzymkowskiSenior Solution ArchitectCommented:
If you want this to be done in Powershell, please let me know and i will assist.

Will.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

ivan rosaAuthor Commented:
Deroode,

pretty close..., for your first, I created this one line script, which pretty much does what I need but I don't know how to split the result into the two columns


for /f "tokens=1" %a in (computerlist.txt) DO dsquery computer -name %a>>output.csv

so this is what CSV would look like e.g.
CN=WORKSTATION1,OU=Computers,OU=Marketing,OU=Hollywood,OU=USA,DC=corp,DC=acme,DC=com

do you know how to split the result csv like so?
Column A                           Column B
cn=workstation1         OU=Computers,OU=Marketing,OU=Hollywood,OU=USA,DC=corp,DC=acme,DC=com
0
deroodeSystems AdministratorCommented:
I wouldn't know how to do that; mainly because the output of de dsquery command is in fact a comma separated list, and for the csv file you actually want the first name, a comma, and then the rest of the line in quotes so the result is:

"cn=workstation1","OU=Computers,OU=Marketing,OU=Hollywood,OU=USA,DC=corp,DC=acme,DC=com "

Maybe with some tricks you could do that in one line, but i guess that would become quite unreadable. My script does the job and is pretty straightforward....
0
ivan rosaAuthor Commented:
This has actually helped me to combine my script with the above
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

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.