1 year ago

#77080

test-img

Michele

Import-csv, column name has multiple lines - how do I access data

I have an excel file I convert to csv, and use this to import the data. For some reason, I can't get the data for the sdk column, maybe because it has a line break in the name.

This is what the csv looks like:

Display to OOO    Error Code (device)  Error Code (device)  Error Code (device)  SDK    
                  Major code           Minor code           Minor code 2         code     Description            Conditions for error
Err 01            1(1)                 1(1)                 N                    1000     Error Message here     The device had an error so to speak

What I need is the row's sdk number and the Conditions that detect error. But I can't find a row index that works for the SDK column.

This is my code:

            $Excel = New-Object -ComObject Excel.Application
            $wb = $Excel.Workbooks.Open($errorCodeListFilePath)
            $xBasePath = Split-Path -Path $errorCodeListFilePath #defaults to parent
            $xBaseName = Split-Path -Path $errorCodeListFilePath -Leaf -Resolve 
            $xBaseName -Match '(.*).xlsx'
            $baseFileName = $Matches[1]
            $csv_filenamePath = ""
            foreach ($ws in $wb.Worksheets) #csv must be closed to do this
            {
                $ws.SaveAs("$xBasePath\" + $baseFileName + ".csv", 6) #if it exists, there's a popup to say yes to
                $csv_filenamePath = "$xBasePath" + "\" + $baseFileName + ".csv" #the last sheet, there's only one
            }
            $Excel.Quit()

            #get contents of csv
            $csv_contents = Import-Csv -Path $csv_filenamePath # -Header '', ''
            foreach ($row in $csv_contents)
            {
                #if($row.sdk -contains "1200")
                #{
                    Write-Host "error desc: $row"   #see note 1 below
                    Write-Host $row.'SDK code'  #no output
                    Write-Host $row['SDK code'] #no output
                    Write-Host $row.'SDK'       #no output
                    Write-Host $row.'Conditions that detect error' #see note 2 below
                    Write-Host $row['Conditions that detect error'] #prints nothing

                #}
            }

Note 1:

error desc: @{Display to OOO=Err 01; Error Code (device)
Major code=1(1); Error Code (device)
Minor code=1(1); Error Code (device)
Minor code 2=N; SDK
code=1000; Description=Error Message here; Conditions that detect error=The device had an error so to speak}

Note 2:

The device had an error so to speak.

My question is what can I do to obtain the data in the sdk column name? I think the issue is happening because the column name has multiple lines. I can't modify the original document. Eventually I'm going to use a pipeline with this once I find out how to get the data (via matching with a specific sdk number), so that would be ok to use pipelines. But as far as I know, I have to have the header name to match a sdk number in the pipeline, and that isn't working to use the header name.

I've been looking at: import-csv

import-csv microsoft

I have poershell 5.1 and am using vsCode

Update: I edited the original xlsx file to remove the line break but still couldn't access the data well with what was returned with the import csv.

I'm trying this ImportExcel now:

#install ImportExcel at powershell prompt per comment on next line
Install-Module -Name ImportExcel -Scope CurrentUser -Force (dependency - 1. Close all instances of PowerShell (console, ISE, VSCode), 2. Move the PackageManagement folder from OneDrive, 3. Open a PowerShell Console (Run As Administrator), 4. Run Install-Module ImportExcel)



   #import it
    $importedExcel = Import-Excel -Path $errorCodeListFilePath -StartRow 2        #| Out-GridView #it starts on row 2
    $ConditionForErr = $importedExcel | Where-Object 'SDK code' -eq 1200        #| $_.'Conditions for error' #| Out-GridView
    Write-Host $ConditionForErr #prints everything for the row
    Write-Host $ConditionForErr['SDK code'] #prints nothing

I'm not clear on how to just get the value in the Conditions for error column when SDK code is 1200. I'm not finding a good example in a search.

powershell

indexing

row

import-csv

0 Answers

Your Answer

Accepted video resources