There has to be an easy way to do this. I have a Powershell script which connects to a database and I want to save the resulting datatable as a CSV. Here is the code so far:
$connString = "Provider=msdaora;Data Source=MyDatabase;User Id=test;Password=test
$qry = "select * from employees"
$OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connString)
$OLEDBConn.open()
$readcmd = New-Object system.Data.OleDb.OleDbCommand
$readcmd.Connection = $OLEDBConn
$readcmd.CommandTimeout = '300'
$readcmd.CommandText = $qry
$da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)
$dt = New-Object system.Data.datatable
[void]$da.fill($dt)
$OLEDBConn.close()
this should do it I think:
$ds = New-Object System.Data.DataSet
$da.Fill($ds) >$null| Out-Null
$ds.Tables[0] | export-csv tofile.csv -notypeinformation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With