0

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 = "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
     }

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

How can i solve that the output is not like:

"Client 01 Client 01", "Client 01 Client 01", "2022-12-31T08:21:10+01:00 2022-12-31T08:21:10+01:00"

1 Answer 1

0
$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 
     }
2
  • 1
    Code without any explanation is useless. Can you elaborate on this a little more?
    – Toto
    Commented Sep 20, 2022 at 9:15
  • Good solution, but indeed, without explaining why, I can't upvote this answer.
    – LPChip
    Commented Sep 20, 2022 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .