Original http://stackoverflow.com/questions/16654866/invoke-sqlcmd-inputfile-and-variable
Init.ps1
$DATABASEFILENAME = "C:\Program Files\Microsoft SQL Server\mssql10_50.sql2008instance\mssql\data\mydb.mdf" $ Databaselogname = "C:\Program Files\Microsoft SQL Server\mssql10_50.sql2008instance\mssql\data\mydb_log.ldf" $ Dbuserowner = "Domain\spsetup"
Createdatabase.ps1
try { $createDatabaseScript = ($scriptsFolder, $eachRelease, $DeployEnvironment, "Config"-join "\") + "\JM Siterequest database.sql " $sqlVariable =" Databasefilename= $DATABASEFILENAME "," databaselogname=$ Databaselogname "," dbuserowner= $DBUSEROWNER " invoke-sqlcmd-serverinstance" $MySQLServer "-inputfile" $ Createdatabasescript "-erroraction stop-variable $sqlVariable}catch [Exception] { write-error" Database Error: $_. Exception "}
$sqlVariable are very picky about the parameter's reputation. You cannot have spaces before or after a variable .
Note that there can be no spaces before or after $sqlVariable variables.
SQL Script
CREATE DATABASE [Siterequestdb] on PRIMARY (NAME = n ' siterequestdb ', FILENAME = N ' $ (databasefilename) ', SIZE = 3072KB , MAXSIZE = UNLIMITED, filegrowth = 1024KB) LOG on (NAME = n ' siterequestdb_log ', FILENAME = N ' $ (databaselogname) ', SIZ E = 1024KB, MAXSIZE = 2048GB, filegrowth = 10%) GO
How to call variables in $sqlvariable in SQL script $ (variable name)
PowerShell uses Invoke-sqlcmd to connect script files and save parameters to the script file.