Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Replace a substring using Powershell


Replace a substring using Powershell

Author
Message
Roust_m
Roust_m
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1068 Visits: 1953
Hi,

I've got a job, one step of which adds some articles into a publication:

$DBScriptFile = "C:\Scripts\RO_replication.sql" 
#SCRIPT
Invoke-Sqlcmd -ServerInstance localhost -database master -InputFile $DBScriptFile



RO_replication.sql looks this way:

USE MyDB{CountryCode}
GO
PRINT 'Adding articles to MyDB{CountryCode}_RO'
GO
exec sp_addarticle @publication = MyDB{CountryCode}_RO', @article = N'AdChannel', ...
GO
...


I need to change the Powershell code above, so it replaces {CountryCode} with the result of this query:

Select CountryCode From [DBUtilities].[dbo].[EnvBuild] 
Where Id = (select MAX(Id) from [DBUtilities].[dbo].[EnvBuild])



The original TSQL script should stay generic (keeping {CountryCode}) so the changes made by replacement should not be saved.

Any ideas?

Thanks.



Roust_m
Roust_m
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1068 Visits: 1953
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?



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search