我试图将 .xml 文件导出为 .csv 文件(将多个 .xml 文件导出为一个 .csv 文件)这是我的工作代码:
$path = "C:\Temp\Cert\"
$FileLogdate = Get-Date -format 'dd_MM_yyyy'
$exportpath = "C:\Temp\Cert\$FileLogdate-sdf.csv"
Get-ChildItem $path -filter *.xml |
ForEach-Object {
[xml]$empDetails = Get-Content $_.Fullname
$empDetails.Objs.Obj |
% {
[pscustomobject] @{
"Client" = $_.ms.s.innertext
"CertificateExpire" = $_.ms.dt.innertext
}
} |
ConvertTo-Csv -NoTypeInformation
}
现在我遇到的问题是,客户端和证书中可以有多个这样的条目:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>Selected.System.Security.Cryptography.X509Certificates.X509Certificate2</T>
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<DT N="NotAfter">2022-12-31T08:21:10+01:00</DT>
<S N="Subject">Client 01</S>
<S N="Issuer">DOMAIN</S>
</MS>
</Obj>
<Obj RefId="1">
<TNRef RefId="0" />
<MS>
<DT N="NotAfter">2022-12-31T08:21:10+01:00</DT>
<S N="Subject">Client 01</S>
<S N="Issuer">DOMAIN</S>
</MS>
</Obj>
</Objs>
我该如何解决输出不一样的情况:
"Client 01 Client 01", "Client 01 Client 01", "2022-12-31T08:21:10+01:00 2022-12-31T08:21:10+01:00"
答案1
$path = "\\Server"
$FileLogdate = Get-Date -format 'dd_MM_yyyy'
$exportpath = "C:\Temp\Cert\$FileLogdate-sddf.csv"
Get-ChildItem $path -filter *.xml |
ForEach-Object {
[xml]$empDetails = Get-Content $_.Fullname
$empDetails.Objs.Obj |
% {
[pscustomobject] @{
"Client" = $_.ms.s | ?{$_.n -eq 'Subject'} | %{$_.innerText}
"Issuer" = $_.ms.s | ?{$_.n -eq 'Issuer'} | %{$_.innerText}
"CertificateExpire" = $_.ms.dt | ?{$_.n -eq 'NotAfter'} | %{$_.innerText}
}
} | Export-CSV -Append -Path $exportpath -NoTypeInformation
}