Powershell SQL查询结果不合适

时间:2013-08-14 16:53:34

标签: sql-server-2008 powershell powershell-v2.0

您好我已经构建了一个可以正常工作的脚本,除了一件事,有时返回的字符串很长,以至于它不适合powershell控制台,当我稍后将文本发送到richtextbox时,我得到了所有... ....最后而不是整个字符串

$username = "myaccount"
$sqlconnection = New-Object system.data.sqlclient.sqlconnection
$sqlconnection.ConnectionString ="server=myserver\sccm;database=sccm;trusted_connection=true;"
$sqlconnection.Open()
sqlcmd = New-Object system.data.sqlclient.sqlcommand
$sqlcmd = $sqlconnection.CreateCommand()
$sqlcmd.CommandText = "SELECT Info from SCCM.dbo.log WHERE Username = '$username'"
$sqlcmd.Connection = $sqlconnection
$data = New-Object system.data.sqlclient.sqldataadapter $sqlcmd
$dataset = New-Object system.data.dataset
$data.Fill($dataset)
$global:result = $dataset.Tables

我无法在任何地方指定-Width参数,所以我迷失了如何获得结果的全长?

1 个答案:

答案 0 :(得分:0)

您可以将数据集保存为csv文件,而不是在powershell中显示:

#Fill the dataset with the SQL response. Using [void] redirects console output to null (don't display)
[void]$data.Fill($dataset)

#Pipe the contents of the dataset. Use Select to select all columns/properties excluding those that were created by the DataSet object (not actual data)  

#Pipe to Export-CSV to create a CSV file, use -notypeinformation flag to skip Object type information from the file (e.g. System.String etc)  

$dataset.Tables[0] | Select *  -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray | Export-CSV -notypeinformation -path C:\Somedir\Somefile.csv