I have a SQL table that contains several hundred rows of data. One of the columns in this table contains text reports that were stored as plain text within the column.
Essentially, I need to iterate through each row of data in SQL and output the contents of each row's report column to its own individual text file with a unique name pulled from another column.
I am trying to accomplish this via PowerShell and I seem to be hung up. Below is what I have thus far.
foreach ($i=0; $i -le $Reports.Count; $i++)
{
$SDIR = "C:\harassmentreports"
$FILENAME = $Reports | Select-Object FILENAME
$FILETEXT = $Reports | Select-Object TEXT
$NAME = "$SDIR\$FILENAME.txt"
if (!([System.IO.File]::Exists($NAME))) {
Out-File $NAME | Set-Content -Path $FULLFILE -Value $FILETEXT
}
}
Assuming that
$Reportsis a list of the records from your SQL query, you'll want to fix the following issues:In an indexed loop use indexed access to the elements of your array:
Define variables outside the loop if their value doesn't change inside the loop:
Expand properties if you want to use their value:
Use
Join-Pathfor constructing paths:Use
Test-Pathfor checking the existence of a file or folder:Use either
Out-Fileor
Set-Contentnot both of them. The basic difference between the two cmdlets is their default encoding. The former uses Unicode, the latter ASCII encoding. Both allow you to change the encoding via the parameter
-Encoding.You may also want to reconsider using a
forloop in the first place. A pipeline with aForEach-Objectloop might be a better approach: