PowerShell Export-Csv命令中的控制台文本

时间:2018-12-04 09:04:21

标签: powershell

我使用Export-Csv命令来导出数据库表,但是它将打印已为控制台中的每个表导出的行数。我没有找到该命令的任何参数来防止这种情况。

$server = "USER\SQLEXPRESS"
$database = "database_test"
$tablequery = "SELECT name from sys.tables"

#Delcare Connection Variables
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $tablequery
$command.Connection = $connection

#Load up the Tables in a dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()

$DriveName = (Get-Location).Drive.Name
$extractDir = md -Force "$($DriveName):\csv\files"

# Loop through all tables and export a CSV of the Table Data
foreach ($Row in $DataSet.Tables[0].Rows)
{
    $connection.open();
    #Specify the output location of your dump file
    $command.CommandText = "SELECT * FROM [$($Row[0])]"
    $command.Connection = $connection

    (Get-Culture).NumberFormat.NumberDecimalSeparator = '.'
    (Get-Culture).DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'

    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $command
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $connection.Close()

    $extractFile = "$($extractDir)\$($Row[0]).csv"
    $DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation -Encoding UTF8
}

导出的CSV示例:

"vat_id","category_id","vat_code"
"1","1","1"
"2","1","2"
"3","1","3"
"4","1","4"
"5","1","5"
"19","1","6"
"2345","1","8"
"2346","1","9"
"2347","1","10"

0 个答案:

没有答案