[英]Powershell - Export Certificate Info
I have a list of .cer and .der files which I need to note the expiry dates for.我有一个 .cer 和 .der 文件列表,我需要记下它们的到期日期。 I have the following which appears to give the expiry dates but when I add the export-csv command it doesn't export the data:
我有以下内容似乎给出了到期日期,但是当我添加 export-csv 命令时,它不会导出数据:
gci C:\test1 -Recurse -Filter *.cer |
%{
$cert = New-Object Security.Cryptography.X509Certificates.X509Certificate2 $_.FullName
if ($cert.NotBefore -lt (Get-Date))
{
Write-Output $cert.FriendlyName
Write-Output $cert.SubjectName
Write-Output $cert.notbefore
Write-Output $cert.NotAfter
}
}|export-csv c:\test2\test.csv -Append
If I remove the last section then I get the information appear on screen, but when it exports to CSV I just get a #type.system.string followed by a column of '0'.如果我删除最后一部分,那么我会在屏幕上显示信息,但是当它导出到 CSV 时,我只会得到一个 #type.system.string 后跟一列“0”。 Does anyone have any ideas here?
有没有人在这里有任何想法? I am aware that the above will only give the '.cer' files and I'll need to find the '.der' files too but I'll try and cross that bridge when I come to it :D
我知道上面只会给出“.cer”文件,我也需要找到“.der”文件,但是当我遇到它时,我会尝试越过那座桥:D
Instead of doing Write-Output
, create a PSCustomObject
and then export to CSV.不是执行
Write-Output
,而是创建一个PSCustomObject
然后导出到 CSV。 Note the change to your if
block below:请注意以下
if
块的更改:
gci C:\test1 -Recurse -Filter *.cer |
%{
$cert = New-Object Security.Cryptography.X509Certificates.X509Certificate2 $_.FullName
if ($cert.NotBefore -lt (Get-Date))
{
[PSCustomObject]@{
FriendlyName = $cert.FriendlyName
SubjectName = $cert.SubjectName
NotBefore = $cert.notbefore
NotAfter = $cert.NotAfter
}
}
}|export-csv c:\test2\test.csv -NoTypeInformation -Append -Encoding UTF8
This works because the PSCustomObject
only has the NoteProperty
properties you added to it.这是有效的,因为
PSCustomObject
只有您添加到其中的NoteProperty
属性。 If you had tried this with a plain HashTable
your CSV would be full of properties that exist on a HashTable
object, and isn't what you want.如果您使用普通的
HashTable
尝试过此操作,您的 CSV 将充满HashTable
对象上存在的属性,这不是您想要的。
Now you have a common set of column names, and each returned PSCustomObject
through the pipeline is a row in your CSV.现在您有了一组通用的列名,每个通过管道返回的
PSCustomObject
都是 CSV 中的一行。 I've added -NoTypeInformation
to Export-Csv
so it doesn't add a comment on top of the PowerShell type it was converted from.我已将
-NoTypeInformation
添加到Export-Csv
因此它不会在转换自的 PowerShell 类型之上添加注释。 In most cases you don't need this and it can confuse some parsers.在大多数情况下,您不需要它,它可能会混淆一些解析器。 Specifying the UTF8 encoding helps a bit with compatibility for some third-party parsers (see the end of this answer for more information), but isn't strictly necessary.
指定 UTF8 编码有助于某些第三方解析器的兼容性(有关更多信息,请参阅此答案的结尾),但并非绝对必要。
If you don't want headers, that is a little trickier, but still fairly simple.如果你不想要标题,那有点棘手,但仍然相当简单。
Export-Csv
doesn't support exporting without headers. Export-Csv
不支持没有标题的导出。 You have to first convert the object to CSV format, skip the first line, and then write the rest of the output to the file yourself.您必须先将对象转换为 CSV 格式,跳过第一行,然后自己将其余的输出写入文件。 So in this case, you would do the following after your
Where-Object
block:因此,在这种情况下,您将在
Where-Object
块之后执行以下操作:
} | ConvertTo-Csv -NoTypeInformation | Select -Skip 1 | Set-Content -Encoding UTF8 c:\test2\test.csv
Prior to PowerShell 6, writing files from PowerShell using the redirection operator or Set-Content
with any encoding will include a Byte-Order Mark (BOM) at the start of the file.在 PowerShell 6 之前,使用重定向运算符或
Set-Content
以任何编码从 PowerShell 写入文件将在文件开头包含字节顺序标记 (BOM)。 I believe Export-Csv
also does this.我相信
Export-Csv
也Export-Csv
。 You won't see this in most editors and it usually won't cause issues with Windows software, but there are some CSV parsers that will get confused by a BOM.您不会在大多数编辑器中看到它,它通常不会导致 Windows 软件出现问题,但有一些 CSV 解析器会被 BOM 混淆。
Using the UTF8 encoding helps somewhat since UTF8 has wider support than PowerShell's default UTF16, but if you're using PowerShell 5.1 or earlier consider writing the file out by using the System.IO.File.WriteAllText
to specify a BOM-less encoding.使用 UTF8 编码会有所帮助,因为 UTF8 比 PowerShell 的默认 UTF16 具有更广泛的支持,但如果您使用的是 PowerShell 5.1 或更早版本,请考虑使用
System.IO.File.WriteAllText
来指定无 BOM 编码的文件。 Where $csvString
is the CSV formatted string you want to write to a file:其中
$csvString
是您要写入文件的 CSV 格式字符串:
$utf8NoBOM = New-Object System.Text.UTF8Encoding( $False )
[System.IO.File]::WriteAllText( $csvString, "C:\path\to\file.csv", $utf8NoBOM )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.