代码之家  ›  专栏  ›  技术社区  ›  Franco Tiveron

从powershell脚本调用外部命令时显示的日志

  •  1
  • Franco Tiveron  · 技术社区  · 3 年前

    在powershell模块中,有一个类具有以下方法(此处简化)。它只是简单地调用docker来自动构建一个图像。

    一切正常,图像构建正确。我无法理解的是,为什么docker的日志没有打印在控制台中(如果我直接调用同一个docker命令,而不是在powershell模块中)。

    [void] BuildImage() {
        $imageId = ...
        docker build -f ../Dockerfile . -t $imageId
    }
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Santiago Squarzon    3 年前

    你有一个 void method ,它不产生输出。如果要将输出发送到 success stream (stdout) ,然后将返回类型更改为 [string[]] 和使用 return :

    [string[]] BuildImage() {
        $imageId = ...
        return docker build -f ../Dockerfile . -t $imageId
    }
    

    如果您有兴趣将输出发送到其他PowerShell流,可以尝试以下示例,其中包括 redirection of errors 然后检查输出类型是否为 ErrorRecord 它将其发送到 error stream 否则它会将其发送到 information stream

    [void] BuildImage() {
        $imageId = ...
        docker build -f ../Dockerfile . -t $imageId 2>&1 | ForEach-Object {
            if($_ -is [System.Management.Automation.ErrorRecord]) {
                return Write-Error $_
            }
    
            Write-Host $_
        }
    }
    
    推荐文章