Powershell and CSV Problems

Hello been write a lot of new functions lately but now i need to write output to a csv file and I can't the out-put too look right i been looking around a lot on the internet but non of the examples i have found dose what I need. Here is the code

$numColstoExport = "4"

$content= @("1", "2", "3","4", "5", "6", "7")
            
$finalhash=@()
$Headers=@("Namn","Status","Net","RAM","test", "test","test")
foreach ($row in $content){
    $obj = new-object PSObject
    for ($i=0;$i -lt $numColstoExport; $i++){
              $obj | add-member -membertype NoteProperty -name $Headers[$i] -value $row[$i]
      }
   $finalhash+=$obj
   $obj=$null
}

 $finalhash | export-csv -Path C:\Users\sebbe\Desktop\test2.csv -NoTypeInformation


The code generate a csv but I can't get the $content to be displayes right. Everything in content is placed on separated rows for some reason. But if I add an extra row  

$content= @("1", "2", "3","4", "5", "6", "7"),

("1", "2", "3","4", "5", "6", "7")

The content looks alright. 

Any tips? 


July 29th, 2015 12:20pm

What do you expect your output to look like?

What you're doing doesn't make much sense to me.

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 12:35pm

Consider the following:

$results=for($i=1;$i -lt 10;$i++){
    $objhash=@{
        Name=1*$i
        Status=2*$i
        Net=3*$i
        RAM=4*$i
        test1=5*$i
        test2=6*$i
        test3=7*$i
    }
    new-object PSObject -Property $objhash
}
$results | export-csv -Path C:\Users\sebbe\Desktop\test2.csv -NoTypeInformation
July 29th, 2015 12:49pm

What do you expect your output to look like?

What you're doing doesn't make much sense

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 1:02pm

Consider the following:

$results=for($i=1;$i -lt 10;$i++){
    $objhash=@{
        Name=1*$i
        Status=2*$i
        Net=3*$i
        RAM=4*$i
        test1=5*$i
        test2=6*$i
        test3=7*$i
    }
    new-object PSObject -Property $objhash
}
$results | export-csv -Path C:\Users\sebbe\Desktop\test2.csv -NoTypeInformation
July 29th, 2015 1:13pm

Here is a starter.

function addarray{
    Param(
        $anarray=@(1,2,3,4,5,6,7)
    )
[pscustomobject]@{
        Name=$anarray[0]
        Status= $anarray[1]
        Net= $anarray[2]
        RAM= $anarray[3]
        test1= $anarray[4]
        test2= $anarray[5]
        test3= $anarray[6]
    }
}
addarray @(1, 2, 3, 4, 5, 6, 7) | 
    export-csv -Path C:\Users\sebbe\Desktop\test2.csv -NoTypeInformation

You need to think through your requirements and then determine what it is you are asking about.  Ask yourself what this would actually be useful for.  In object systems we do not use arrays for much.  Try thinking in objects.

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 1:16pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics