格式化存储过程中的Powershell输出

格式化存储过程中的Powershell输出,powershell,Powershell,我在尝试通过将存储过程的输出转换为文本文件时遇到了一个小问题。Powershell #Connection Object $cn = New-Object System.Data.SqlClient.SqlConnection( "Data Source=localhost; Database=test;User ID=test;Password=xyzzy;" ) $q = "exec usp_Users" #Data Adapter which will gather t

我在尝试通过将存储过程的输出转换为文本文件时遇到了一个小问题。Powershell

#Connection Object
$cn = New-Object System.Data.SqlClient.SqlConnection(
    "Data Source=localhost; Database=test;User ID=test;Password=xyzzy;"
    )

$q = "exec usp_Users"

#Data Adapter which will gather the data using our query
    $da = New-Object System.Data.SqlClient.SqlDataAdapter($q, $cn)
#DataSet which will hold the data we have gathered
    $ds = New-Object System.Data.DataSet
#Out-Null is used so the number of affected rows isn't printed
    $da.Fill($ds) >$null| Out-Null
#Close the database connection
$cn.Close()

if($ds.Tables[0].Rows.Count -eq 0){   
write-host '0:No Data found'   
exit 2 
}  

$file = "C:\temp\" + "users" + $(Get-Date -Format 'MM_dd_yyyy') + ".txt"
$ds.Tables[0] |  out-File $file -encoding ASCII -width 255
以下是输出:

Column1
-----
USER_NAME,USER_TYPE
test@spamex.com,MasterAdministrator
foo@hotmail.com,UserAdministrator
test4@test.com,Users

如何删除“Column1”和下划线?

使用展开属性选择对象
可能会有帮助:

ds.Tables[0] | Select-Object -expand Column1 | out-file..

您可以使用导出csv将数据表直接导出到csv文件中:


$ds.Tables[0]|将csv导出到file.csv-notypeinformation

仍将“column1”标题保留到文件中。很高兴知道导出csv标签