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

问题描述 投票:0回答:3

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

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

但是,如果我只是在脚本中放入“echo'test'”或甚至只是'test'字符串,则输出会在Octopus部署任务日志中捕获。知道在脚本中记录控制台应用程序的正确方法是什么?谢谢。

powershell console-application octopus-deploy
3个回答
0
投票

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


0
投票

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

试试这款带有OctopusDeploy的PowerShell,

$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"
}

您应该尝试“Write-Output”而不是“Write-Host”查看Octopus部署任务日志。


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)'"        
    }
}

用法:

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

© www.soinside.com 2019 - 2024. All rights reserved.