troubleshooting Question

Powershell To export data

Avatar of svs1919
svs1919 asked on
Powershell
4 Comments1 Solution17 ViewsLast Modified:
Hello experts,

I am using the below powershell to get data from a ODBC data source and then write the output to a csv. Looks like the script is working but I think it is running in a loop where it is loading the data then clearing and reloading data again.

Can you please suggest me how to stop the loop and just load the data once


$OuputFile = "C:\Test.csv"
function Get-ODBC-Data{
   param([string]$query=$(throw 'query is required.'))
   $conn = New-Object System.Data.Odbc.OdbcConnection
   $conn.ConnectionString = "DSN=Test;Uid=username;Pwd=password;"
   $conn.open()
   $cmd = New-object System.Data.Odbc.OdbcCommand($query,$conn)
   $ds = New-Object system.Data.DataSet
   (New-Object system.Data.odbc.odbcDataAdapter($cmd)).fill($ds) | out-null
   $conn.close()
   $ds.Tables[0]
}

function Set-ODBC-Data{
   param([string]$query=$(throw 'query is required.'))
  $conn = New-Object System.Data.Odbc.OdbcConnection
  $conn.ConnectionString= "DSN=Test;Uid=username;Pwd=password;"
  $cmd = new-object System.Data.Odbc.OdbcCommand($query,$conn)
  $conn.open()
  $cmd.ExecuteNonQuery()
  $conn.close()
}

$query = "SELECT * FROM Tablename"
$result = Get-ODBC-Data -query $query
set-odbc-data -query $query

$result | Export-Csv $OuputFile -NoTypeInformation
(Get-Content $OuputFile) | foreach {$_ -replace ‘"‘} | Set-Content $OuputFile
ASKER CERTIFIED SOLUTION
Qlemo
"Batchelor", Developer and EE Topic Advisor
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros