locked
Need help importing CSV File RRS feed

  • Question

  • Hi Guys,

    I have been at this for a couple of days with little success.

    I have exported out a Powershell object by using below code(the two variables values have preceding code(not shown) to collect them:

    "

    $timestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}

    $props = @{historysaveduser = $historysaveduser -join ",`r`n";

               historysavednumberoffailed = $historysavednumberoffailed -join ",`r`n"}
    $obj = New-Object -TypeName PsObject -Property $props

    $obj | Export-CSV -Path "C:\Shared Folder logs\$timestamp.csv" -NoTypeInformation"

    I get a CSV file like below: 

    "historysaveduser","historysavednumberoffailed"
    "ABTest,
    ACTest,
    ADTest","54,
    63,
    72"

      What I now need to do is to send the values for both historysaveduser and historysavednumberoffailed by using import CSV, so I can pipe this data into another script to generate a chart. The problem is that I am unable get "ABTest", "ACTest" and "ADTest" as $historysaveduser[0], historysaveduser[1] and historysaveduser[2] no matter what I try. I am able to get historysaveduser[0] as "A" or ABTest,ACTest, ADTest only.

    Please help!


    Wednesday, April 4, 2018 6:20 PM

All replies

  • Import-Csv C:\myfile.csv | Foreach {
    # Below will print each row of data $_.historysaveduser $_.historysavednumberoffailed }
    Also, looking at your csv data, it looks malformed

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, April 4, 2018 6:24 PM edit
    Wednesday, April 4, 2018 6:22 PM
  • If you posted your code correctly you would quickly see your mistake.

    Please fix your original post.


    \_(ツ)_/

    Wednesday, April 4, 2018 6:25 PM
  • You are trying to join two arrays.  This cannot be done.  You must enumerate both arrays and create a new object for each index in the two arrays.

    I think I will leave it to clayman2 to show you how to do that.


    \_(ツ)_/


    • Edited by jrv Wednesday, April 4, 2018 6:28 PM
    • Proposed as answer by jrv Wednesday, April 4, 2018 7:54 PM
    Wednesday, April 4, 2018 6:27 PM
  • Thank you - I have already got to that point.

    I think I did not explain myself well enough - Sorry!

    So what I am trying to do is create charts from two arrays - $historysaveduser and $historysavednumberoffailed.

    Say X = $historysaveduser and Y = $historysavednumberoffailed

    Problem is that when I import this data using import-csv, I need $historysaveduser[0] to be ABTest, $historysaveduser[1] to be ACTest etc. This will match up with corresponding values for $historysavednumberoffailed[0] and $historysavednumberoffailed[1]. I can only get $historysaveduser[0] as "A" or "ABTest,ACTest,ADTest". The same for $historysavednumberoffailed[0]. Obviously I am unable to create a chart when my X and Y values are not coming out like this.

    Wednesday, April 4, 2018 6:51 PM
  • Can you please fix your original post by using the code posting tool? It is unreadable as posted.


    \_(ツ)_/

    Wednesday, April 4, 2018 7:35 PM
  • Sorry for the very delayed response as I have been busy with other things for the last month.

    Thank you as that has nearly worked!

    So what I get now is:

    ABTest,
    ACTest,
    ADTest
    54,
    63,
    72

    $historysaveduser[0]= ABTest
    $historysaveduser[1] = ACTest
    historysaveduser[2] = ADTest


    which is all good!

    $historysavednumberoffailed = nothing and says null array!


    Any ideas why this is null as it must be picking it up from the CSV file, since the values 54, 63 and 72 are being picked up?

    Sunday, May 6, 2018 1:11 PM
  • What is $historysaveduser and $historysavednumberoffailed ?

    • Edited by JS2010 Monday, May 7, 2018 3:58 AM
    Monday, May 7, 2018 3:57 AM
  • Hello,

    $historysaveduser = ABTest, ACTest etc

    $historysavednumberoffailed = 54,63 etc which is the number of times they have tried to access a shared folder that they do not have access to.

    Sunday, May 20, 2018 12:13 PM
  • Hi there,

    I think that your requirement is not clear.

    Please Post three things:

    1. What is the content of the CSV that you are trying to "import."

    2. The code that you are working (This is already done)

    3. The desired output, this will give you a quick answer without complication.


    As an additional note, make sure you are using the "Insert block Code" snippet so you can have the code in a proper format.



    • Edited by j0rt3g4 Sunday, May 20, 2018 4:49 PM
    Sunday, May 20, 2018 4:48 PM