0

I am writing a Powershell Script to read SPListItems, create a PSObject for each, add it to the PS Array and after the loop ends I use the following to convertto-html

myArray | convertto-html -path ......

Part of my code within iterating the list items is as follows:

$obj = new-object PSOBject
$obj | Add-Member -MemberType NoteProperty -Name "Title" -Value $item["Title"] |Out-Null
$obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $file.Name |Out-Null
$obj | Add-Member -MemberType NoteProperty -Name "Link" -Value $file.ServerRelativeUrl |Out-Null

$myArray += $obj

The problem is that I do get the HTML. However, I also get raw psobjects. The Html generated is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
System.Collections.Hashtable
System.Collections.Hashtable
System.Collections.Hashtable
System.Collections.Hashtable

<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>IsReadOnly</th><th>IsFixedSize</th><th>IsSynchronized</th><th>Keys</th><th>Values</th> 
<th>SyncRoot</th><th>Count</th></tr>
<tr><td>False</td><td>False</td><td>False</td><td>System.Collections.Hashtable+KeyCollection</td> 
<td>System.Collections.Hashtable+ValueCollection</td><td>System.Object</td><td>2</td></tr>
<tr><td>False</td><td>False</td><td>False</td><td>System.Collections.Hashtable+KeyCollection</td> 
<td>System.Collections.Hashtable+ValueCollection</td><td>System.Object</td><td>2</td></tr>
<tr><td>False</td><td>False</td><td>False</td><td>System.Collections.Hashtable+KeyCollection</td> 
<td>System.Collections.Hashtable+ValueCollection</td><td>System.Object</td><td>2</td></tr>

</table>
</body></html>

Other than using html string to concatenate a html document, can someone please help me fix it using Convertto-Html?

Thank you.

11
  • how do you initialize $myArray before you start adding objects to it? – willman Mar 12 '20 at 19:48
  • can you post your entire myArray | convertto-html command? – willman Mar 12 '20 at 19:56
  • ----- $myArray = @() – Ali Mar 12 '20 at 19:56
  • it was not letting me add without the leading dashes. Therefore, added some - otherwise actual declaration starts from the $ – Ali Mar 12 '20 at 19:57
  • $myArray | ConvertTo-Html -head $Header | out-file "c:\temp\htmldoc.html" -Force $Header is just a here-string with <style> tag to add some css to the output – Ali Mar 12 '20 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.