相关疑难解决方法(0)

在PowerShell中捕获EXE输出

先是一点背景.

我的任务是使用GPG(gnupg.org)使用Powershell脚本加密文件.我正在调用的特定exe只是gpg.exe.我想在执行命令时捕获输出.

例如,我在powershell中导入一个公钥,如下所示:

& $gpgLocation --import "key.txt"
Run Code Online (Sandbox Code Playgroud)

$ gpgLocation只是gpg.exe的文件位置(默认为"C:\ Program Files\GNU\GnuPG\gpg.exe"

我的全部问题是,如果我尝试:

& $gpgLocation --import "key.txt" | out-file gpgout.txt
Run Code Online (Sandbox Code Playgroud)

我得到的只是一个1kb文件,名称相应,但它完全是空白的.我已经为out-file尝试了几个标志,看看我是否遇到了怪癖.

我也尝试将命令发送到此代码(并使用通常的out-file等捕获输出):

param
(
    [string] $processname, 
    [string] $arguments
)

$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo;
$processStartInfo.FileName = $processname;
$processStartInfo.WorkingDirectory = (Get-Location).Path;
if($arguments) { $processStartInfo.Arguments = $arguments }
$processStartInfo.UseShellExecute = $false;
$processStartInfo.RedirectStandardOutput = $true;

$process = [System.Diagnostics.Process]::Start($processStartInfo);
$process.WaitForExit();
$process.StandardOutput.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我很绝望!

powershell

28
推荐指数
3
解决办法
4万
查看次数

标签 统计

powershell ×1