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
1

1 Answer 1

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",""
Sign up to request clarification or add additional context in comments.

3 Comments

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 ...
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
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.