quinta-feira, 8 de outubro de 2015

Powershell script to read .csv file and processing each row

1. Introduction

This script shows how to process each line of a .csv file.  Put fields headers in first line.


2. Script

2.1. Sample 'services.csv' input file

servico,login,senha
Spooler,lp0795\tmp,tmp
Themes,lp0795\tmp,tmp
Spooler,lp0795\tmp,tmp


2.2. Script sample

# ##############################################################################
# filename: csv-process.ps1
# author  : josemarsilva@yahoo.com.br
# date    : 2015-10-09
# purpose : Sample script processing each row from a .csv file. First row has
#           fields titles
# pre-reqs: 
#           - Set-ExecutionPolicy Unrestricted
# remarks :
# ############################################
#
$csvFile = "C:\Users\Josemarsilva\Downloads\services.csv"
$bufferRead = Import-Csv $csvFile
foreach ( $line in $bufferRead ) {
    # println fields ...
    "servico = " + $line.servico + "; login = " + $line.login + "; senha=" + $line.senha
    # do whatever you want here! you have each field in $line.<field>
}


2.3. Script output sample

servico = Spooler; login = lp0795\tmp; senha=tmp
servico = Themes; login = lp0795\tmp; senha=tmp
servico = Spooler; login = lp0795\tmp; senha=tmp

Nenhum comentário:

Postar um comentário