Problem creating a step to execute SQL scripts From Folder

I’ve created the following step template using PowerShell to execute SQL files from a particular shared path. The step runs and says completed successfully but does not edit the DB. Neither do I get any of the messages. What could I be doing wrong?

STEP
$ServerName = $OctopusParameters[‘DatabaseServerName’]
$DatabaseName = $OctopusParameters[‘DevelopmentDatabaseName’]
$OutputToFile = $OctopusParameters[‘DBScriptPath’]
$ScriptPath = $OctopusParameters[‘DBScriptPath’]
$SqlLogin = $OctopusParameters[‘DatabaseUserName’]
$SqlPassword = $OctopusParameters[‘DatabasePassword’]

function ExecuteSQLScripts($ScriptPath, $ServerName, $DatabaseName, $SqlLogin, $SqlPassword, [switch]$OutputToFile) {
Write-Host "Executing SQL Scripts…"
Write-Host "- SQL Path: $ScriptPath"
foreach ($f in Get-ChildItem -path $ScriptPath -Filter *.sql | Sort-Object) {
Write-Host "- Script: $($f.FullName)"
if ($OutputToFile) {
$out = “$ScriptPath” + $f.BaseName + ".txt"
Write-Host "- Output: $out"
invoke-sqlcmd -S $ServerName -U $Username -P $Password -b -I -i $f.FullName -o $out
}
else {
invoke-sqlcmd -S $ServerName -U $Username -P $Password -b -I -i $f.FullName
}
if ($LastExitCode -ne 0) {
throw “SQL Script Execution failed. sqlcmd.exe returned exit code $LastExitCode.”
}
}
}

PARAMETERS
Server Name
#{$ServerName}

Database Name
#{$DatabaseName}

Output File
#{$OutputToFile}

Path to SQL scripts
#{$ScriptPath}

Sql Login
#{$SqlLogin}

Sql Password
#{$SqlPassword}

Hi Moody,

Thanks for getting in touch! Could you please share us the complete step template JSON file? this will allow us to test it exacly like Octopus would invoke it. To get the complete JSON go to Library > Step Template > [your step template] > Export

Also, it would be good to check (if you havent already) on your DB logs if any connection was made from the designated user at that time.

Thanks

Dalmiro

Hello,
Thanks for the prompt response, It doesn’t seem like any connection was made. Plse see below the Json file

{
“Id”: “ActionTemplates-33”,
“Name”: “Execute SQL Scripts From Folder”,
“Description”: “This script executes SQL scripts placed in a shared folder”,
“ActionType”: “Octopus.Script”,
“Version”: 5,
“Properties”: {
“Octopus.Action.Script.ScriptBody”: “$ServerName = $OctopusParameters[‘DatabaseServerName’]\r\n$DatabaseName = $OctopusParameters[‘DevelopmentDatabaseName’] \r\n$OutputToFile = $OctopusParameters[‘DBScriptPath’]\r\n$ScriptPath = $OctopusParameters[‘DBScriptPath’]\r\n$SqlLogin = $OctopusParameters[‘DatabaseUserName’]\r\n$SqlPassword = $OctopusParameters[‘DatabasePassword’]\r\n\r\n\r\nfunction ExecuteSQLScripts($ScriptPath, $ServerName, $DatabaseName, $SqlLogin, $SqlPassword, [switch]$OutputToFile) {\r\n Write-Host “Executing SQL Scripts…”\r\n Write-Host “- SQL Path: $ScriptPath”\r\n foreach ($f in Get-ChildItem -path $ScriptPath -Filter *.sql | Sort-Object) {\r\n Write-Host “- Script: $($f.FullName)”\r\n if ($OutputToFile) {\r\n $out = “$ScriptPath\” + $f.BaseName + “.txt”\r\n Write-Host “- Output: $out”\r\n invoke-sqlcmd -S $ServerName -U $Username -P $Password -b -I -i $f.FullName -o $out\r\n }\r\n else {\r\n invoke-sqlcmd -S $ServerName -U $Username -P $Password -b -I -i $f.FullName\r\n }\r\n if ($LastExitCode -ne 0) {\r\n throw “SQL Script Execution failed. sqlcmd.exe returned exit code $LastExitCode.”\r\n }\r\n }\r\n}”
},
“SensitiveProperties”: {},
“Parameters”: [
{
“Name”: “$ServerName”,
“Label”: “Server Name”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$DatabaseName”,
“Label”: “Database Name”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$OutputToFile”,
“Label”: “Output File”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$ScriptPath”,
“Label”: “Path to SQL scripts”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$SqlLogin”,
“Label”: “Sql Login”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$SqlPassword”,
“Label”: “Sql Password”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “Sensitive”
}
}
],
“LastModifiedOn”: “2015-03-31T17:56:30.918+00:00”,
“LastModifiedBy”: “redacted”,
"$Meta": {
“ExportedAt”: “2015-03-31T18:56:56.782Z”,
“OctopusVersion”: “2.6.4.951”,
“Type”: “ActionTemplate”
}
}

Hello,
Wondering if you have an update on this issue.

Thank you,
Moody Amakobe

Hi Moody,

Sorry for the delay on this. We’ve had our share of holidays this past days and we are getting back on track! I’ve just tried to add the step using the JSON you sent me but I got the error attached below. Can you confirm that you got this JSON from Library > Step Template > [your step template] > Export ?

Thanks
Dalmiro

Hello,
I completely undersntad regarding the holidays, it’s a necessary ingredient for your brain :slight_smile: Anyway I didn’t see the attachment, but find below the json. I’m pretty sure it was the one.

{
“Id”: “ActionTemplates-33”,
“Name”: “Execute SQL Scripts From Folder”,
“Description”: “This script executes SQL scripts placed in a shared folder”,
“ActionType”: “Octopus.Script”,
“Version”: 5,
“Properties”: {
“Octopus.Action.Script.ScriptBody”: “$ServerName = $OctopusParameters[‘DatabaseServerName’]\r\n$DatabaseName = $OctopusParameters[‘DevelopmentDatabaseName’] \r\n$OutputToFile = $OctopusParameters[‘DBScriptPath’]\r\n$ScriptPath = $OctopusParameters[‘DBScriptPath’]\r\n$SqlLogin = $OctopusParameters[‘DatabaseUserName’]\r\n$SqlPassword = $OctopusParameters[‘DatabasePassword’]\r\n\r\n\r\nfunction ExecuteSQLScripts($ScriptPath, $ServerName, $DatabaseName, $SqlLogin, $SqlPassword, [switch]$OutputToFile) {\r\n Write-Host “Executing SQL Scripts…”\r\n Write-Host “- SQL Path: $ScriptPath”\r\n foreach ($f in Get-ChildItem -path $ScriptPath -Filter *.sql | Sort-Object) {\r\n Write-Host “- Script: $($f.FullName)”\r\n if ($OutputToFile) {\r\n $out = “$ScriptPath\” + $f.BaseName + “.txt”\r\n Write-Host “- Output: $out”\r\n invoke-sqlcmd -S $ServerName -U $Username -P $Password -b -I -i $f.FullName -o $out\r\n }\r\n else {\r\n invoke-sqlcmd -S $ServerName -U $Username -P $Password -b -I -i $f.FullName\r\n }\r\n if ($LastExitCode -ne 0) {\r\n throw “SQL Script Execution failed. sqlcmd.exe returned exit code $LastExitCode.”\r\n }\r\n }\r\n}”
},
“SensitiveProperties”: {},
“Parameters”: [
{
“Name”: “$ServerName”,
“Label”: “Server Name”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$DatabaseName”,
“Label”: “Database Name”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$OutputToFile”,
“Label”: “Output File”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$ScriptPath”,
“Label”: “Path to SQL scripts”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$SqlLogin”,
“Label”: “Sql Login”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$SqlPassword”,
“Label”: “Sql Password”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “Sensitive”
}
}
],
“LastModifiedOn”: “2015-03-31T17:56:30.918+00:00”,
“LastModifiedBy”: “ME”,
"$Meta": {
“ExportedAt”: “2015-04-06T19:08:01.394Z”,
“OctopusVersion”: “2.6.4.951”,
“Type”: “ActionTemplate”
}
}

Hi Moody,

I was able to import the step. I’ll be setting up a SQL server environment to test this out and get back to you tomorrow. Thanks for the patience!

Dalmiro

NP Thanks for the help!

Hi Moody,

On my end it also seems like no connections were made. Before jumping to troubleshoot to script, can i recommend you to use DBup to run scripts on your database? It involves creating another project and a nuget package, but after that its as straightforward as putting scripts on a folder on your project and running a .exe from an Octopus step.

Down at the bottom of this link there’s a video where Paul does a great job explaining how to use DBup

http://docs.octopusdeploy.com/display/OD/SQL+Server+databases

Let me know if this suits your project’s needs

Thanks!

Dalmiro

Hello,
An update, I managed to get this working thanks to http://www.sqlfeatherandquill.com/2011/01/10/how-to-execute-all-sql-files-in-a-directory-with-powershell/
please find the JSON below, feel free to modify it and share.

{
“Id”: “ActionTemplates-33”,
“Name”: “Execute SQL Scripts From Folder”,
“Description”: “This script executes SQL scripts placed in a shared folder”,
“ActionType”: “Octopus.Script”,
“Version”: 8,
“Properties”: {
“Octopus.Action.Script.ScriptBody”: “$ServerName = $OctopusParameters[‘DatabaseServerName’]\r\n$DatabaseName = $OctopusParameters[‘DevelopmentDatabaseName’] \r\n$OutputToFile = $OctopusParameters[‘DBScriptPath’]\r\n$ScriptPath = $OctopusParameters[‘DBScriptPath’]\r\n\r\n\r\nforeach ($f in Get-ChildItem -path $ScriptPath -Filter *.sql | sort-object)\r\n{\r\n$out = $OutputToFile + $f.name.split(”.")[0] + “.txt” ;\r\ninvoke-sqlcmd –ServerInstance $ServerName -Database $DatabaseName -InputFile $f.fullname | format-table | out-file -filePath $out\r\n}"
},
“SensitiveProperties”: {},
“Parameters”: [
{
“Name”: “$ServerName”,
“Label”: “Server Name”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$DatabaseName”,
“Label”: “Database Name”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$OutputToFile”,
“Label”: “Output File”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
},
{
“Name”: “$ScriptPath”,
“Label”: “Path to SQL scripts”,
“HelpText”: null,
“DefaultValue”: null,
“DisplaySettings”: {
“Octopus.ControlType”: “SingleLineText”
}
}
],
“LastModifiedOn”: “2015-05-04T15:34:32.218+00:00”,
“LastModifiedBy”: “Me,
”$Meta": {
“ExportedAt”: “2015-05-04T15:34:39.699Z”,
“OctopusVersion”: “2.6.4.951”,
“Type”: “ActionTemplate”
}
}

Hi,

Thanks for taking the time to share your answer! Hopefully it’ll help other with the same issue.

Cheers

Dalmiro

When i try to import json above i get Unexpected token
in JSON at position 1860