Regex 证书颁发者替换为CN或OU,System.Collections.Hashtable+;ValueCollection CSV错误
比如说,Regex 证书颁发者替换为CN或OU,System.Collections.Hashtable+;ValueCollection CSV错误,regex,powershell,certificate,Regex,Powershell,Certificate,比如说, <!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } </style> </head> <body> <table style="width:100%"> <tr> <th>...<
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>...</th>
<th>Issuer</th>
<th>...</th>
</tr>
<tr>
<td>...</td>
<td>System.Collections.Hashtable+ValueCollection
</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>System.Collections.Hashtable+ValueCollection
</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>System.Collections.Hashtable+ValueCollection
</td>
<td>...</td>
</tr>
</table>
</body>
</html>
CN=Microsoft根证书颁发机构2010,O=Microsoft Corporation,L=Redmond,S=Washington,C=US
CN=Avast Web/Mail Shield根目录,O=Avast Web/Mail Shield,OU=由Avast Antivirus为SSL/TLS扫描生成
OU=版权所有(c)1997微软公司,OU=微软时间戳服务根目录,OU=微软公司,O=微软信任网络
应该回来
CN=Microsoft Root Certificate Authority 2010, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
CN=Avast Web/Mail Shield Root, O=Avast Web/Mail Shield, OU=generated by Avast Antivirus for SSL/TLS scanning
OU=Copyright (c) 1997 Microsoft Corp., OU=Microsoft Time Stamping Service Root, OU=Microsoft Corporation, O=Microsoft Trust Network
Microsoft根证书颁发机构2010
Avast Web/Mail屏蔽根目录
版权所有(c)1997年微软公司。
但现在我有一个问题,即发行人在找到正确的匹配项后没有保存到CSV文件。找到匹配项后,我的CSV文件中的Issuer列将填写“System.Collections.Hashtable+ValueCollection”。有人能告诉我我做错了什么吗?多亏了Wiktor+iRon,我才能够回答我的问题。这就解决了OU和CN的问题,并且能够使用匹配的OU和CN颁发者写入csv文件。这是工作的完整脚本。:)
cls
$array=@()
获取子项-递归|
foreach对象({
$obj=新对象-类型名称PSObject
$obj |添加成员-成员类型NoteProperty-名称“PSPath”-值$\ PSPath
$obj |添加成员-成员类型NoteProperty-名称“FriendlyName”-值$\ FriendlyName
如果($\u.Issuer-match((?您需要的正则表达式实际上是一个模式,与字符串开头的OU=
或CN=
后的非逗号块相匹配(默认情况下,Powershell逐行处理文件,因此其行为类似于行的开头):
详细信息
(?m)
-多行模式打开,启用^
以匹配行位置的起点
(?在推出任何复杂的解决方案之前,为什么不试试(?在@123testing123上似乎不起作用。您需要在regex101Ah上启用全局和多行标志。谢谢!抱歉,这是我第一次使用该网站。现在我的问题是试图将此逻辑添加到数组中,以便将其保存到csv文件。到目前为止,我有($uu.Issuer-Match)(?它,您没有在regex101站点上使用正确的修饰符。为什么要在regex测试站点上尝试?请在您拥有的代码中尝试。regex101无论如何都不支持.NET regex。
Microsoft Root Certificate Authority 2010
Avast Web/Mail Shield Root
Copyright (c) 1997 Microsoft Corp.
cls
$array = @()
Get-ChildItem -recurse |
foreach-object ({
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name "PSPath" -Value $_.PSPath
$obj | Add-Member -MemberType NoteProperty -Name "FriendlyName" -Value $_.FriendlyName
if( $_.Issuer -match ("(?<=^(?:CN|OU)=)[^,\r\n]+")) {
$matches.values
}
$Obj | Add-Member -MemberType NoteProperty -Name "Issuer" -Value "$($matches.values)"
$obj | Add-Member -MemberType NoteProperty -Name "NotAfter" -Value $_.NotAfter
$obj | Add-Member -MemberType NoteProperty -Name "NotBefore" -Value $_.NotBefore
$obj | Add-Member -MemberType NoteProperty -Name "SerialNumber" -Value $_.SerialNumber
$obj | Add-Member -MemberType NoteProperty -Name "Thumbprint" -Value $_.Thumbprint
$obj | Add-Member -MemberType NoteProperty -Name "DnsNameList" -Value $_.DnsNameList
$obj | Add-Member -MemberType NoteProperty -Name "Subject" -Value $_.Subject
$obj | Add-Member -MemberType NoteProperty -Name "Version" -Value $_.Version
$array += $obj
$obj = $null
$matches.values = $null
})
$array | Export-Csv -Path "c:\temp\temp4.csv"
(?<=^(?:CN|OU)=)[^,]+
(?m)(?<=^(?:CN|OU)=)[^,\r\n]+