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


Powershell: Passing variables to scriptfile using invoke-sqlcmd


Powershell: Passing variables to scriptfile using invoke-sqlcmd

Author
Message
jamie.crawford
jamie.crawford
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 22
Hi,
I have several large scripts that we have to usually modify manually to make changes to our databases. Usually this consists of 3 different variables. Lets use $city, $state, $country for example. I have been looking for a way to pass these variables to the script but can not find any examples, so I'm not sure if it is actually possible. I see that invoke-sqlcmd has a -variable option, which I tried passing an array too to see if I could pass the variables to the script without any luck.

$scriptfile = 'c:\temp\mybigscript.sql'
$server = 'localhost'
$database ='bigdb"
$MyArray = "MyVar1 = 'miami'", "MyVar2 = 'florida'", "MyVar3 = 'usa'"
Invoke-SqlCMD -InputFile "$scriptfile" -ServerInstance $server -Database "$database" -variable $myarray

Is what I'm attempting to do even possible?

Any help is appreciated.
Thanks!
Gary Varga
Gary Varga
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8340 Visits: 6154
I cannot sum it up any better than this DevCentral article.

Just in case the link ever goes here is an example of a named parameter (must be the first line of the script):

param([string]$namedArgument="default")
# Defaults to the text "default" if not present.

Write-Host $namedArgument


Or using the standard $args variable:

Write-Host $args[0]



Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
jamie.crawford
jamie.crawford
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 22
Hi,
I can see how that works with the variables in the command line.. I am not having problems doing that, I am having problems changing the variables inside of the actual sql script (add_location.sql) that invoke-sqlcmd is running. I tried a simple script below to see if it would work, and did not have any luck.
Thanks again for the help!


param (
[string]$ScriptFile = "C:\Users\Administrator\Downloads\DATABASE\INSTALL\XX\Add_LOCATION.sql",
[string]$Server = "localhost",
[string]$Database = "DB_POWERSHELL",
[string]$jdcfacility = "myfacilityname"
)

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$sqlSrv = New-Object 'Microsoft.SqlServer.Management.Smo.Server' ($server)
TRAP {Write-Host -ForegroundColor Red "Error:Unable to run script" ;continue}
Invoke-SqlCMD -InputFile "$scriptfile" -ServerInstance $server -Database "$database" -v $jdcfacility
Gary Varga
Gary Varga
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8340 Visits: 6154
Does this offer what you need: http://technet.microsoft.com/en-us/library/cc281720.aspx

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
jamie.crawford
jamie.crawford
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 22
I figured it out using an example from this page..

http://thefirstsql.com/2010/06/09/using-sqlcmd-from-powershell/


Here is my example that worked...

#Powershell script
#Parameters that must be set:
$DBServer = "localhost"
$database = "DB_POWERSHELL"
$CITY = "KANSAS CITY"
$STATE = "MO"
$COUNTRY = "USA"
$DBScriptFile = "C:\Users\Administrator\Downloads\DATABASE\INSTALL\XX\AddCity.sql"

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$sqlSrv = New-Object 'Microsoft.SqlServer.Management.Smo.Server' ($server)
$DBParam1 = "CITY=" + $CITY
$DBParam2 = "STATE=" + $STATE
$DBParam3 = "COUNTRY=" + $COUNTRY
$DBParams = $DBParam1, $DBParam2, $DBParam3
Invoke-Sqlcmd -InputFile $DBScriptFile -Variable $DBParams -Serverinstance $DBServer -Database "$database"


#SMALL SECTION OF CODE FROM ADDCITY.SQL

INSERT #CITY VALUES
('$(CITY)', '$(STATE)', '$(COUNTRY)');



Thanks for your help!
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