PowerShell uses Invoke-sqlcmd to connect script files and save parameters to the script file.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.