PowerShell для преобразования вложенного отчета XML в таблицу HTML - PullRequest
0 голосов
/ 14 октября 2019

Мне нужна ваша помощь в создании отчета в формате HTML из XML. Существует несколько отчетов XML с несколькими тегами, которые я хочу преобразовать в таблицу HTML, разделив каждую таблицу на основе дочерних атрибутов из XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<report domain="DUMMY_APP" time="10/04 09:05">
<server appli="DUMMY_APP PF10" description="Dummy services" name="DUMMY_APP_98667.DummyDomain.com" status="prod">
  <service name="CYA HOTBackup Client Service" result="ok" state="running" />
  <service name="Dummy_app Docbase Service GBRPF10" result="ok" state="running" />
  <service name="Dummy_app Java Method Server" result="ok" state="running" />
  <service name="Dummy_app Docbroker Service Docbroker" result="ok" state="running" />
  <service name="Dummy_app Docbroker Service Docbroker2" result="ok" state="running" />
  <service name="Dummy_app Docbase Service TEPBR" result="ok" state="running" />
  <service name="Dummy_app Docbase Service TOTAUSTRAL" result="ok" state="running" />
</server>
<server appli="DUMMY_APP PF10" description="Tomcat services" name="DUMMY_APP_658787.DummyDomain.com" status="prod">
  <service name="Tomcat8080" result="ok" state="running" />
  <service name="Tomcat8081" result="ok" state="running" />
  <service name="Tomcat8082" result="ok" state="running" />
  <service name="Tomcat8083" result="ok" state="running" />
  <service name="Tomcat8084" result="ok" state="running" />
  <service name="Tomcat8085" result="ok" state="running" />
</server>
<server appli="DUMMY_APP PF11" description="Documentum services" name="DUMMY_APP_67898.DummyDomain.com" status="prod">
  <service name="CYA HOTBackup Client Service" result="ok" state="running" />
  <service name="Dummy_app Docbase Service GBRPF11" result="ok" state="running" />
  <service name="Dummy_app Java Method Server" result="ok" state="running" />
  <service name="Dummy_app Docbroker Service Docbroker" result="ok" state="running" />
  <service name="Dummy_app Docbroker Service Docbroker2" result="ok" state="running" />
  <service name="Dummy_app Docbase Service SOHARLNGB" result="ok" state="running" />
</server>
<server appli="DUMMY_APP PF11" description="Tomcat services" name="DUMMY_APP_0098.DummyDomaian.external.com" status="prod">
  <service name="Tomcat8080" result="ok" state="running" />
  <service name="Tomcat8081" result="ok" state="running" />
  <service name="Tomcat8082" result="ok" state="running" />
  <service name="Tomcat8083" result="ok" state="running" />
  <service name="Tomcat8084" result="ok" state="running" />
  <service name="Tomcat8085" result="ok" state="running" />
</server>
<server appli="DUMMY_APP2" description="" name="VEEP-STAGE.Local" status="prod">
   <server-error message="[ERR] 'DEBUG' Issue at VEEP-STAGE.local" result="ok" />
</server>
<oracle>
  <database appli="DummyApp PF10" description="DummyApp-LOPDM01 (11.2.0.3)" name="PF10 AP4GBR10@DummyApp-LOPDM01" result="ok" state="running" status="production" />
  <database appli="DummyApp PF10" description="DummyApp-LOPDM05 (11.2.0.3)" name="PF10 AP4TEPBR@DummyApp-LOPDM05" result="ok" state="running" status="production" />
  <database appli="DummyApp PF10" description="DummyApp-LOPDM01" name="PF10 AP4TOTAU@DummyApp-LOPDM01" result="ok" state="running" status="production" />

</oracle>
<sqlserver>
</sqlserver>
<urls>
  <url appli="DUmmyApp PF10" description="http://DummyApp-apprw10:8081/review-annotate/info" name="PF10 Common  Urls emc-review-pas-app" result="ok" state="running" status="production" server_name="" url="http://DummyApp-apprw10:8081/review-annotate/info"/>
  <url appli="DummyApp PF10" description="http://DummyApp-apprw10:8081/report/isDeployed" name="PF10 Common  Urls isDeployed" result="ok" state="running" status="production" server_name="" url="http://DummyApp-apprw10:8081/report/isDeployed"/>
  <url appli="DummyApp PF10" description="http://DummyApp-apprw10:8081/pjc-mi/mivalidator.jsp" name="PF10 Common  Urls mivalidator" result="ok" state="running" status="production" server_name="" url="http://DummyApp-apprw10:8081/pjc-mi/mivalidator.jsp"/>
  <url appli="DummyApp PF10" description="http://DummyApp-apprw10:8081/pjc-update/muvalidator.jsp" name="PF10 Common  Urls muvalidator" result="ok" state="running" status="production" server_name="" url="http://DummyApp-apprw10:8081/pjc-update/muvalidator.jsp"/>
 </urls>
</report>

Я легко могу получить таблицу HTML из кода ниже:

$t = [xml](New-Object System.Net.WebClient).DownloadString("input.xml")
$ServerStatus = @()
$URLStatus = @()
$DBStatus = @()
foreach ($node in ($t.report).ChildNodes) {
    foreach ($item in $node.ChildNodes) {
        if ($node.Localname -contains "server") {
            foreach ($server in $item) {
                if ($item.LocalName -contains "server-error") {
                    $SERVICE_STATUS = $item.message
                } else {
                    $SERVICE_STATUS = $item.state
                }
                $serverStatus += $Item |
                    Select-Object @{Name='APPLICATION_NAME';Expression={$node.appli}},
                        @{Name='SERVER_DESCRIPTION';Expression={$node.description}},
                        @{Name='SERVER_NAME';Expression={$node.name}},
                        @{Name='SERVICE_NAME';Expression={$item.name}},
                        @{Name='SERVICE_RESULT';Expression={$item.result}},
                        @{Name='SERVICE_STATE';Expression={$SERVICE_STATUS}}
            }
        }
        if ($node.LocalName -contains "oracle") {
            foreach ($db in $item) {
                $DBStatus += $Item |
                    Select-Object @{Name='APPLICATION_NAME';Expression={$db.appli}},
                        @{Name='DB_DESCRIPTION';Expression={$db.description}},
                        @{Name='DB_NAME';Expression={$db.name}},
                        @{Name='DB_RESULT';Expression={$db.result}},
                        @{Name='DB_STATE';Expression={$db.state}}
            }
        }
        if ($node.LocalName -contains "urls") {
            foreach ($url in $item) {
                $URL_DESCRIPTION = $u.description
                $URL_STATE = $u.state
                $APPLICATION_NAME = $u.name
                $URLStatus += $Item |
                    Select-Object @{Name='APPLICATION_NAME';Expression={$url.appli}},
                        @{Name='URL_DESCRIPTION';Expression={$url.description}},
                        @{Name='URL_NAME';Expression={$url.url}},
                        @{Name='URL_RESULT';Expression={$url.result}},
                        @{Name='URL_STATE';Expression={$url.state}}
            }
        }
    }
}

$ServerStatus
$DBStatus
$URLStatus

$processHtml = $ServerStatus | Foreach {$_} |
    Select "Application_Name","Server_Name","Service_Name","Service_State","Service_Result" |
    ConvertTo-Html -Head $htmlformat
$htmlformat -replace "&lt;","<" -replace "&quot;",'"' -replace "&gt;",">" |
    Out-File $env:TEMP\_serviceCheck.html

Но в этом случае результат слишком велик, так как три фрагмента HTML $ServerStatus,$DBStatus,$URLStatus для каждого массива.

Как я могу создать отдельный массив из этого массива на основе имени приложения?

--Application Name 1----
Server Status TABLE
DB Status TABLE
URL Status TABLE

--Application Name 2----
Server Status TABLE
DB Status TABLE
URL Status TABLE
...