Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

execute all sql scripts stored in certain folder and if any script fail in between then loop should exit and record error

$
0
0

execute all sql scripts stored in certain folder and if someone fails in between then loop should exit and record error 

#run sql scripts/sps from folder C:\MyTSQLScripts and if any error ocour then it this loop will stop and error record in output file
Get-ChildItem -Path "C:\MyTSQLScripts" -Filter "*.sql" | % {invoke-sqlcmd -InputFile $_.FullName}
foreach ($f in Get-ChildItem -path "C:\MyTSQLScripts\" -Filter *.sql | sort-object){
$out = "C:\MyTSQLScripts\" + $f.name.split(".")[0] + ".txt" ;
invoke-sqlcmd –ServerInstance myServer -Database DBname -InputFile $f.fullname | format-table | out-file -filePath $out
}
foreach ($f in Get-ChildItem -path "C:\MyTSQLScripts\" -Filter *.sql | sort-object -desc ) 
{ 
$out = "C:\MyTSQLScripts\" + $f.name.split(".")[0] + ".txt" ; 
invoke-sqlcmd -InputFile $f.fullname | format-table | out-file -filePath $out 
}
above script gives error: The term 'invoke-sqlcmd' is not recognized as the name of a cmdlet, function, s
cript file, or operable program. Check the spelling of the name, or if a path w
as included, verify that the path is correct and try again.



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>