在Powershell中将sql Collection转换为String

时间:2017-07-28 19:53:33

标签: powershell

我一直在尝试从sql查询中提取数据并将其转换为HTML,最后将结果嵌入到电子邮件正文中。

我的代码如下;

$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

存储在$ FileInfo中的数据的结果格式看起来不错;

名称DeviceID0 DiskSizeMB FreeSpaceMB LastHWScan LastScanDate
---- --------- ---------- ----------- ---------- ------ ------
Server01 C:53244 2010 7/28/2017 3:18:01 PM 7/28/2017 5:25:51 AM

...但是当我将它传递给ConvertTo-HTML($ FileInfo | ConvertTo-HTML)时,生成的格式就像这样;

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

当我查看我的$ FileInfo的类型时,我得到了这个;

$FileInfo.GetType();

IsPublic IsSerial Name                                     BaseType                                                                                                                                                               
-------- -------- ----                                     --------                                                                                                                                                               
True     True     Object[]                                 System.Array  

...所以我怀疑ConvertTo-HTML模块正在寻找字符串格式的输入,但是我似乎无法使其正常工作 - 即使在尝试$FileInfo | Out-String -Stream之后的选项

我想相信这可以轻松完成 - 我找不到合适的方法。提前谢谢!

1 个答案:

答案 0 :(得分:1)

删除format-table,在第一个表中使用select with exlude not required选项,试试这个

$SQLDataset.tables[0] |
    select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray | 
        ConvertTo-Html