【问题标题】:write multiple arrays as columns in csv using powershell使用powershell将多个数组写入csv中的列
【发布时间】:2022-09-23 16:43:18
【问题描述】:

我有下面的代码,在其中我在不同的数组中获取所需的数据。该数组具有列标题和与之关联的值

$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
   }
   }
   }
   }

}

上面的每个数组都有如下数据

$CiphersTable=@()

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

$HashesTable=@()

Hashes
------
SHA256
SHA384
SHA512

$KeyTable=@()

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

请让我知道如何将这些数据写入 csv 中的单独列。 需要在 csv 文件中如下所示的输出

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

    标签: powershell


    【解决方案1】:

    我不太确定为什么你会想要这样的数据,因为它不是真正的 csv,但这里有......

    # 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",""
    

    【讨论】:

    • 感谢您的答复。我试过你的代码,但收到错误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 ...
    • 修复了上述问题。 ;Ciphers }..... 的末尾丢失。但是在将其写入 csv 文件时,它正在写入数值而不是数据。这是通过编写$data 本身来解决的。再次感谢您的帮助
    • 啊,是的。这是 Windows PowerShell 和 PowerShell Core 之间的兼容性问题。我已经修复了代码,现在可以在 WIndows PowerShell 和 PowerShell Core 中工作......
    猜你喜欢
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2013-10-16
    • 2012-07-24
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多