Solved

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

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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