Link to home
Start Free TrialLog in
Avatar of Jeffrey Renfroe
Jeffrey RenfroeFlag for United States of America

asked on

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

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"
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

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
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.
Avatar of Jeffrey Renfroe

ASKER

Thank you for the quick response
Happy to help - thanx for the grade! :^)