很抱歉,如果我问的是非常基本的东西,但我昨晚刚刚启动了PowerShell,我对新语言有了感觉。
我正在使用以下链接提供的导出到XLSX脚本:
https://gallery.technet.microsoft.com/office/Export-XLSX-PowerShell-f2f0c035
我也使用简单的SQL Server pull(SQL_Connection_Script.ps1):
$dataSource = "####"
$user = "####"
$pwd = "####"
$database = "####"
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
$query = "Select * from name where id = '1000'"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($result)
$table
$connection.Close()
我的问题是当我导出这个对象时,我得到了额外的列:RowError,RowState,Table,ItemArray和HasErrors。
无论如何要通过我不知道的命令删除这些列,还是应该插入动态Select语句,如下所示?
我希望在可能的情况下不必使用动态Select语句。
.\Desktop\SQL_Connection_Script.ps1 | Select $DynamicHeadersHere | Export-XLSX -path .\Desktop\testing123.xlsx -Append
所以看起来我的SQL Server Pull功能正在吸引额外的领域。有什么想法吗?
答案 0 :(得分:0)
环顾四周后,我发现我可以排除属性。
.\Desktop\SQL_Connection_Script.ps1 | Select * -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray | Export-XLSX -path .\Desktop\testing123.xlsx -Append