Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

Extracting Values from a PowerShell import-csv cmdlet

Dear All,
I have a CSV file which I am reading using the import-csv cmdlet. The file is similar to:
The problem is are the fields at the end with the headers SCSI1, SCS2 etc. There could be anything from 1 to 32 of these. I am doing the standard powershell stuff to read the file and process each line in it record at a time as shown below. I can get to each field no bother but the group at the end are proving to be a problem. I need to turn the values in the SCSI fields into an array. If there were a fixed number I could just do @($R.SCSI1, $R.SCSI2....) but there could be quite a few and the number could vary line by line.

Does anyone have any thoughts on how this could be done?

Best wishes....
Colin Bruce
$recs = import-csv "data.csv"
foreach ($r in $recs)
  .......process fields here

Open in new window

  • 2
1 Solution
Chris DentPowerShell DeveloperCommented:

Hi Colin,

How about something like this?

$Recs = @()
Import-CSV "data.csv" | %{
  $Temp = @()
  ForEach ($Property in ($_ | Get-Member -Type NoteProperty -Name "SCSI*")) {
    If ($_.$($Property.Name) -ne $Null) { $Temp += $_.$($Property.Name) }
  $Recs += $_ | Select-Object NCPUs, Memory, @{n='SCSI';e={ $Temp }}

Open in new window

CovUniAuthor Commented:
Dear Chris,
Thanks for the reply and help. It worked perfectly. I am very impressed. I spent hours trying to do this and got nowhere. I owe you a beer at the very least.

Best wishes...
Chris DentPowerShell DeveloperCommented:

Glad I could help out :)


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now