I am totally new to Powershell. I feel as though I have exhausted looking on the internet, but I also know that this answer should be easy.
I want a loop to:
- Run 50 SQL files
- Save them as a custom named txt file
For example, if SQL Files are:
- E:\SQLscripts\apples.sql
- E:\SQLscripts\bananas.sql
- E:\SQLscripts\clementines.sql
- E:\SQLscripts\oranges.sql
- E:\SQLscripts\watermelons.sql
- etc.
I want to have:
- E:\OutputTXT\apples.txt
- E:\OutputTXT\bananas.txt
- E:\OutputTXT\clementines.txt
- E:\OutputTXT\oranges.txt
- E:\OutputTXT\watermelons.txt
- etc.
Rather than posting 10 different chunks of code, I have tried the following
- Export-CSV -NoTypeInformation
- Export-CSV -NoTypeInformation -Force
- Export-CSV -NoTypeInformation -Force -
- Out-File $outdir.$filename.basename.txt
- Out-File %filename.basename.txt%
- Out-File ($filename.Basename + ".txt")
- Path ($outdir + $filename.Name + ".csv")
$sqlserver = 'local'
$sqldb = 'dbname'
$sqluser = 'user'
$sqlpw = 'pw'
$sourcedir = 'E:\SQLscripts'
$outdir = 'E:\OutputTXT'
foreach ($filename in get-childitem -Path $sourcedir -Filter "*.sql") {
invoke-sqlcmd –ServerInstance $sqlserver -Database $sqldb -Username $sqluser -Password
$sqlpw -InputFile $filename.fullname |
(missing piece here)
}