I'm using the below POWERSHELL scrip to export the content of a SQL table to a .csv. It has been working well, unfortunately it's failing as the table in question has now had some NULLS added to it.
Is there a way to modify this to allow it to pass NULLS, or is it best to amend the underlying table.
$server = "sqlmtest"
$database = "mi_lookups"
$query = "SELECT * FROM [dbo].[SQLMtest-BIS-BUS-BPS_system_agent_lookup]"
$extractFile = @"
G:\system_Lookup_Tables\system_agent_lookup.csv
"@
$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 = $query
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
$DataSet.Tables[0] | Export-Csv $extractFile
Thank you.
selectstatement such that NULLs are not returned, or yourExport-Csvpipeline such that NULLs do not cause a problem