使用powershell格式化CSV文件输出 [英] Format CSV file output using powershell

查看:55
本文介绍了使用powershell格式化CSV文件输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码,将证书详细信息存储在一个csv文件中.它可以工作,但是在csv文件的每一行中都在开头写了符号 @ {,在结尾写了} .还写了 = .所以我不知道没有这些符号怎么填充我的csv文件

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

这是我编写的代码

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

推荐答案

尝试如下操作:

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

这篇关于使用powershell格式化CSV文件输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆