Why don't you use the PowerShell ConvertTo-Html cmdlet to generate the html output? Remove the call to the writedata function from the code and change the line containing the last } into:
} | ConvertTo-Html
There are some things that don't work in your script. One thing is that in PowerShell you have to define a function before you use it. You called the writedata function before it was defined. That does not work.
Something else is that you used properties generated by the New-Object cmdlet as input for the writedata function. However at the location where you called writedata the current object in the pipeline is not the output of the New-Object cmdlet.
Message was edited by: Robert van den Nieuwendijk