Hello
This is my first post. I am pretty new to Powershell and wondered if anybody can help!. I have this code which needs to write each row from the SQL query to a serperate txt file. The problem I have is that the first result is created fine but the remaing results get grouped together and put into another file which is not what I want. I believe it is to do with the loop.
Any help Please
Thanks
$Username = "hsw_pcc";
$Password = "hsw_pcc";
#$file = "F:\Mark.txt"; # Sets the location for the file to write to
$Connection = New-Object Data.SqlClient.SqlConnection
$Connection.ConnectionString = "Data Source=test2k801\t2k801;Initial Catalog=SWData;Persist Security Info=True;User ID=$Username ;Password=$Password"
$Connection.Open()
$sql = "select callref, prob_text
Problem from opencall
where suppgroup = '3RDLINE/APPSUPPORT/'
and (closedby IS NULL OR closedby =
'')
and callclass ='Incident' and owner = ''
order by callref"
$Command = New-Object System.Data.SqlClient.SqlCommand $sql,$Connection
$Reader = $Command.ExecuteReader()
$counter = $Reader.FieldCount
while($Reader.read())
{ # open of the while loop
$file = "F:\Testing"+$i+".txt"
echo $counter
for($i = 0; $i -lt $counter; $i++)
{
@{ $Reader.GetName($i) = $Reader.GetValue($i);}
$reader[$i] >> $file
} # close of the for
} # close of the while
$Connection.Close()