user2776281 user2776281 - 3 months ago 10
PowerShell Question

Use batch file to lookup text in CSV & parse it in to run an application install

I have an application that is to be deployed to 1000+ Windows machines. Each machine is in a remote office and is given a Node ID 1-35 and a Branch ID eg. 001122 (unique to each office). I have the install running silently but at one point it asks you to enter the Node ID and Branch ID. I am using the below code in a batch file to allow the engineer running the install to enter the details manually but this is very time consuming and we would prefer to have no user interaction.

We have all the hostnames, IPs, Node ID and Branch ID in one CSV.

My question is, is it possible for the bat file (or another scripting language) to check the IP address or hostname of the pc locate this in the CSV and to parse in the Node ID and Branch ID found in the corresponding rows. How might I go about changing set /p to look at a csv. I am not a coder so am unsure of how to go about reading in a CSV.

Sample of batch file used:

@echo off

setlocal

(set /p NodeId=Enter Node ID:)
(set /p GroupId=Enter Branch ID:)

:: Check that parameter is passed in
if %GroupId%.==. echo WS Group Id required as parameter & GoTo :END
if %NodeId%.==. echo WS Node Id required as parameter & GoTo :END
if %NodeId% GTR 31 echo WS Node Id should be less than 32 & GoTo :END

set GroupId=%GroupId%
set NodeId=%NodeId%

echo Installing Workstation Group=%GroupId% and Node=%NodeId%

Answer

Assuming a CSV like the following:

Hostname,IP,NodeID,BranchID
Chef,192.168.0.10,1-35,001122
Secretary,192.168.0.12,1-38,001122
Test,192.168.0.24,1-40,00112
...

this should work:

for /f "tokens=1-4 delims=," %%a in ('type file.csv ^|find /i "%computername%" ') do (
  echo Installing Workstation %%a, Group=%%d, Node=%%c with Address %%b
  set groupID=%%d
  set nodeID=%%c
)
Comments