0

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
Empty Coder
  • 589
  • 6
  • 19
  • Using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [Different length arrays to one CSV](https://stackoverflow.com/a/73644637/1701026)): `$Cipher |FulJoin $Hashes |FullJoin $Keys` – iRon Sep 23 '22 at 14:42

1 Answers1

2

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",""
mclayton
  • 8,025
  • 2
  • 21
  • 26
  • Thanks for your response. I tried your code but getting error `The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property. At E:\VMBuild\IISCrypto.ps1:78 char:9 + "KeyExchangeAlgorithms" = if( $_ -lt $KeyTable.Length ) { $Ke ...` – Empty Coder Sep 23 '22 at 07:52
  • Fixed the above issue. `;` was missing at the end of `Ciphers }.....`. but while writing it to csv file, it is writing numeric values instead of the data. which is fixed by writing the `$data` itself. Thanks again for help – Empty Coder Sep 23 '22 at 08:10
  • 1
    Ah, yeah. That's a compatibility issue between Windows PowerShell and PowerShell Core. I've fixed the code to work in WIndows PowerShell as well as PowerShell Core now... – mclayton Sep 23 '22 at 08:44