Solved

Combine DSQuery result into loop script

Posted on 2016-11-01
6
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This article will show, step by step, how to integrate R code into a R Sweave document
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

739 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