﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / Replace a substring using Powershell / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 03:13:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Replace a substring using Powershell</title><link>http://www.sqlservercentral.com/Forums/Topic1390227-391-1.aspx</link><description>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 $QueryThe last command does not work, because $Query holds the path to the file:C:\Scripts\test.sqlI 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?</description><pubDate>Sun, 02 Dec 2012 16:58:34 GMT</pubDate><dc:creator>Roust_m</dc:creator></item><item><title>Replace a substring using Powershell</title><link>http://www.sqlservercentral.com/Forums/Topic1390227-391-1.aspx</link><description>Hi,I've got a job, one step of which adds some articles into a publication:[code="sql"]$DBScriptFile = "C:\Scripts\RO_replication.sql" #SCRIPTInvoke-Sqlcmd -ServerInstance localhost -database master -InputFile $DBScriptFile[/code]RO_replication.sql looks this way:[code="sql"]USE MyDB{CountryCode}GOPRINT 'Adding articles to MyDB{CountryCode}_RO'GOexec sp_addarticle @publication = MyDB{CountryCode}_RO', @article = N'AdChannel', ...GO...[/code]I need to change the Powershell code above, so it replaces {CountryCode} with the result of this query:[code="sql"]Select CountryCode From [DBUtilities].[dbo].[EnvBuild] Where Id = (select MAX(Id) from [DBUtilities].[dbo].[EnvBuild])[/code]The original TSQL script should stay generic (keeping  {CountryCode}) so the changes made by replacement should not be saved.Any ideas?Thanks.</description><pubDate>Wed, 28 Nov 2012 18:52:16 GMT</pubDate><dc:creator>Roust_m</dc:creator></item></channel></rss>