【问题标题】:Format CSV file output using powershell使用 powershell 格式化 CSV 文件输出
【发布时间】:2019-05-19 17:23:41
【问题描述】:

我编写了以下代码,将证书详细信息存储在 csv 文件中。它可以工作,但在 csv 文件的每一行中,开头的符号 @{ 和结尾的 } 都被写入。在包含标题的第一列中,符号 = 也被写入。所以我不知道如何在没有这些符号的情况下填充我的 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 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'

【问题讨论】:

  • $obj 是干什么用的?
  • [1] 删除整个$obj 部分和以$obj | Add-Content 开头的行。 [2] 将$CertsDetail | 行更改为使用Export-CSV 而不是Add-Content。 [3] 阅读来自Get-Help Export-CSV 的帮助... [grin]

标签: powershell csv


【解决方案1】:

试试这样的:

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

【讨论】:

  • 它可以工作,但还有两件事:1-> #TYPE System.Management.Automation.PSCustomObject 出现在文件的头部 2-> 以隐藏格式写入的过期日期(#### ##) 另外我想知道如何使列的大小适合内容
  • 要摆脱#TYPE System.Management.Automation.PSCustomObject,只需将-NoTypeInformation 添加到您的Export-Csv,如下所示:Export-Csv -Path 'C:\Users\hanna\Desktop\certificate.csv' -NoTypeInformation
  • 如果您调整列的大小(假设您在 Excel 中查看 CSV),是否允许您查看到期日期?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 2018-11-29
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2017-08-20
相关资源
最近更新 更多