FOR 循环中包含多列的 SQL 查询没有 HTML 输出
No HTML output for SQL queries with more than one column in a FOR loop
我正在 for
循环中处理不同的 SQL 查询,以将它们导出到 HTML 报告中。
这是我的代码:
$OutputFile = "MyReport.htm"
##set HTML formatting
$a = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
width: auto
}
TH{border-width: 0px;
padding: 0px;
border-style: solid;
border-color: black;
border-collapse: collapse;
background-color:#C0C0C0
}
TD{border-width: 0px;
padding: 0px;
border-style: solid;
border-color: black;
border-collapse: collapse;
background-color:white
}
</style>
"@
$a | ConvertTo-Html -head $a | Out-File $OutputFile
for ($i=0; $i -lt $dataSource.Length; $i++) {
$TableHeaderActual = $TableHeader[$i]
$body = @"
<p style="font-size:14px;font-weight:bold;text-decoration:underline;family:helvetica;color:black;">
$TableHeaderActual
</p>
"@
$dataSourceActual = $dataSource[$i]
$databaseActual = $database[$i]
$sqlCommandActual = $sqlCommand[$i]
##Create a string variable with all our connection details
$connectionDetails = "Provider=sqloledb; " +
"Data Source=$dataSourceActual; " +
"Initial Catalog=$databaseActual; " +
"Integrated Security=SSPI;"
##Connect to the data source using the connection details and T-SQL command we provided above, and open the connection
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionDetails
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommandActual,$connection
$connection.Open()
##Get the results of our command into a DataSet object, and close the connection
$dataAdapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataSet = New-Object System.Data.DataSet
$dataAdapter.Fill($dataSet)
$connection.Close()
[string]$props = $dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray, HasErrors, Table -First 1 |
ForEach-Object { $_.PSObject.Properties | Select-Object -Expand Name }
##Return all of the rows and pipe it into the ConvertTo-HTML cmdlet, and then pipe that into our output file
$dataSet.Tables[0] |
ConvertTo-Html -Body $body -Property $props |
Out-File -Append $OutputFile
}
$TableHeader
、$dataSource
、$database
和 $sqlCommand
是字符串数组。
它可以工作,但是当我得到一个包含多个列的 SQL 查询的结果时,输出不起作用。我得到的只是 table 边框,仅此而已。
你能帮忙吗?
这似乎是 PowerShell v2 中 ConvertTo-Html
cmdlet 的限制(或错误)。根据在线帮助,参数应该接受一个对象数组作为输入:
PS C:\> <b>$PSVersionTable</b>
名称值
---- -----
CLR版本 2.0.50727.3655
构建版本 6.0.6002.18111
PS版本 2.0
WSManStack 版本 2.0
PS兼容版本 {1.0, 2.0}
序列化版本 1.1.0.1
PS远程协议版本 2.1
PS C:\> <b>Get-Help ConvertTo-Html -参数属性</b>
-属性 <对象[]>
包括 HTML 中对象的指定属性。
必需的?错误的
位置? 1个
默认值
接受管道输入?错误的
接受通配符?假
但是,在 PowerShell v2 中,只有将 属性 名称列表转换为字符串数组时才有效:
[string[]]$props = $dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray,
HasErrors, Table -First 1 |
ForEach-Object { $_.PSObject.Properties | Select-Object -Expand Name }
无论您是否有一个或多个 属性 个名称,都可以转换为字符串数组。
但是,更好的解决方案是至少升级到 PowerShell v3,才能真正解决问题。
我正在 for
循环中处理不同的 SQL 查询,以将它们导出到 HTML 报告中。
这是我的代码:
$OutputFile = "MyReport.htm"
##set HTML formatting
$a = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
width: auto
}
TH{border-width: 0px;
padding: 0px;
border-style: solid;
border-color: black;
border-collapse: collapse;
background-color:#C0C0C0
}
TD{border-width: 0px;
padding: 0px;
border-style: solid;
border-color: black;
border-collapse: collapse;
background-color:white
}
</style>
"@
$a | ConvertTo-Html -head $a | Out-File $OutputFile
for ($i=0; $i -lt $dataSource.Length; $i++) {
$TableHeaderActual = $TableHeader[$i]
$body = @"
<p style="font-size:14px;font-weight:bold;text-decoration:underline;family:helvetica;color:black;">
$TableHeaderActual
</p>
"@
$dataSourceActual = $dataSource[$i]
$databaseActual = $database[$i]
$sqlCommandActual = $sqlCommand[$i]
##Create a string variable with all our connection details
$connectionDetails = "Provider=sqloledb; " +
"Data Source=$dataSourceActual; " +
"Initial Catalog=$databaseActual; " +
"Integrated Security=SSPI;"
##Connect to the data source using the connection details and T-SQL command we provided above, and open the connection
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionDetails
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommandActual,$connection
$connection.Open()
##Get the results of our command into a DataSet object, and close the connection
$dataAdapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataSet = New-Object System.Data.DataSet
$dataAdapter.Fill($dataSet)
$connection.Close()
[string]$props = $dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray, HasErrors, Table -First 1 |
ForEach-Object { $_.PSObject.Properties | Select-Object -Expand Name }
##Return all of the rows and pipe it into the ConvertTo-HTML cmdlet, and then pipe that into our output file
$dataSet.Tables[0] |
ConvertTo-Html -Body $body -Property $props |
Out-File -Append $OutputFile
}
$TableHeader
、$dataSource
、$database
和 $sqlCommand
是字符串数组。
它可以工作,但是当我得到一个包含多个列的 SQL 查询的结果时,输出不起作用。我得到的只是 table 边框,仅此而已。
你能帮忙吗?
这似乎是 PowerShell v2 中 ConvertTo-Html
cmdlet 的限制(或错误)。根据在线帮助,参数应该接受一个对象数组作为输入:
PS C:\> <b>$PSVersionTable</b>
名称值
---- -----
CLR版本 2.0.50727.3655
构建版本 6.0.6002.18111
PS版本 2.0
WSManStack 版本 2.0
PS兼容版本 {1.0, 2.0}
序列化版本 1.1.0.1
PS远程协议版本 2.1
PS C:\> <b>Get-Help ConvertTo-Html -参数属性</b>
-属性 <对象[]>
包括 HTML 中对象的指定属性。
必需的?错误的
位置? 1个
默认值
接受管道输入?错误的
接受通配符?假
但是,在 PowerShell v2 中,只有将 属性 名称列表转换为字符串数组时才有效:
[string[]]$props = $dataSet.Tables[0] |
Select-Object * -ExcludeProperty RowError, RowState, ItemArray,
HasErrors, Table -First 1 |
ForEach-Object { $_.PSObject.Properties | Select-Object -Expand Name }
无论您是否有一个或多个 属性 个名称,都可以转换为字符串数组。
但是,更好的解决方案是至少升级到 PowerShell v3,才能真正解决问题。