Dynamically build XML from PowerShell array

Welcome Forums General PowerShell Q&A Dynamically build XML from PowerShell array

Viewing 4 reply threads
  • Author
    Posts
    • #263984
      Participant
      Topics: 3
      Replies: 4
      Points: 43
      Rank: Member

      Hi there,

      I’m trying to build a dynamic HTML from an array of objects.  The application that consumes the HTML needs the HTML tags as it would be in a normal webpage.

      OK, so for each object in the array I need to dynamically build the following HTML.  Are there any standard commands in PowerShell that can create this code so I can use the write-output command?  I have about 50 elements in the array that have 2 variables, $servername and $status

       

      Write-Output “<PRTG>”
      “<result>”
      “<channel>Server Name</channel>”
      “<value>$server</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “<result>”
      “<channel>Server Status</channel>”
      “<value>$status</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “</PRTG>”

      }

       

      Thanks!

       

      • This topic was modified 1 week, 3 days ago by ajtsystems76.
      • This topic was modified 1 week, 2 days ago by kvprasoon. Reason: Title correction
    • #264008
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      Can you elaborate the question a little bit with a sample output ?

    • #264077
      Participant
      Topics: 3
      Replies: 4
      Points: 43
      Rank: Member

      Good Morning,

      My array contains the following

      Server      Status
      ——-       ——-

      Server1     running
      Server2    running
      Server3    error
      Server4    running

      The array is named $array and the server column inside the array is dynamic, so the number of server entries will increase and decrease.

      I also have a function called $buildhtml that I pass the array to as follows:

      Function Buildhtml([array]$Servernames){

      Foreach($srv in $servernames){

      Write-Output “<PRTG>”
      “<result>”
      “<channel>Server Name</channel>”
      “<value>$status</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “</PRTG>”

      }

      }

      For each server in the array I need to populate the following html with the $server and $status and importantly write-output:

      Write-Output “<PRTG>”
      “<result>”
      “<channel>Server Name</channel>”
      “<value>$server</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “<result>”
      “<channel>Server Status</channel>”
      “<value>$status</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “</PRTG>”

      }

       

      So basically I need to end up with something like this.  And remember the amount of servers will vary depending on how many are in the array

      Write-Output “<PRTG>”
      “<result>”
      “<channel>Server Name</channel>”
      “<value>$server</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “<result>”
      “<channel>Server Status</channel>”
      “<value>$status</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “</PRTG>”

      “<result>”
      “<channel>Server Name</channel>”
      “<value>$server1</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “<result>”
      “<channel>Server Status</channel>”
      “<value>$status1</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”

      “<result>”
      “<channel>Server Name</channel>”
      “<value>$server2</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “<result>”
      “<channel>Server Status</channel>”
      “<value>$status2</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      “</PRTG>”

      etc………………..

      The problem I have with my function is that the write-output inside the foreach loop only contains 1 server at a time and I need it to contain all the servers from the array that is passed to it.  This is because the application that consumes the output doesnt really work if not all the servers are in the write-output html tags

       

      • This reply was modified 1 week, 2 days ago by ajtsystems76.
    • #264095
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      So you have a collection of servers with its name and status. From the function it looks like you are dynamically creating XML with the servername and status. I didn’t get the html here ?

    • #264116
      Participant
      Topics: 3
      Replies: 4
      Points: 43
      Rank: Member

      Its OK, I’ve managed to get it working by building the $htmlasr array.  Yes, its XML, not html

       

      $htmlasr = @”
      <prtg>
      “@
      foreach($asr in $AsrItems)
      {
      $server = $asr.RecoveryAzureVMName | out-string
      $status = $asr.ReplicationHealth | out-string
      $status = $status.Trim()

      if($status -eq “Normal”){$statusnumber = 0}
      else {$statusnumber = 1}

      write-host $server + $status + $statusnumber
      $htmlasr += @”
      “<result>”
      “<channel>$server</channel>”
      “<value>$statusnumber</value>”
      “<showChart>1</showChart>”
      “<showTable>1</showTable>”
      “<error>1</error>”
      “</result>”
      #“<result>”
      #“<channel>Replication Status</channel>”
      #“<value>$statusnumber</value>”
      #“<showChart>1</showChart>”
      #“<showTable>1</showTable>”
      #“<error>1</error>”
      #“</result>”
      “@
      }
      $htmlasr += @”
      </prtg>
      “@

Viewing 4 reply threads
  • You must be logged in to reply to this topic.