【问题标题】:PowerShell and Java KeyTool - Suppress All KeyTool OutputPowerShell 和 Java KeyTool - 禁止所有 KeyTool 输出
【发布时间】:2021-02-03 13:56:06
【问题描述】:

我正在为我的公司创建一个有点复杂的构建后脚本环境,它将处理许多移动部件。使用 Powershell 脚本提供了更大的灵活性,因此我已经开始学习它。
但是,安装 Java 证书时会出现输出重定向问题。

现在,一切都按预期进行。检查证书,删除证书,甚至安装证书都可以正常工作 - 除了一个小问题:

(这是脚本成功运行的输出)

[Command:   C:\Program Files\Java\jre1.8.0_261\bin\keytool.exe]
[Arguments: -list -storepass "storepass" -keystore "C:\Program Files\Java\jre1.8.0_261\lib\security\cacerts" -alias "ourcert.crt"]
[Command:   C:\Program Files\Java\jre1.8.0_261\bin\keytool.exe]
[Arguments: -import -storepass "storepass" -keystore "C:\Program Files\Java\jre1.8.0_261\lib\security\cacerts" -alias "ourcert.crt" -file "\\unc\drive\share\path\ourcert.crt" -noprompt]
Certificate was added to keystore   <-- This line here
  Java Cert Installed in Store.

我正在使用“&$Command $args”方法调用所有外部命令,下面是我正在运行的脚本。

# This function is used all over the place to streamline the external command execution of
# KeyTool, Sonar Scanner, and MSBuild
function Invoke([String] $command, [String[]] $arguments)
{  
    Write-Host "    [Commnad:   $command]"
    Write-Host "    [Arguments: $arguments]"

    &$command $arguments
}

function ValidateKeyTool() 
{ # Our developers may or maynot have the same version of java so this is to find the most recent version on their system
    $path = [System.IO.Directory]::GetFiles("C:\Program Files (x86)\Java", "keytool.exe", [System.IO.SearchOption]::AllDirectories);
    $path = $path + [System.IO.Directory]::GetFiles("C:\Program Files\Java", "keytool.exe", [System.IO.SearchOption]::AllDirectories);
    $path = $path | Sort-Object -Descending;
    $script:KeyTool = $path | Select -First 1;
    $script:KeyStore = (Join-Path -Path (Split-Path (Split-Path $path)) -ChildPath "lib\security\cacerts");
    return ([System.IO.File]::Exists($KeyTool) -and [System.IO.File]::Exists($KeyStore));
}


function CheckCertExists() 
{
    if (ValidateKeyTool) 
    {
        $args = @("-list", "-storepass", """storepass""", "-keystore", """$KeyStore""", "-alias", """ourcert.crt""");
        Invoke $KeyTool $args | Out-Null;
        return ($LastExitCode -eq 0)
    }
    else 
    {
        throw "Unable to determine Java KeyTool or KeyStore";
    }
}

function InstallCert()
{
    if (!(CheckCertExists))
    {
        $args = @("-import", "-storepass", """storepass""", "-keystore", """$KeyStore""", "-alias", """ourcert.crt""", "-file", $CertFile, "-noprompt");
        Invoke $KeyTool $args | Out-Null;  #this DOESN'T Work, the Out-Null doesn't trap the output
        if ($LastExitCode -eq 0) 
        {
            Write-Host "  Java Cert Installed in Store."
        }
        else 
        {
            throw "Error occured attempting to Install the Java Cert into the Store."
        }
    }
    else 
    {
        Write-Host "  Java Cert already installed."
    }    
}

所有带有“| Out-Null”的 KeyTool 执行都会按预期捕获输出,对于 -list,对于 -delete,但不是对于 -import。无论我尝试了什么,带有“-import”的密钥工具总是会产生“证书已添加到密钥库”输出消息。我想压制它,并且只为成功/失败退出 $LastExitCode。

【问题讨论】:

    标签: java powershell redirect keytool


    【解决方案1】:

    可能的原因是消息正在输出到另一个output stream。例如。它可能不是将消息输出到标准成功流 (1),而是将消息输出到错误流 (2) 或警告流 (3) 或其他流。流水线到| Out-Null 只会处理成功流,例如:

    PS C:\> Write-Output "hi" | Out-Null
    PS C:\>
    PS C:\> Write-Warning "hi" | Out-Null
    WARNING: hi
    

    抑制来自所有流的所有消息的钝锤方法是redirect all the streams,如下所示:

    Invoke $KeyTool $args *> $null
    

    “更好”的方法是,如果您知道将消息发送到哪个流(例如错误流 (2)),您可以重定向各个流。为此,您可以将错误流 (2) 重定向到成功流 (1),然后将成功流 (1) 重定向到 $null

    Invoke $KeyTool $args 2>&1 > $null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2014-11-16
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多