Mike
Mike

Reputation: 13

Powershell - how to pass csv fields as parameters

I have a problems that I'm trying to solve, but can't seem to figure the key part out. We have tons of records that we process every day using a .jar file, but the problem is that we have to go one by one and that is time consuming. I think we can cut a tremendous amount of time if we use a powershell script.

The problem is that I don't know how to pass the parameters from a csv to a function in powershell.

My csv looks like this

NAME,ID                                                                                                                                                                   
-------                                                                                                                                                                   
John,18                                                                                                                                                                     
Dave,19                                                                                                                                                    
Carmen,20                                                                                                                                                       
Eric,21                                                                                                                                                                    
Tom,22                                                                                                                                                          
Lisa,23                                                                                                                                                          
Kyle,24

The function is

function CreateUser 
& java -jar --create -user $name -id $id -file D:/HR/$name-$id-form.pdf

I imported the csv file using

$dataCSV = "D:\HR\Input\20150303NewUsers.csv"
$data = Import-Csv $dataCSV

So I need something that will go systematically down the file and pass the name field inside the csv as $name and the ID field as $id over and over again until completed. But I can't figure out how to pass those two down on a ForEach-Object method :(

I'm stuck... I've been fighting this all weekend, but nothing.

Any help or guidance will be greatly appreciated! Or if anyone know how to do this in python, that will be cool too! :)

Upvotes: 1

Views: 5616

Answers (3)

user716255
user716255

Reputation: 453

This script will extract values underneath the Name header and place them inside $name variable. Then it will place the values underneath the ID header and place them inside the $id variable.

$data = import-csv -path 'D:\HR\Input\20150303NewUsers.csv'

foreach($item in $data)
{
    $name = $item.Name
    $id = $item.ID 
}

Upvotes: 0

Get-Sleep
Get-Sleep

Reputation: 34

Not sure if typo but your D:\... needs to be enclosed in quotation marks, you haven't closed it off.

Once $data holds the list of imported values simply do foreach ($item in $data) {do something}

Where $item is any word (variable) you want, it simply refers to each row in the CSV.

So...

$data = Import-Csv "D:\importfile.csv"
foreach( $item in $data )
{
    # Do-whatever
}

Upvotes: 0

Walter Mitty
Walter Mitty

Reputation: 18940

I have written a tool that steps through a table (imported from a csv file) and generates an expansion of a template for each row in the table. One thing I do is to copy each of the values in the row to a powershell variable of the same name as the column. This may help you.

Here is the tool that I wrote:

<#  This scriptlet is a table driven template tool. 
    It's a refinement of an earlier attempt.

    It generates an output file from a template and
    a driver table.  The template file contains plain
    text and embedded variables.  The driver table has
    one column for each variable, and one row for each
    expansion to be generated.

    2/15/2015

#>

param ($driver, $template, $out);

$OFS = "`r`n"
$list = Import-Csv $driver
[string]$pattern = Get-Content $template
Clear-Content $out -ErrorAction SilentlyContinue

foreach ($item in $list) {
   foreach ($key in $item.psobject.properties) {
      Set-variable -name $key.name -value $key.value
      }
   $ExecutionContext.InvokeCommand.ExpandString($pattern)  >> $out
   }

The part that may interest you is the innner loop, where I do a Set-Variable that matches the column name with the actual value.

Upvotes: 1

Related Questions