Solved

Combine DSQuery result into loop script

Posted on 2016-11-01
6
61 Views
Last Modified: 2016-11-05
I'm using DSQuery to get the Server list then I want to run a loop script against each one.
How do I correctly set the FOR command to combine them?

dsquery computer -o rdn "OU=myservers,DC=company,DC=com" -limit 0


echo on
SET /a i=1

:loop
IF %i%==10 GOTO END
echo y | reset session %i% /server: %dsqueryresults%
SET /a i=%i%+1
GOTO LOOP
:end
0
Comment
Question by:GGHC
  • 3
  • 3
6 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 41868940
@echo off
setlocal enabledelayedexpansion

set /a Count=1
for /f %%a in ('dsquery computer -o rdn "OU=myservers,DC=company,DC=com" -limit 0') do (
  echo %%a
	if !Count!==10 exit /b
  echo y | reset session !Count! /server:%%a
  set /a Count=!Count!+1
)

:end

Open in new window

0
 

Author Comment

by:GGHC
ID: 41869414
Thanks NVIT

It didn't seem to work. It almost appears like it is not looping.

The goal is to run through session 1 through 10 for each server.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 41869434
I tested it before posting.

Change line 8 to:
echo y ^| reset session !Count! /server:%%a

Open in new window


This will echo the command instead of running it.

Then run the .bat. Does it look correct?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:GGHC
ID: 41871992
Thanks LVL 22
Almost there. It does loop but it suppose to loop the "Reset Session 1>10 for each server
Here is an example of the expected output

echo y | reset session 1 /server:Server1  
echo y | reset session 2 /server:Server1  
echo y | reset session 3 /server:Server1  
echo y | reset session 4 /server:Server1  
"
"
"
echo y | reset session 1 /server:Server2
echo y | reset session 2 /server:Server2
echo y | reset session 3 /server:Server2
"
"
"

------------------------------------------------------------
Below is the old Script that had hard coded computer names.
Now names are dynamic which is why I need the Dsquery command in the mix

echo on
SET /a i=1

:loop
IF %i%==10 GOTO END
echo y | reset session %i% /server:ServerDC1
echo y | reset session %i% /server:ServerDC2
echo y | reset session %i% /server:Server1b1
echo y | reset session %i% /server:Server1c1

SET /a i=%i%+1
GOTO LOOP
:end
-------------------------------------------------

Thanks for your help
0
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 41873226
Here is a test version.
When you're satisfied with the test output and to run it for real, on Line 8, remove the ^
@echo off
setlocal enabledelayedexpansion

for /f %%a in ('dsquery computer -o rdn "OU=myservers,DC=company,DC=com" -limit 0') do (
  set Host=%%a
	for /L %%A in (1,1,10) do (
		set Count=%%A
		echo y ^| reset session !Count! /server:!Host!
	)
)

Open in new window

1
 

Author Closing Comment

by:GGHC
ID: 41875189
Brilliant! Worked like a charm! Thank you so much!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question