如何在Octopus自定义PowerShell脚本中捕获控制台应用程序输出?

时间:2016-05-06 04:55:35

标签: powershell console-application octopus-deploy

我在一开始就创建了一个带参数编号检查的控制台应用程序。在部署软件包之后,在部署PowerShell脚本部分中,我直接调用此应用程序而不使用参数来测试脚本。似乎Octopus只捕获退出代码,并且显示任务日志中捕获的应用程序根本没有输出。

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

然而,如果我只是简单地说“回答”&#39;测试&#39;&#34;甚至只是测试&#39;在脚本中的字符串中,输出是在Octopus部署任务日志中捕获的。知道在脚本中记录控制台应用程序的正确方法是什么?感谢。

4 个答案:

答案 0 :(得分:0)

抱歉,这不是Octopus的错,实际上是为.Net framework 4.6.1构建的控制台应用程序,但触手服务器只有4.5.2。当我通过远程桌面在该服务器上运行应用程序时,会弹出错误消息,说明4.6.1 .Net框架丢失。使用4.5.2重建应用程序修复了此问题。但是很难找到它,因为八达通任务日志与此无关。希望这将有助于其他人。

答案 1 :(得分:0)

创建名为“yourpowershellfile.ps1”的文件或创建e部署步骤“运行脚本”。

使用OctopusDeploy尝试此PowerShell,

_eAuthors.Add()

您应该尝试“写入输出”而不是“写入主机”查看Octopus部署任务日志。

答案 2 :(得分:0)

如果您发现此问题是因为您确实需要在Octopus部署步骤中运行可执行文件后获得控制台输出,则可以使用以下代码。编写以下函数需要进行一些研究,我很乐意重复使用Octopus步骤,我需要控制台输出:

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'

<强>输出:

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

答案 3 :(得分:0)

我们只需将Deploy.ps1添加到程序包中,并使用以下代码:

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

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