This is where I got so far:
$DBScriptFile = "C:\Scripts\test.sql"
$replacement = invoke-sqlcmd -query "Select CountryCode From [DBUtilities].[dbo].[EnvBuild] Where Id = (select MAX(Id) from [DBUtilities].[dbo].[EnvBuild])"
$Query = $DBScriptFile -replace "{CountryCode}",[string]$replacement[0]
Invoke -Sqlcmd -ServerInstance localhost -database master -InputFile $Query
The last command does not work, because $Query holds the path to the file:
C:\Scripts\test.sql
I need it to hold the actual text of the script instead. C:\Scripts\test.sql still has {CountryCode} in it. I could output the result of the replacement back into the file, but I don't want to save the file. I don't want to make numerous copies of the file either. I just want to be able to put the script into a variable and run it.
Any ideas?