Solved

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

Posted on 2014-01-30
5
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 54

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 70

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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