Format CSV file output using powershell

痞子三分冷 提交于 2020-12-15 06:43:56

问题


I wrote the following code that stores certificates details in a csv file. It works but in each line of the csv file the symbols @{ at the beginning and } at the end are written.also in the first column which contains the headers the symbol = is also written. So I did not know how populate my csv file without these symbols

Here is the code that I wrote

$StartDate = Get-Date
 $CertPath = 'Cert:\LocalMachine\'

  $CertsDetail =  Get-ChildItem -Path $CertPath -Recurse | Where-Object {$_.PsIsContainer -ne $true } | ForEach-Object {                               
   $DaysLeft = (New-TimeSpan -Start $StartDate -End $_.NotAfter).Days
   If ($DaysLeft -lt 30) {
    $Under30 = $true
    $Text = "The Certificate is but valid about to expire"
}
Else {
    $Under30 = $false
}
If ($DaysLeft -lt 1) {
    $Expired = $true
    #$Not_Expired = $false
    $Text = "The Certificate is expired"
}
Else {
    $Expired = $false
    #$Not_Expired = $true
    $Text = "The Certificate is still valid and not going soon to expire"
}
[pscustomobject]@{Text=$Text;`
                Subject = $_.Subject;`
                ExpireDate = $_.NotAfter;`
                DaysRemaining = $DaysLeft;`
                Under30Days = $Under30;`
                Expired = $Expired;`
                #Not_Expired = $Not_Expired
                }

                }
         $obj = [PSCustomObject] @{
         'Example Header 1' = $null
         'Example Header 2' = $null
         'Example Header 3' = $null
         'Example Header 4' = $null 
         'Example Header 5' = $null 
         'Example Header 6' = $null 


$obj | Add-Content -Path 'C:\Users\hanna\Desktop\certificate.csv'
$CertsDetail | Add-Content -Path 'C:\Users\hanna\Desktop\certificate.csv'

回答1:


Try something like this:

$StartDate = Get-Date
$CertPath = 'Cert:\LocalMachine\'
$CertsDetail = Get-ChildItem -Path $CertPath -Recurse | 
    Where-Object { $_.PsIsContainer -ne $true } | ForEach-Object {                               
    $DaysLeft = (New-TimeSpan -Start $StartDate -End $_.NotAfter).Days
    if ($DaysLeft -lt 1) {
        $Under30 = $true
        $Expired = $true
        $Text = "The Certificate is expired"
    }
    elseif ($DaysLeft -lt 30) {
        $Under30 = $true
        $Expired = $false
        $Text = "The Certificate is but valid about to expire"
    }
    else {
        $Under30 = $false
        $Expired = $false
        $Text = "The Certificate is still valid and not going soon to expire"
    }
    [PSCustomObject]@{
        Text = $Text
        Subject = $_.Subject
        ExpireDate = $_.NotAfter
        DaysRemaining = $DaysLeft
        Under30Days = $Under30
        Expired = $Expired
    }
}
$CertsDetail | Export-Csv -Path 'C:\Users\hanna\Desktop\certificate.csv'


来源:https://stackoverflow.com/questions/56210279/format-csv-file-output-using-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!