# Dynamically build XML from PowerShell array

**URL:** <https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101>\
**Category:** PowerShell Help\
**Created:** [October 16, 2020, 8:07am UTC](https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101 "2020-10-16T08:07:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajtsystems](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ajtsystems](https://forums.powershell.org/u/ajtsystems)\
**Post date:** [October 16, 2020, 8:07am UTC](https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101/1 "2020-10-16T08:07:22Z")

</div>

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

&nbsp;

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

}

&nbsp;

Thanks!

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 16, 2020, 9:57am UTC](https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101/2 "2020-10-16T09:57:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ajtsystems](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ajtsystems](https://forums.powershell.org/u/ajtsystems)\
**Post date:** [October 16, 2020, 9:12pm UTC](https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101/3 "2020-10-16T21:12:48Z")

</div>

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

}

&nbsp;

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

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 16, 2020, 11:25pm UTC](https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101/4 "2020-10-16T23:25:33Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![ajtsystems](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ajtsystems](https://forums.powershell.org/u/ajtsystems)\
**Post date:** [October 17, 2020, 12:57am UTC](https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101/5 "2020-10-17T00:57:20Z")

</div>

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

&nbsp;

$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\>  
"@

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/dynamically-build-xml-from-powershell-array/15101/6 "2024-05-16T20:28:43Z")

</div>


