我遇到了一个小问题,试图通过存储过程将输出转换为文本文件。的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'和下划线?
答案 0 :(得分:3)
select-object
可能有所帮助:
ds.Tables[0] | Select-Object -expand Column1 | out-file..
答案 1 :(得分:0)
您可以使用export-csv:
将数据表直接导出到csv文件中$ ds.Tables [0] | export-csv tofile.csv -notypeinformation