Invoke-sqlcmd output without “quotes” and headers

左心房为你撑大大i 提交于 2020-08-08 04:42:49

问题


Output example:

#TYPE System.Data.DataRow <---
"PersonalNr" <---
"00001005"
"00001008"
"00001009"
"00001013"
"00001019"
"00001024"

Requirements:

I want a output without the 2 first lines and without the quote symbols, how can i do that?

For the headers I tried the options -h -1 but the output is still the same. Is it even possible with Sqlcmd?

Current Code:

Invoke-Sqlcmd -ServerInstance SERVER -h -1 -Query $QueryFmt | Export-CSV $AttachmentPath

回答1:


This code should work:

Invoke-Sqlcmd -ServerInstance "" -Database "" -Query "" | ConvertTo-Csv -NoTypeInformation -Delimiter "," | Select-Object -Skip 1 | % {$_ -replace '"', ""} | Out-File ("C:\test.csv") -Force -Encoding ascii

Invoke-Sqlcmd produces:

PersonalNr       
---    
00001005  
00001001  
00001006  
00001007  
00001008 

With Export-Csv, the file looks like:

#TYPE System.Data.DataRow
"PersonalNr"
"00001005"
"00001001"
"00001006"
"00001007"
"00001008"

Using my above mentioned code, I get the file looking like this:

00001005
00001001
00001006
00001007
00001008



回答2:


Try this:

## You data for export to csv
$csv = Invoke-Sqlcmd -ServerInstance SERVER  -Query $QueryFmt
# 

##Will remove type information  and will remove header
$csv | Export-CSV -NoTypeInformation | select -Skip 1 | Set-Content $AttachmentPath 
#



回答3:


Try this,

$query_result | Format-Table -AutoSize -Wrap -HideTableHeaders



来源:https://stackoverflow.com/questions/43047946/invoke-sqlcmd-output-without-quotes-and-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!