When I manually apply a SQL file, like a re-indexing job, everything runs perfectly. I want to automate applying SQL files in powershell, but I'm getting all kinds of incorrect syntax errors when using Get-Content. Is there a better way to get a SQL files contents, then apply those contents on a remote server that doesn't re-format the code to the point where I get incorrect syntax errors.
Generic error I get (any syntax might throw an error - ps seems to be mis-applying the syntax when getting the content of the file):
Incorrect syntax near [anything]
Note: all GOs have been removed, so this isn't related to those; it may throw an error due to a begin, a goto, etc. The reason is that it's not retaining how the SQL file is built.
Update
The SQL files can be anything from adding permissions to creating an index to building a table to creating a stored procedure. I have about 100 SQL files. If I manually execute them, they all work, so this isn't related to bad SQL syntax, but related to how Powershell is reading the SQL syntax and running it as a command.
Answer
The below appears to work:
Get-Content $file -Raw
SQLcodeGet-Contentyou should possibly pipe intoOut-Sting....Get-Content $file | Out-String. Does that change anything?