Solved

Combine DSQuery result into loop script

Posted on 2016-11-01
6
51 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 23

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 23

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 23

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now