I've been trying to pull data from a sql query and get it converted to HTML to finally embed the results in an email body.
My code is as follows;
$SQLCommand = New-Object System.Data.SqlClient.SqlCommand
$SQLCommand.CommandText = "SELECT DISTINCT SYS.Name,LDISK.DeviceID0,LDISK.Size0 AS DiskSizeMB,LDISK.FreeSpace0 AS FreeSpaceMB,SCCM.dbo.v_GS_WORKSTATION_STATUS.LastHWScan,SCCM.dbo.v_GS_LastSoftwareScan.LastScanDate
FROM v_FullCollectionMembership_Valid SYS
JOIN v_GS_LOGICAL_DISK LDISK ON SYS.ResourceID = LDISK.ResourceID
INNER JOIN SCCM.dbo.v_GS_WORKSTATION_STATUS
ON LDISK.ResourceID = SCCM.dbo.v_GS_WORKSTATION_STATUS.ResourceID
INNER JOIN SCCM.dbo.v_GS_LastSoftwareScan
ON SCCM.dbo.v_GS_LastSoftwareScan.ResourceID =
SCCM.dbo.v_GS_WORKSTATION_STATUS.ResourceID
WHERE
LDISK.DeviceID0 = 'C:' AND
LDISK.DriveType0 = 3 AND
((LDISK.FreeSpace0 <= ((LDISK.Size0 * 10) / 100)) OR
(LDISK.FreeSpace0 <= 1024)) AND
SYS.CollectionID = 'SMS00001'
ORDER BY
SYS.Name"
$SQLCommand.Connection = $SQLConnection
$SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SQLCommand
$SQLDataset = New-Object System.Data.DataSet
$SqlAdapter.fill($SQLDataset) | out-null
$FileInfo = $SQLDataset.tables | FT -AutoSize
The resulting format of the data stored in $FileInfo looks good;
Name DeviceID0 DiskSizeMB FreeSpaceMB LastHWScan LastScanDate
---- --------- ---------- ----------- ---------- ------------
Server01 C: 53244 2010 7/28/2017 3:18:01 PM 7/28/2017 5:25:51 AM
...however when I pipe this to ConvertTo-HTML ($FileInfo | ConvertTo-HTML) the resulting format comes out like this;
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>ClassId2e4f51ef21dd47e99d3c952918aff9cd</th><th>pageHeaderEntry</th><th>pageFooterEntry</th><th>autosizeInfo</th><th>shapeInfo</th><th>groupingEntry</th></tr>
<tr><td>033ecb2bc07a4d43b5ef94ed5a35d280</td><td></td><td></td><td>Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo</td><td>Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo</td><td></td></tr>
<tr><td>9e210fe47d09416682b841769c78b8a3</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
When I look at the type of my $FileInfo I get this;
$FileInfo.GetType();
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
...so I suspect that the ConvertTo-HTML module is looking for input in string format however I can't seem to get this to work properly - even after trying options like $FileInfo | Out-String -Stream
I want to believe that this can be done easily - I just can't find the right approach. Thanks in advance!
ConvertTo-Htmlis no exception. Formatting cmdlets such asFormat-Table(ft) should solely be used for immediate display - the output of such cmdlets is not useful for processing the data further, so assigningFormat-Tableoutput to the variable you're piping toConvertTo-Htmlwill not produce meaningful results.