0

I would like to make a new line in my hashtable to extract it in a csv.

I initialize my variable in hastable

$vlr=@{}

$vlr["OS"]=,@("test","test2")

I extract my variable in a .csv

$Output += New-Object PSObject -Property $vlr

$output | Convert-OutputForCSV  | export-csv -NoTypeInformation -Delimiter ";" -Path $filepath

and the problem is in the extraction the result of the values ​​is on the same line

My goal is that each value is in a different line

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • 1
    please, do not post images of code/errors/data. why? lookee ... Why not upload images of code/errors when asking a question? - Meta Stack Overflow — https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Lee_Dailey Nov 03 '20 at 11:53
  • What is `Convert-OutputForCSV` ? – Theo Nov 03 '20 at 11:56
  • General advice: [I avoid using the increase assignment operator (+=) to create a collection](https://stackoverflow.com/questions/60708578/why-should-i-avoid-using-the-increase-assignment-operator-to-create-a-colle/60708579?r=SearchResults&s=3|13.7702#60708579) as it is exponential expensive. – iRon Nov 03 '20 at 12:16

4 Answers4

0

You might want to use the Out-String cmdlet for this:

$vlr=@{}
$vlr["OS"]=,@("test","test2") | Out-String
$Object = New-Object PSObject -Property $vlr
$Object | ConvertTo-Csv

"OS"
"test
test2
"
iRon
  • 20,463
  • 10
  • 53
  • 79
0

this solution does not work because in the case where $vlr with several names the extraction will be complicated

$vlr=@{}

$vlr["OS"]=,@("test","test2")

$vlr["PS"]=,@("lous","tique")

it's a problem

https://gallery.technet.microsoft.com/scriptcenter/Convert-OutoutForCSV-6e552fc6 For the function Convert-OutputForCSV

0

I don't know what the posted function does, but you can make your own function to handle a single-key or multi-key hash table provided all of the key value counts are the same.

function Convert-OutputForCsv {
    param(
    [parameter(ValueFromPipeline)]
    [hashtable]$hash
    )

    # Array of custom object property names
    $keys = [array]$hash.Keys

    # Loop through each key's values
    for ($i = 0; $i -lt $hash.($keys[0]).count; $i++) {
        # Custom object with keys as properties. Property values are empty.
        $obj = "" | Select $keys
        # Loop through key names
        for ($j = 0; $j -lt $keys.Count; $j++) {
            $obj.($keys[$j]) = $hash.($Keys[$j])[$i]
        }
        $obj
    }
}

$vlr=[ordered]@{}
$vlr["OS"]='test','test2'
$vlr["PS"]='lous','tique'
$vlr | Convert-OutputForCsv | Export-Csv -NoTypeInformation -Delimiter ";" -Path $filepath

Honestly, if you are in control of the input data, I would just type out a CSV instead of typing out hash tables.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
0

this solution is good in my simplified case but not adapted to my case unfortunately I'm merging my old base2 array with my new base array and my goal is to concatenate the values ​​in an excel to make them usable

  $base2 = Get-content $filepath2 | select -first 1 
$base2 = $base2 -split ";" 
$base2 = $base2.Replace("`"", "") 
$cunt2 = $base2.count - 1  
$h2 = ipcsv $filepath2 -Delimiter ";" 
$HashTable2 = @{}
for ($i = 0 ; $i -le $cunt2 ; $i++) { 
    foreach ($r in $h2) { 
        $HashTable2[$base2[$i]] = $r.($base2[$i]) 
    }

base2 = old tables

  $base = Get-content $filepath2 | select -first 1 
$base = $base -split ";" 
$base = $base.Replace("`"", "") 
$cunt = $base.count - 1  
$h1 = ipcsv $filepath -Delimiter ";" 
$HashTable = @{}
for ($i = 0 ; $i -le $cunt ; $i++) { 
    foreach ($r in $h1) { 
        $HashTable[$base[$i]] = $r.($base[$i]) 
    }

New tables $base

once the two arrays are initialized, I merge them and this is where I have to separate the values ​​row by row

$csvfinal = $hashtable, $hashtable2 | Merge-Hashtables