Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import-CSV with dynamic fieldname filter.?

Status
Not open for further replies.

PaulSc

MIS
Aug 21, 2000
148
GB
Hi after picking a few brains (again...) please about a query I have using PowerShell..

I have a CSV file which contains the following data

Code:
REF,   SUN,MON,TUE,WED,THU,FRI,SAT,ACTIVE
Ref001,N,  Y,  Y,  Y,  Y,  Y,  N,  Y
REF002,Y,  N,  N,  N,  N,  N,  Y,  Y
REF003,N,  N,  N,  Y,  N,  N,  Y,  Y
(spaced for ease of reading)

Its inside a function where I would like to be able to call and pass in a "day" name i.e. WED

i.e FilterCsv -Day "WED"

I then use

Code:
importedcsv = import-csv - path "xxxx/yyyy.csv" | Where-Object ($_.ACTIVE -eq "Y")

but I'd like to be able to do the following

Code:
importedcsv = import-csv - path "xxxx/yyyy.csv" | Where-Object ($_.ACTIVE -eq "Y" -and [highlight #EF2929]$day[/highlight] -eq "Y")
to only select rows where ACTIVE = Y AND WED=Y (i.e REF001 & REF003)

however of course $Day is the dynamic fieldname I want to use (i.e. $_.WED = "Y" or SUN="Y" etc)

Ive tried creating a variablw with
Code:
new-variable -Name "Day" -value $Day -visibility Public
but it doesnt filter when using this param
of course it works if I hardcode it as $_.WED="Y" and I know I can have lots of ifs/switches to pick based on the input/day name but I'd like to try and make it more efficient..

So my question is can I or how can I pass through the fieldname to the WHERE-OBJECT value..?

Sorry cant post more code but theres there potential of changing the CSV format if its easier..?

Thank you
PaulSc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top