Trying to run a Powershell scirpt from package

Beginning deployment…

Warning 17:19:22
The script file “ServerAppSQLs\PostDeploy.ps1” contained within the package will not be executed because it is contained within a child folder. As of Octopus Deploy 2.4, scripts in sub folders will not be executed.

Info 17:19:23
Deployment complete.

Hi Mohammed,

Thanks for getting in touch! You need to have the script at the root of your package (meaning not inside of a subfolder like “ServerAppSQLs”) if you want Octopus to execute it automatically during your deployment. See attached screenshot for better reference

Hope that helps!

Dalmiro

I am trying to run the below package with PostDeploy.ps1 calling Oxtopus.1.0.bat file with script to run tables and stored procedures.

Postdeploy.ps1

param([string]$Server=“ALAWQCPM005\TEST5,50426”,[string]$ServerID =“dltest5”, [string]$BatFile=“Octopus.1.0.bat”)
cd
cd C:\Mujeeb\ServerAppSQLs
#write-output “$BatFile “”$Server”" > $ServerID.core.log"
cmd.exe /C “$BatFile “”$Server”" > $ServerID.core.log"

[cid:image001.png@01D09DEF.FCF3B900]

Hi Mujeeb,

The correct syntax to call an executable on the same directory would be

& .\console.bat

(see attached screenshot of how my NuGet package looks like)

Try to adapt that syntax to the needs of your bat and let me know how it goes.

Thanks!

Dalmiro

Can you please tell me what will be syntax to run this PS script on below server calling the .bat file from C:\ServerAppSQLs ?

param([string]$Server=“ALAWQCPM005\TEST5,50426”,[string]$ServerID =“dltest5”, [string]$BatFile=“Octopus.1.0.bat”)
cd
cd C:\ServerAppSQLs

I am getting error:
Tentacle script execution
Error 11:59:03
cd : Cannot find path ‘C:\ServerAppSQLs’ because it does not exist.
Error 11:59:03
At C:\WINDOWS\system32\config\systemprofile\AppData\Local\Tentacle\Temp\5478b7a
Error 11:59:03
8-9254-43a4-bd4f-62b593cea67d.ps1:3 char:1
Error 11:59:03

  • cd C:\ServerAppSQLs
    Error 11:59:03

Error 11:59:03

  • CategoryInfo : ObjectNotFound: (C:\ServerAppSQLs:String) [Set-L
    Error 11:59:03
    ocation], ItemNotFoundException
    Error 11:59:03
  • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLo
    Error 11:59:03
    cationCommand
    Info 11:59:03
    ============================================== PowerShell exit code: 1 ==============================================
    Fatal 11:59:03
    PowerShell script returned a non-zero exit code: 1 Tentacle version 2.6.5.1010

[cid:image001.png@01D09EBA.B40A1650]

According to the error message, the path C:\ServerAppSQLs doesnt seem to exist. Make sure this path exists before you try to call the executable that is supposed to be there.

Please try using this script inside of PostDeploy.ps1 and let me know how it goes

$Folder = "ServerAppSQLs"
$File = "Octopus.1.0.bat"
$server = "ALAWQCPM005\TESTS,50426"
$serverID = "dltest5"

$Filepath = "$PSScriptRoot\$folder\$file"

cd $PSScriptRoot\$folder

If(!(Test-Path $Filepath)){
    Write-Output "File not found: $Filepath"
}
Else{
    Write-Output "Running $Filepath ..."
    & $Filepath $server | Out-File "$serverID.core.log"
}

I am still not able to run the post deploy script on the server. Looks like powershell script is not getting executed on the server where code has been deployed.

I have created a Nuget package called test5.

Created Deploy a Nuget package step in octopus Deploy website and created a release and deployed it to server. All the folder are getting created but postdeploy.ps1 is not getting executed on the server.

execute the postdeploy.ps1 from Powershell ISE works correctly.

What trigger the postdeploy.ps1 to execute on server : alawqpm005 ?

Please see blow the screen shots for more details.

[cid:image003.jpg@01D0A210.5EF3B230]

[cid:image004.png@01D0A20F.64C4E9A0]

[cid:image005.png@01D0A210.5EF3B230]

image003.jpg

Hi,

Acording to image005.png you are still using the same script, which doesnt seem to work. Could you please test with the script i provided and let me know the results?

Here’s the script:

$Folder = "ServerAppSQLs"
$File = "Octopus.1.0.bat"
$server = "ALAWQCPM005\TESTS,50426"
$serverID = "dltest5"

$Filepath = "$PSScriptRoot\$folder\$file"
cd $PSScriptRoot\$folder

If(!(Test-Path $Filepath)){
    Write-Output "File not found: $Filepath"
}
Else{
    Write-Output "Running $Filepath ..."
    & $Filepath $server | Out-File "$serverID.core.log"
}

Regards,

Dalmiro

Do you want me to run this in as a PowerShell script or as a script step from octopus application ?

I’d like you to put the code I provided inside your PostDeploy.ps1 script and then start a deployment

Okay, thank you.

After updating PostDeploy.ps1 with script with below code and then started a deployment, received error below:

I have included the Octopus.1.0.bat data, and the folder containing all the scripts, and error screen shot from Octopus deploy.

Please let me know your findings.

Octopus.1.0.bat contains:

@Echo Octopus.1.0.bat, SQLServer: %1

@Echo – .NET CLR Procs –

@Echo – Tables –

SQLCMD -E -d “Octopus” -S %1 -i “ProductionServerList.sql”

SQLCMD -E -d “Octopus” -S %1 -i “TestServerList.sql”

@Echo – Procedures –

SQLCMD -E -d “Octopus” -S %1 -i “ProductServerSelectSP.sql”

SQLCMD -E -d “Octopus” -S %1 -i “TestServerSelectSP.sql”

@Echo – Seed/Conversion Scripts –

SQLCMD -E -d “Octopus” -S %1 -i “ProductionServerSeed_Script.sql”

SQLCMD -E -d “Octopus” -S %1 -i “TestServerSeed_Script.sql”

@Echo – END OF SQLS –

[cid:image004.jpg@01D0A2CA.E4C3B8F0]

[cid:image005.jpg@01D0A2CA.E4C3B8F0]

Please let me know if you need any more information.

Thank you,

Mohammed Mujeeb

image004.jpg

image005.jpg

Hi Mohammed,

The good news is that the bat is being executed by the postdeploy.ps1 script now(yay!). The bad news is that i’m not sure how your batch file is supposed to work, hence i cant be that helpful troubleshooting it. Specially taking into account that this executes scripts on your database, which is something i’d really rather not mess around with.

Now that you know how to execute the bat from an Octopus deployment, please proceed to do a careful troubleshooting of the code inside of the batch file.

Sorry for the bad news

Dalmiro

Hi,

I am getting error below when I try to install Octopus Deploy Tentacle on one of the server.

Error: The command that failed was: “netsh” advfirewall firewall add rule “name=Octopus Deploy Tentacle” dir=in action=allow protocol=TCP localport=10933
Deleted instance: Tentacle

Windows fire wall service is disable on the server.

Do you know how to resolve this issue?

[cid:image001.jpg@01D0A9DE.E6568C90]

Thank you,

Mohammed Mujeeb
Senior Test Engineer
Insurance Marketing Solutions
LexisNexis | Risk Solutions
3096896413 x 46244 Direct
mohammed.mujeeb@lexisnexis.commailto:mohammed.mujeeb@lexisnexis.com

---------------------------------------- The information contained in this e-mail message is intended only for the personal and confidential use of the recipient(s) named above. This message may be an attorney-client communication and/or work product and as such is privileged and confidential. If the reader of this message is not the intended recipient or an agent responsible for delivering it to the intended recipient, you are hereby notified that you have received this document in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify us immediately by e-mail, and delete the original message.

image001.jpg

Hi Mohammed,

Please enable the windows firewall and try again.

http://webplay.pro/blog/enable-disable-firewall-in-windows-server-2012.html

Thanks,

Dalmiro

Thank you! that worked.

Power shell script error with following message. Can you please help me in resolving the script errors.

[cid:image002.jpg@01D0ADA7.26AADD20]

But if I run the Power shell script with following code it runs successfully. But this script is not executing as a package from Octopus deploy.

[cid:image004.jpg@01D0ADA7.26AADD20]

Can you please help me in resolving the first script errors.

Thank you,

Mohammed Mujeeb
Senior Test Engineer
Insurance Marketing Solutions
LexisNexis | Risk Solutions
3096896413 x 46244 Direct
mohammed.mujeeb@lexisnexis.commailto:mohammed.mujeeb@lexisnexis.com

image004.jpg

Hi Mohamed,

Its not possible to read the error message on that resolution while being highlighted. Please copy the text or (better) send a screenshot with a higher resolution.

Dalmiro

PS C:\PowershellScripts> C:\PowershellScripts\PostDeploy.ps1
Running C:\PowershellScripts\ServerAppSQLs\Octopus.1.0.bat …
Octopus.1.0.bat : Sqlcmd: ‘ProductionServerList.sql’: Invalid filename.
At C:\PowershellScripts\PostDeploy.ps1:11 char:5

  • & $Filepath $server | Out-File "$serverID.core.log"
    
  • ~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (Sqlcmd: 'Produc…valid filename.:String) [], RemoteException
    • FullyQualifiedErrorId : NativeCommandError
      Sqlcmd: ‘TestServerList.sql’: Invalid filename.
      Sqlcmd: ‘ProductServerSelectSP.sql’: Invalid filename.
      Sqlcmd: ‘TestServerSelectSP.sql’: Invalid filename.
      Sqlcmd: ‘ProductionServerSeed_Script.sql’: Invalid filename.
      Sqlcmd: ‘TestServerSeed_Script.sql’: Invalid filename.

Please also find Octopus.1.0.bat file contents below:

@Echo Octopus.1.0.bat, SQLServer: %1

@Echo – .NET CLR Procs –
@Echo – Tables –

SQLCMD -E -d “Octopus” -S %1 -i "ProductionServerList.sql"
SQLCMD -E -d “Octopus” -S %1 -i “TestServerList.sql”

@Echo – Procedures –

SQLCMD -E -d “Octopus” -S %1 -i "ProductServerSelectSP.sql"
SQLCMD -E -d “Octopus” -S %1 -i “TestServerSelectSP.sql”

@Echo – Seed/Conversion Scripts –

SQLCMD -E -d “Octopus” -S %1 -i "ProductionServerSeed_Script.sql"
SQLCMD -E -d “Octopus” -S %1 -i “TestServerSeed_Script.sql”

@Echo – END OF SQLS –

Hi Mohamed,

You need to test this in the context of an Octopus Deployment, as its your final goal to run the script from there and not from the ISE. I made one last change to the script, please try it from Octopus this time.

$Folder = "ServerAppSQLs"
$File = "Octopus.1.0.bat"
$server = "ALAWQCPM005\TESTS,50426"
$serverID = "dltest5"

$Filepath = "$PSScriptRoot\$folder\$file"
cd $PSScriptRoot\$folder

If(!(Test-Path $Filepath)){
    Write-Output "File not found: $Filepath"
}
Else{
    Write-Output "Running $Filepath ..."
    & $Filepath $server | Out-File "$serverID.core.log"
}

Dalmiro