Solved

Configure batch file to read from text file and pass values to commands

Posted on 2014-01-30
5
740 Views
Last Modified: 2014-01-31
Hello. I have the below batch file that does performs two actions. It copies a PowerShell script to machine and executes PowerShell on that machine using PSExec. The way I have it currently configured, I have to replace <<server>> in each command with the server name that I want to perform the action on. Instead of doing it this way, I would like to have the batch file look at a text file and pass the names of the server to the command. For example, I have a text file called machines.txt with names in the following format:

Server1
Server2

Is it possible to have the batch file look at machines in the text file and pass this to the commands?

Current configuration of script
xcopy C:\Temp\DP_Prep_Ver1_Network\DP_Prep_Ver1.ps1 \\<<Server>>\e$:
psexec \\<<server>> -u sccm\user -p password cmd /c "echo . | powershell -file e:\DP_Prep_Ver1.ps1"
0
Comment
Question by:Lorrec
5 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 39823029
try this:

@echo off
for /f %%a in (servers.txt) do call :process %%a
goto :eof

:process
set server=%1
xcopy C:\Temp\DP_Prep_Ver1_Network\DP_Prep_Ver1.ps1 \\%server%\e$
psexec \\%server% -u sccm\user -p password cmd /c "echo . | powershell -file e:\DP_Prep_Ver1.ps1"

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39823084
Can even simplify a bit, although I appreciate programming is partly an art...

@echo off
for /f "usebackq" %%S in ("c:\temp\servers.txt") do (
  xcopy C:\Temp\DP_Prep_Ver1_Network\DP_Prep_Ver1.ps1 \\%%S\e$
  psexec \\%%S -u sccm\user -p password cmd /c "echo.| powershell -file e:\DP_Prep_Ver1.ps1"
)

Open in new window

~bp
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39824275
Any reason you are not using PowerShell Remoting here? And what's the "echo ." for?

If using your original code, it might be better to perform the copies, and then execute en block:
@echo off
for /F "usebackq delims=" %%S in ("C:\Temp\machines.txt") do xcopy c:\Temp\DP_Prep_Verl_Network\DP_Prep\Verl.ps1 \\%%S\e$
psexec @"c:\Temp\machines.txt" -d  -u sccm\user -p password cmd /c "echo.| powershell -file e:\DP_Prep_Ver1.ps1"

Open in new window

Note that I've used the "detached" switch, so psexec is not waiting for finishing execution of each command.
0
 

Author Closing Comment

by:Lorrec
ID: 39824657
Thank you for the quick response
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39824719
Happy to help - thanx for the grade! :^)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

15 Experts available now in Live!

Get 1:1 Help Now