0

I used the code found here https://stackoverflow.com/a/17497834/8918324 to make an SQL query that dosen't need to import the libraries for SQL.

The goal is having it run on a server and once a week pull the query and send it as a CSV table to my boss.

$conn = New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerName, $DatabaseName, $ConnectionTimeout
$conn.ConnectionString = $ConnectionString
$conn.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand ($Query, $conn)
$cmd.CommandTimeout = $QueryTimeout
$ds = New-Object System.Data.DataSet
$da = New-Object System.Data.SqlClient.SqlDataAdapter ($cmd)
$da.Fill($ds)
$conn.Close()
$ds.Table

Expected is a result like Visitor number name visited ... in an CSV. Depending where I put it it is eihter empty or just gives a table ID.

Where do I put the Export-Csv cmdlet?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

0 Answers0