Solved

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

Posted on 2014-01-30
5
767 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 53

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 69

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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