2011-03-30 4 views
3

저장 프로 시저의 출력을 통해 텍스트 파일로 출력하려고하는 작은 문제가 있습니다. 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 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 
[email protected],MasterAdministrator 
[email protected],UserAdministrator 
[email protected],Users 
I가 '열 1'과 밑줄을 제거 할 수 있습니까

? 확장 속성

답변

3

select-object 도움이 될 :

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

rockin! 매력처럼 작동 –

0

사용하여 CSV 파일에 직접 데이터 테이블을 내보낼 수 있습니다 수출 CSV :

$ ds.Tables [0] | export-csv tofile.csv -notypeinfo

+0

여전히 파일에 'column1'헤더를 둡니다. 그래도 export-csv 태그를 잘 알고 있습니다. –