我正在使用PowerShell创建HTML报告,该报告显示多个SQL查询。现在我遇到了问题,来自-ExcludeProperty
的{{1}}禁用了SQL查询的给定表名。
目前我将其排除在外:' RowError,RowState,ItemArray,HasErrors'但我需要排除'表'太。但是,如果我将其排除,则名称将替换为' *'。
此查询:
Select-Object
例如以这种方式显示:
--------- |____*____| |______37_|
你能帮忙吗?
编辑: 感谢您的反馈意见!双重报价不起作用。此外,如果我写Files_in_queue我得到相同的输出。 这是我的输出声明:
SELECT count(*) AS 'Files in queue' FROM ...
它向我展示了上述SQL语句的结果,但表头中有$dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray, HasErrors, Table |
ConvertTo-Html –Body $body |
Out-File -Append $OutputFile
,而不是*
- 文本。
这就是我得到SQL结果的方法:
AS
编辑2:
$connectionDetails = "Provider=sqloledb; " +
"Data Source=$dataSourceActual; " +
"Initial Catalog=$databaseActual; " +
"Integrated Security=SSPI;"
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionDetails
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommandActual,$connection
$connection.Open()
$dataAdapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataSet = New-Object System.Data.DataSet
$dataAdapter.Fill($dataSet)
$connection.Close()
$dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray, HasErrors, Table |
ConvertTo-Html –Body $body |
Out-File -Append $OutputFile
错误:
ConvertTo-Html : System.Management.Automation.PSObject can not be converted in the following type: {System.String, System.Management.Automation.ScriptBlock}. Bei C:\temp\html_test.ps1:138 Zeichen:15 + ConvertTo-Html <<<< –Body $body -Property $props | + CategoryInfo : InvalidArgument: (:) [ConvertTo-Html], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.ConvertToHtmlCommand
编辑3:
这是转换为$props = $dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray, HasErrors, Table -First 1 |
ForEach-Object { $_.PSObject.Properties | Select-Object -Expand Name }
$dataSet.Tables[0] | ConvertTo-Html –Body $body -Property $props |
Out-File -Append $OutputFile
:
$props
和输出语句:
[string]$props = $dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray, HasErrors, Table -First 1 |
ForEach-Object { $_.PSObject.Properties | Select-Object * -Expand Name }
如何构建循环,上述代码仅适用于单列表?我试过这个:
$dataSet.Tables[0] | ConvertTo-Html –Body $body -Property $props |
Out-File -Append $OutputFile
我还在if ($dataSet.Length = 1) {
[string]$props = $dataSet.Tables[0] |
Select-Object * -ExcludePropertyRowError, RowState, ItemArray, HasErrors, Table -First 1 |
ForEach-Object { $_.PSObject.Properties | Select-Object * -Expand Name }
} else {
[string]$props = $dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray, HasErrors, Table |
ForEach-Object { $_.PSObject.Properties | Select-Object * -Expand Name }
}
子句和$dataSet.Tables[0].Length
中尝试if
作为条件。
答案 0 :(得分:0)
当ConvertTo-Html
的输入只有一个属性时,您观察到的行为似乎就会发生。如果您仅选择Table
属性:
... | Select-Object Table | ConvertTo-Html -Body $body
输出:
...
<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr>
<tr><td>Table</td></tr>
<tr><td>Table</td></tr>
...
</table>
...
我不清楚此行为的原因,但您可以通过-Property
cmdlet的ConvertTo-Html
参数指定所需的列标题来缓解此问题:
... | Select-Object Table | ConvertTo-Html -Body $body -Property 'Files in queue'
输出:
...
<table>
<colgroup><col/></colgroup>
<tr><th>Files in queue</th></tr>
<tr><td>Table</td></tr>
<tr><td>Table</td></tr>
...
</table>
...
您甚至不需要Select-Object
步骤,因为-Property
的{{1}}参数已经选择了指定的属性。
如果您知道只有一列并且知道您将要用作列标题的值,则可以像这样定义查询字符串:
ConvertTo-Html
并像这样处理表:
$coltitle = 'Files in queue'
$sqlCommandActual = "SELECT count(*) AS `"$coltitle`" FROM ..."
如果您不知道列标题(因为您正在调用存储过程或其他任何内容),您可以像这样在运行中确定它:
$dataSet.Tables[0] |
ConvertTo-Html –Body $body -Property $coltitle |
Out-File -Append $OutputFile
这适用于单个或多个属性。