How do I convert repeated xml nodes into a comma delimited string using powershell

前端 未结 1 1018
长情又很酷
长情又很酷 2021-01-24 13:14

I have some 13000 log files that are formatted in XML format and I need to convert all of them into a spreadsheet\\csv file.

As you will see I\'m not programmer but I\'v

相关标签:
1条回答
  • 2021-01-24 14:08

    Powershell has native support for XML, maybe this will help get you started?

    It also has a native CSV Exporter with Export-Csv :)

    [xml]$XMLfile = gc C:\Temp\migration.xml
    
    $MasterArray = @()
    $MasterArray = "" | Select User, Result, TotalEmails, SuccessfulEmails, FailedEmails, Failures
    
    $MasterArray.User = $XMLfile.MigrationUserStatus.user
    $MasterArray.Result = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.MigrationStatus.value
    $MasterArray.TotalEmails = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.TotalCount.value
    $MasterArray.SuccessfulEmails = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.SuccessCount.value
    $MasterArray.FailedEmails = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.FailCount.value
    
    $Failures = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.FailedMessages
    $ConcatFailures = @()
    foreach ($Failure in $Failures)
    {
        $ConcatFailures += $Failure.ErrorMessage + "," + $Failure.SentTime + "," + $Failure.ReceivedTime
    }
    
    $MasterArray.Failures = $ConcatFailures -Join "|"
    $MasterArray
    $MasterArray | Export-Csv -NoType "C:\Temp\export.csv"
    

    For the other fields, you can check if they exist and add them if they do pretty easily, this should work:

    foreach ($Failure in $Failures)
    {
        if ($Failure.ErrorMessage) { $ConcatFailures += $Failure.ErrorMessage }
        if ($Failure.SentTime) { $ConcatFailures += $Failure.ErrorMessage }
        if ($Failure.ReceivedTime) { $ConcatFailures += $Failure.ReceivedTime }
        if ($Failure.MessageSubject) { $ConcatFailures += $Failure.MessageSubject }
        if ($Failure.MessageSize) { $ConcatFailures += $Failure.MessageSize }
    }
    

    To handle the xml files you want to add an outer loop to go through all of the xml files, and then append the data into an array that you build up as you go. This should do what you want, with some adjustments to the paths used:

    $XMLFiles = gci "C:\Temp\" -Filter "*.xml"
    $MasterArray = @()
    
    foreach ($XMLFile in $XMLFiles)
    {
        [xml]$XMLfile = gc $XMLFile.FullName
    
        $TempArray = @()
        $TempArray = "" | Select User, Result, TotalEmails, SuccessfulEmails, FailedEmails, Failures
    
        $TempArray.User = $XMLfile.MigrationUserStatus.user
        $TempArray.Result = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.MigrationStatus.value
        $TempArray.TotalEmails = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.TotalCount.value
        $TempArray.SuccessfulEmails = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.SuccessCount.value
        $TempArray.FailedEmails = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.FailCount.value
    
        $Failures = $XMLfile.MigrationUserStatus.StoreList.EmailMigrationStatus.FolderList.FailedMessages
        $ConcatFailures = @()
    
        foreach ($Failure in $Failures)
        {
            if ($Failure.ErrorMessage) { $ConcatFailures += $Failure.ErrorMessage }
            if ($Failure.SentTime) { $ConcatFailures += $Failure.ErrorMessage }
            if ($Failure.ReceivedTime) { $ConcatFailures += $Failure.ReceivedTime }
            if ($Failure.MessageSubject) { $ConcatFailures += $Failure.MessageSubject }
            if ($Failure.MessageSize) { $ConcatFailures += $Failure.MessageSize }
        }
        $TempArray.Failures = $ConcatFailures -Join "|"
    
        $MasterArray += $TempArray
    }
    
    $MasterArray
    $MasterArray | Export-Csv -NoType "C:\Temp\export.csv"
    
    0 讨论(0)
提交回复
热议问题