Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Combine DSQuery result into loop script

Posted on 2016-11-01
6
Medium Priority
?
134 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 25

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 25

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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 

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 25

Accepted Solution

by:
NVIT earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

916 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