Jeffrey Renfroe
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_Netwo rk\DP_Prep _Ver1.ps1 \\<<Server>>\e$:
psexec \\<<server>> -u sccm\user -p password cmd /c "echo . | powershell -file e:\DP_Prep_Ver1.ps1"
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_Netwo
psexec \\<<server>> -u sccm\user -p password cmd /c "echo . | powershell -file e:\DP_Prep_Ver1.ps1"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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"
Note that I've used the "detached" switch, so psexec is not waiting for finishing execution of each command.
ASKER
Thank you for the quick response
Happy to help - thanx for the grade! :^)
Open in new window
~bp