Solved

Combine DSQuery result into loop script

Posted on 2016-11-01
6
38 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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

16 Experts available now in Live!

Get 1:1 Help Now