0

I simply created an console app with argument number check at the very beginning. And after the package is deployed, in the deployment PowerShell script part, I directly call this app with no argument to test the script. It seems Octopus just captures the exit code and show there is no output from the app captured in task log at all.

    static void Main(string[] args)
    {
        if (args.Length < 4)
        {
            Console.WriteLine("Invalid argument number");
            Environment.ExitCode = -1;
            return;
        }
    }

However, if I simply put "echo 'test'" or even just 'test' string in the script, the output was captured in Octopus deployment task log. Any idea what is the correct way to log console app in the script? Thanks.

bigbearzhu
  • 2,381
  • 6
  • 29
  • 44

4 Answers4

0

Sorry, it is not fault of Octopus, It is actually the console app was built for .Net framework 4.6.1 but the tentacle server is only having 4.5.2. When I run the app on that server through remote desktop, it pops up error message saying 4.6.1 .Net framework is missing. Rebuild the app with 4.5.2 fixed this issue. However it was really hard to find this out because Octopus task log has nothing related to that. Hope this would help someone else in the future.

bigbearzhu
  • 2,381
  • 6
  • 29
  • 44
0

Create a file named "yourpowershellfile.ps1" or Create e deployment step "Run a script".

Try this powershell with OctopusDeploy,

$FullPath = "C:\MyFolder"
if ($OctopusEnvironmentName -ceq 'Development')
{
  Write-Host "Console app will be execute"
  & "$FullPath\yourconsolefile.exe" | Write-Host
  Write-Host "Console app execution has finied"
}

You should try "Write-Output" instead of "Write-Host" Review the Octopus deploy task log.

Ozgur Kaya
  • 253
  • 2
  • 7
  • Thanks, but the problem of mine was actually there were really no log as my target of the application is set for 4.6.1 .Net framework. The tentacle doesn't have that version of .Net framework installed so the application failed to run. After I built with current target version, the Console.WriteLine does the correct job. – bigbearzhu May 09 '16 at 23:52
0

If you found this question because you really need to get the console output following executables run in your Octopus deployment steps you can use the following code. It took a bit of research to write the following function I happily re-use accross Octopus steps where I need the console output:

Function Invoke-CmdCommand{
    param(
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [ValidateScript({(Test-Path $_.Trim('"').Trim(''''))})]
        [string]$Executable,
        [string]$Parameters = '',
        [switch]$CmdEscape
    )
    BEGIN{
        Write-Verbose "Start '$($MyInvocation.Mycommand.Name)'" 
        $nl = [Environment]::NewLine
        $exitCode = 0
        $cmdOutput = [string]::Empty
        # next line wrap string in quotes if there is a space in the path
        #$Executable = (Format-WithDoubleQuotes $Executable -Verbose:$([bool]($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)))
        $command = "{0} {1}" -f $Executable, $Parameters
        Write-Verbose "COMMAND: $command"
        $terminatePrompt = "/C" # https://ss64.com/nt/cmd.html
        $comSpec = $env:ComSpec

        if($CmdEscape.IsPresent){
            $command = "`"$command`""
            Write-Verbose "ESCAPED COMMAND: $command"
        }        
    }
    PROCESS{
        $cmdResult = .{
            # script block exec: dot does not create local scope as opposed to ampersand
            .$comSpec $terminatePrompt $command '2>&1' | Out-String | Tee-Object -Variable cmdOutput
            return $LastExitCode
        }

        $exitCode = $cmdResult[$cmdResult.length-1]

        if($exitCode -ne 0){
          Write-Host "FAILED with ExitCode: $exitCode; ERROR executing the command:$nl$command$nl" -ForegroundColor Red
          Write-Host "ERROR executing the command:$nl$command" -ForegroundColor Yellow
        }else{
            Write-Host "SUCCESSFULLY executed the command:$nl$command$nl"
        }        
    }
    END{
        if($Host.Version.Major -le 3){
        return ,$cmdOutput # -NoEnumerate equivalent syntax since it is not available in version 2.0
        }else{
            Write-Output -NoEnumerate $cmdOutput
        }
        Write-Verbose "End '$($MyInvocation.Mycommand.Name)'"        
    }
}

USAGE:

Invoke-CmdCommand -Executable (Join-Path (Split-Path $env:ComSpec) ping.exe) -Parameters 'localhost'

OUTPUT:

Pinging localhost [::1] with 32 bytes of data:

Reply from ::1: time<1ms

Reply from ::1: time<1ms

Reply from ::1: time<1ms

Reply from ::1: time<1ms

Ping statistics for ::1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms

Emil
  • 2,196
  • 2
  • 25
  • 24
0

We just add a Deploy.ps1 to the package, with the following code:

& .\MyCompany.Foo.Bar.exe 2>&1

Resources: In the shell, what does " 2>&1 " mean?

Space Monkey
  • 981
  • 1
  • 9
  • 14