I was trying to Export a .xml-File to a .csv-File (multiple .xml-Files to one .csv-File) This is my working code:
$path = "\\stos-matrix-srv\ablage$\zert_check"
$FileLogdate = Get-Date -format 'dd_MM_yyyy'
$exportpath = "C:\Temp\Cert\$FileLogdate-expordt.csv"
Get-ChildItem $path -filter *.xml |
ForEach-Object {
[xml]$empDetails = Get-Content $_.Fullname
[pscustomobject]@{
'Client' = [string]$empDetails.Objs.Obj.MS.S.innerText;
'CertificateExpire' = [string]$empDetails.Objs.Obj.MS.DT.InnerText;
} | Export-CSV -Append -Path $exportpath -NoTypeInformation
}
Now I have the problem, that there can be multiple entrys at Client and Cerificate like that:
<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>
</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>
</MS>
</Obj>
</Objs>
How can i solve that the output is not like:
"Client01 Client 01", "2022-12-31T08:21:10 01:00 2022-12-31T08:21:10 01:00"
CodePudding user response:
Iterate over the objects, e.g.:
$empDetails.Objs.Obj |
% {
[pscustomobject] @{
"Client" = $_.ms.s.innertext
"CertificateExpire" = $_.ms.dt.innertext
}
} |
ConvertTo-Csv -NoTypeInformation
Output:
"Client","CertificateExpire"
"Client 01","2022-12-31T08:21:10 01:00"
"Client 01","2022-12-31T08:21:10 01:00"