简体   繁体   English

使用 powershell 将 soap 日志文件转换为 csv

[英]convert soap log file to csv using powershell

I have been looking for a good way to convert a log file using powershell into a csv so I can import data correctly into Teradata.我一直在寻找一种将使用 powershell 的日志文件转换为 csv 的好方法,这样我就可以将数据正确导入 Teradata。 I have never seen a file like this before so I am going to try to explain as best as I can.我以前从未见过这样的文件,所以我将尽我所能解释。

Here is a sample from the logfile这是来自日志文件的示例

 [XXXXX:11aa11a-123-12a4-12a3-12323aabb123:4] 2021-02-05 00:00:00,000: [DEBUG] Somesystem::Request: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <ctx:App xmlns:ctx="http://namespace"> <ctx:Session> <ctx:UN>XXXXX</ctx:UN> <ctx:SId>11aa11a-123-12a4-12a3-12323aabb123</ctx:SId> <ctx:Creation>2021-02-05T00:00:00+11:00</ctx:Creation> </ctx:Session> <ctx:IC> <ctx:TId>11aa1aa-1aa1-2a22-3aa1-aa1b1233456</ctx:TId> <ctx:Call> <ctx:BI>13</ctx:BI> <ctx:CN>Somesystem</ctx:CN> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BI> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BI> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BI> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BI> <ctx:CN>Somesystem</ctx:CN> <ctx:CL>UnknownLocation</ctx:CL> </ctx:Call> <ctx:Call> <ctx:BI>0</ctx:BI> </ctx:Call> </ctx:IC> </ctx:App> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UT> <wsse:UN>SomesystemUSR</wsse:UN> <wsse:P>Somepassword</wsse:P> </wsse:UT> </wsse:Security> </soapenv:Header> <soapenv:Body> <ns5:ListRequest xmlns:ns5="http://Namespace/Service/V1.0" xmlns="http://www.immi.gov.au/Namespace/Enterprise/ErrorMessages/V1.0" xmlns:ns6="http://Namespace/Core/V1.0" xmlns:ns8="http://Namespace/Service/V1.0" xmlns:ns7="http://Namespace/Core/V1.0" xmlns:ns9="http://Namespace/Core/V1.0" xmlns:ns10="http://Namespace/Core/Messaging/V1.0" xmlns:ns2="http://Namespace/Enterprise/V1.0" xmlns:ns4="http://Namespace/Core/V1.0" xmlns:ns3="http://Namespace/WarningMessages/V1.0"> <ns4:FromDate>2021-01-28</ns4:FromDate> <ns4:ToDate>2021-01-28</ns4:ToDate> <ns4:Code>0123</ns4:Code> <ns4:Type>U</ns4:Type> <ns4:Record>S</ns4:Record> <ns4:AnotherCode>D</ns4:AnotherCode> </ns5:ListRequest> </soapenv:Body> </soapenv:Envelope> (jaxws.LoggingJaxWsHandler) [WContainer: 0] [XXXXX:11aa11a-123-12a4-12a3-12323aabb123BB:4] 2021-02-05 01:00:00,000: [DEBUG] Somesystem::Request: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <ctx:App xmlns:ctx="http://namespace"> <ctx:Session> <ctx:UN>XXXXX</ctx:UN> <ctx:SId>11aa11a-123-12a4-12a3-12323aabb123BB</ctx:SId> <ctx:Creation>2021-02-05T01:00:00+11:00</ctx:Creation> </ctx:Session> <ctx:IC> <ctx:TId>1as23bb3-1er2-234d-234e-bb8b20995147</ctx:TId> <ctx:Call> <ctx:BI>25</ctx:BI> <ctx:CN>Somesystem</ctx:CN> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BranchIndex> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BI> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BI> </ctx:Call> <ctx:Call> <ctx:BI>1</ctx:BI> <ctx:CN>Somesystem</ctx:CN> <ctx:CL>UnknownLocation</ctx:CL> </ctx:Call> <ctx:Call> <ctx:BI>0</ctx:BI> </ctx:Call> </ctx:IC> </ctx:App> <wsse:Security xmlns:wsse="http://xt-1.0.xsd"> <wsse:UT> <wsse:UN>SomeUSR</wsse:UN> </wsse:UT> </wsse:Security> </soapenv:Header> <soapenv:Body> <s:ListResponse xmlns:s="http://Namespace/Service/V1.0" xmlns:t="http://Namespace/Core/V1.0" xmlns:m="http://Namespace/Core/Messaging/V1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://Namespace/Service/V1.0..\Service\V1.0\Response.xsd"> <s:List> <s:FL> <t:lDate>2021-01-05</t:lDate> <t:Code>N</t:Code> <t:lID>AB123</t:lID> </s:FL> <s:FL> <t:lDate>2021-01-05</t:lDate> <t:Code>G</t:Code> <t:lID>CD456</t:lID> </s:FL> <s:FL> <t:Date>2021-01-05</t:Date> <t:Code>W</t:Code> <t:lID>EF654</t:lID> </s:FL> <s:FL> <t:Date>2021-01-05</t:Date> <t:Code>P</t:Code> <t:lID>TR123</t:lID> </s:FL> <s:FL> <t:lDate>2021-01-05</t:lDate> <t:Code>N</t:Code> <t:lID>AB123</t:lID> </s:FL> <s:FL> <t:lDate>2021-01-05</t:lDate> <t:Code>N</t:Code> <t:lID>AB123</t:lID> </s:FL> <s:FL> <t:Date>2021-01-05</t:Date> <t:Code>M</t:Code> <t:lID>AB345</t:lID> </s:FL> <s:FL> <t:Date>2021-01-05</t:Date> <t:Code>C</t:Code> <t:lID>AB456</t:lID> </s:FL> </s:List> </s:ListResponse> </soapenv:Body> </soapenv:Envelope> (jaxws.LoggingJaxWsHandler) [WContainer: 3]

I want to be able to extract data in a format that is easy to import into Teradata for example例如,我希望能够以易于导入 Teradata 的格式提取数据

UN, SId, Creation, TId, BI, CN, FromDate, ToDate, Code, Type, Record, AnotherCode, FL, Ldate, Iid,...... so-on UN, SId, Creation, TId, BI, CN, FromDate, ToDate, Code, Type, Record, AnotherCode, FL, Ldate, Iid,......等等

I am open to any suggestions as to having a better way of getting a flat structure of out this soap tree.我愿意接受任何关于有更好的方法来获得这个 soap 树的平面结构的建议。 As you can see the second SoapEnvelope has a lot more data in soapBody than the first one.如您所见,第二个 SoapEnvelope 在soapBody 中的数据比第一个要多得多。 I am unable to find any question similar to this one.我找不到任何与此类似的问题。

Note: I do not have access to command line注意:我无权访问命令行

As Olaf has stated it might be hard to convert xml into flat csv records.正如 Olaf 所说,将 xml 转换为平坦的 csv 记录可能很困难。 xml is usually made up of many parent/child object whereas csv is typically single object per row xml 通常由许多父/子 object 组成,而 csv 通常是单个 ZA8CFDE6331BD49EB66AC96F8911

To help maybe guide your progress you can try taking one of the xml envelopes and converting that to an xml object in powershell by doing the following. To help maybe guide your progress you can try taking one of the xml envelopes and converting that to an xml object in powershell by doing the following.

$xml = [xml]@"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <ctx:App xmlns:ctx="http://namespace">
      <ctx:Session>
        <ctx:UN>XXXXX</ctx:UN>
        <ctx:SId>11aa11a-123-12a4-12a3-12323aabb123</ctx:SId>
        <ctx:Creation>2021-02-05T00:00:00+11:00</ctx:Creation>
      </ctx:Session>
      <ctx:IC>
        <ctx:TId>11aa1aa-1aa1-2a22-3aa1-aa1b1233456</ctx:TId>
        <ctx:Call>
          <ctx:BI>13</ctx:BI>
          <ctx:CN>Somesystem</ctx:CN>
        </ctx:Call>
        <ctx:Call>
          <ctx:BI>1</ctx:BI>
        </ctx:Call>
        <ctx:Call>
          <ctx:BI>1</ctx:BI>
        </ctx:Call>
        <ctx:Call>
          <ctx:BI>1</ctx:BI>
        </ctx:Call>
        <ctx:Call>
          <ctx:BI>1</ctx:BI>
          <ctx:CN>Somesystem</ctx:CN>
          <ctx:CL>UnknownLocation</ctx:CL>
        </ctx:Call>
        <ctx:Call>
          <ctx:BI>0</ctx:BI>
        </ctx:Call>
      </ctx:IC>
    </ctx:App>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UT>
        <wsse:UN>SomesystemUSR</wsse:UN>
        <wsse:P>Somepassword</wsse:P>
      </wsse:UT>
    </wsse:Security>
  </soapenv:Header>
  <soapenv:Body>
    <ns5:ListRequest xmlns:ns5="http://Namespace/Service/V1.0" xmlns="http://www.immi.gov.au/Namespace/Enterprise/ErrorMessages/V1.0" xmlns:ns6="http://Namespace/Core/V1.0" xmlns:ns8="http://Namespace/Service/V1.0" xmlns:ns7="http://Namespace/Core/V1.0" xmlns:ns9="http://Namespace/Core/V1.0"
      xmlns:ns10="http://Namespace/Core/Messaging/V1.0" xmlns:ns2="http://Namespace/Enterprise/V1.0" xmlns:ns4="http://Namespace/Core/V1.0" xmlns:ns3="http://Namespace/WarningMessages/V1.0">
      <ns4:FromDate>2021-01-28</ns4:FromDate>
      <ns4:ToDate>2021-01-28</ns4:ToDate>
      <ns4:Code>0123</ns4:Code>
      <ns4:Type>U</ns4:Type>
      <ns4:Record>S</ns4:Record>
      <ns4:AnotherCode>D</ns4:AnotherCode>
    </ns5:ListRequest>
  </soapenv:Body>
</soapenv:Envelope>
"@

From there you can examine what the object looks like by drilling down into the properties从那里您可以通过深入研究属性来检查 object 的外观

    PS Env:\> $xml

Envelope
--------
Envelope

PS Env:\> $xml.Envelope

soapenv                                   Header Body
-------                                   ------ ----
http://schemas.xmlsoap.org/soap/envelope/ Header Body

PS Env:\> $xml.Envelope.Body

ListRequest
-----------
ListRequest

PS Env:\> $xml.Envelope.Body.ListRequest

ns5         : http://Namespace/Service/V1.0
xmlns       : http://www.immi.gov.au/Namespace/Enterprise/ErrorMessages/V1.0
ns6         : http://Namespace/Core/V1.0
ns8         : http://Namespace/Service/V1.0
ns7         : http://Namespace/Core/V1.0
ns9         : http://Namespace/Core/V1.0
ns10        : http://Namespace/Core/Messaging/V1.0
ns2         : http://Namespace/Enterprise/V1.0
ns4         : http://Namespace/Core/V1.0
ns3         : http://Namespace/WarningMessages/V1.0
FromDate    : 2021-01-28
ToDate      : 2021-01-28
Code        : 0123
Type        : U
Record      : S
AnotherCode : D

That's all I got.这就是我得到的。 I don't know of any way to convert this type of multidimensional object into flat csv other than picking out what you want from the xml object and creating a custom object with those properties. I don't know of any way to convert this type of multidimensional object into flat csv other than picking out what you want from the xml object and creating a custom object with those properties. I'm sure there's someone smarter out there that might be able to offer more:)我敢肯定那里有更聪明的人可以提供更多:)

UPDATE: So to take this idea a step further and explain what I meant by creating the custom object I've created this function which will take in the xml object and convert it to a custom object with properties I've picked out of the xml. UPDATE: So to take this idea a step further and explain what I meant by creating the custom object I've created this function which will take in the xml object and convert it to a custom object with properties I've picked out of the xml . After that you can use either the ConvertTo-Csv or Export-Csv cmdlets to generate the csv/csv file之后,您可以使用ConvertTo-CsvExport-Csv cmdlet 生成 csv/csv 文件

function Convert-SoapXmlToCustomObject
{
    [cmdletbinding()]
    param(
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [xml[]]$XmlObject
    )

    Process
    {
        if ($XmlObject)
        {
            foreach ($xml in $XmlObject)
            {
                [pscustomobject][ordered]@{
                    Sid         = $xml.Envelope.Header.App.Session.SId
                    Creation    = $xml.Envelope.Header.App.Session.Creation
                    Tid         = $xml.Envelope.Header.App.IC.TId
                    Calls       = @() #if each call needs to be separate record maybe use loop to create
                    Username    = $xml.Envelope.Header.Security.ut.UN
                    FromDate    = $xml.Envelope.Body.ListRequest.FromDate
                    ToDate      = $xml.Envelope.Body.ListRequest.ToDate
                    Code        = $xml.Envelope.Body.ListRequest.Code
                    Type        = $xml.Envelope.Body.ListRequest.Type
                    Record      = $xml.Envelope.Body.ListRequest.Record
                    AnotherCode = $xml.Envelope.Body.ListRequest.AnotherCode

                }
            }
        }
    }
}

$xml, $xml2 | Convert-SoapXmlToCustomObject | ConvertTo-Csv -NoTypeInformation

Output looks like this Output 看起来像这样

"Sid","Creation","Tid","Calls","Username","FromDate","ToDate","Code","Type","Record","AnotherCode"
"11aa11a-123-12a4-12a3-12323aabb123","2021-02-05T00:00:00+11:00","11aa1aa-1aa1-2a22-3aa1-aa1b1233456","System.Object[]","SomesystemUSR","2021-01-28","2021-01-28","0123","U","S","D"
"11aa11a-123-12a4-12a3-12323aabb123BB","2021-02-05T01:00:00+11:00","1as23bb3-1er2-234d-234e-bb8b20995147","System.Object[]","SomeUSR",,,,,,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM