Home > Net >  write multiple arrays as columns in csv using powershell
write multiple arrays as columns in csv using powershell

Time:09-23

I am having the below code in which I am getting required data in different arrays. the array have the column header and the values associated with it

$GetEnabledControls=@()
$CiphersTable=@()
$HashesTable=@()
$KeyTable=@()
$ServersTable=@()
$ClientsTable=@()

$GetEnabledControls = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL" -Recurse | ForEach-Object {
   Get-ItemProperty $_.PSPath | Where-Object { $_.Enabled -ne '0' }
}

foreach($entry in $GetEnabledControls)
{    
    
    $PSPath =$entry.PSPath
    $regex = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\"
    $ShortPath = $PSPath.Replace($regex,"")
    $SplitShortPath = $ShortPath.Split("\")
    $type = $SplitShortPath[0]
    $value = $SplitShortPath[1]

    if($type -eq 'Ciphers'){
    $CiphersTable  = [PSCustomObject]@{
    $type = $value
   }
   }
    if($type -eq 'Hashes'){
    $HashesTable  = [PSCustomObject]@{
    $type = $value
   }
   }
    if($type -eq 'KeyExchangeAlgorithms'){
    $KeyTable  = [PSCustomObject]@{
    $type = $value
   }
   }       
    if($type -eq 'Protocols'){

    $GetChild = $entry.PSChildName
    if($GetChild -eq 'Server'){
    $type='Server Protocols'
    $ServersTable  = [PSCustomObject]@{
    $type = $value
    }
    }
    else
    {
    if($GetChild -eq 'Client'){
    $type='Client Protocols'
    $ClientsTable  = [PSCustomObject]@{
    $type = $value
   }
   }
   }
   }

}

each of the arrays above is having data like below

$CiphersTable=@()

Ciphers    
-------    
AES 128/128
AES 256/256

$HashesTable=@()

Hashes
------
SHA256
SHA384
SHA512

$KeyTable=@()

KeyExchangeAlgorithms
---------------------
ECDH                 
PKCS 

Please let me know how can I write this data as separate columns in csv. need output like below in csv file

Ciphers      Hashes  KeyExchangeAlgorithms
AES 128/128  SHA512  ECDH
AES 256/256  SHA384  PKCS
             SHA512

CodePudding user response:

I'm not quite sure why you'd want the data like that as it's not really a csv, but here goes...

# set up some test data

$CiphersTable = @(
    [pscustomobject] [ordered] @{ "Ciphers" = "AES 128/128" },
    [pscustomobject] [ordered] @{ "Ciphers" = "AES 256/256" }
)

$HashesTable = @(
    [pscustomobject] [ordered] @{ "Hashes" = "SHA256" },
    [pscustomobject] [ordered] @{ "Hashes" = "SHA384" },
    [pscustomobject] [ordered] @{ "Hashes" = "SHA512" }
)

$KeyTable = @(
    [pscustomobject] [ordered] @{ "KeyExchangeAlgorithms" = "ECDH" },
    [pscustomobject] [ordered] @{ "KeyExchangeAlgorithms" = "PKCS" }
)

# convert it to the desired structure

$max = @( $CiphersTable.Length, $HashesTable.Length, $KeyTable.length ) | sort-object | Select-Object -last 1
$data = 0..($max-1) | foreach {
    [pscustomobject] [ordered] @{
        "Ciphers" = if( $_ -lt $CiphersTable.Length ) { $CiphersTable[$_].Ciphers };
        "Hashes"  = if( $_ -lt $HashesTable.Length ) { $HashesTable[$_].Hashes };
        "KeyExchangeAlgorithms" = if( $_ -lt $KeyTable.Length ) { $KeyTable[$_].KeyExchangeAlgorithms }
    }
}

$data | Format-Table

# Ciphers     Hashes  KeyExchangeAlgorithms
# -------     ------- ---------------------
# AES 128/128 SHA256  ECDH
# AES 256/256 SHA384  PKCS
#             SHA512

# convert it to csv if you want to write it to a file

$csv = $data | ConvertTo-Csv -NoTypeInformation
$csv

# "Ciphers","Hashes","KeyExchangeAlgorithms"
# "AES 128/128","SHA256","ECDH"
# "AES 256/256","SHA384","PKCS"
# "","SHA512",""
  • Related