简体   繁体   English

Start-Process 重定向输出到 $null

[英]Start-Process redirect output to $null

I'm starting a new process like this:我正在开始一个这样的新过程:

$p = Start-Process -FilePath $pathToExe -ArgumentList $argumentList -NoNewWindow -PassThru -Wait

if ($p.ExitCode -ne 0)
{
    Write-Host = "Failed..."
    return
}

And my executable prints a lot to console.我的可执行文件打印了很多到控制台。 Is is possible to not show output from my exe?是否可以不显示我的 exe 的输出?

I tried to add -RedirectStandardOutput $null flag but it didn't work because RedirectStandardOutput doesn't accept null .我试图添加-RedirectStandardOutput $null标志但它没有用,因为RedirectStandardOutput不接受null I also tried to add | Out-Null我也尝试添加| Out-Null | Out-Null to the Start-Process function call - didn't work.Start-Process函数调用的| Out-Null - 不起作用。 Is it possible to hide output of the exe that I call in Start-Process ?是否可以隐藏我在Start-Process调用的 exe 的输出?

You're invoking the executable synchronously ( -Wait ) and in the same window ( -NoNewWindow ).同步调用可执行文件( -Wait ),并在同一个窗口-NoNewWindow )。

You don't need Start-Process for this kind of execution at all - simply invoke the executable directly , using & , the call operator, which allows you to:你不需要Start-Process这类执行在所有-只是调用直接执行,使用& ,电话运营商,它允许您:

  • use standard redirection techniques to silence (or capture) output使用标准重定向技术来静音(或捕获)输出
  • and to check automatic variable $LASTEXITCODE for the exit code并检查退出代码的自动变量$LASTEXITCODE
& $pathToExe $argumentList *> $null
if ($LASTEXITCODE -ne 0) {
  Write-Warning "Failed..."
  return
}

If you still want to use Start-Process , see sastanin's helpful answer .如果您仍想使用Start-Process ,请参阅sastanin 的有用答案

Using call operator & and | Out-Null使用调用运算符&| Out-Null | Out-Null is a more popular option, but it is possible to discard standard output from Start-Process . | Out-Null是一个更流行的选项,但可以丢弃Start-Process标准输出。

Apparently, NUL in Windows seems to be a virtual path in any folder .显然, Windows 中的NUL似乎是任何文件夹中的虚拟路径 -RedirectStandardOutput requires a non-empty path, so $null parameter is not accepted, but "NUL" is (or any path that ends with \\NUL ). -RedirectStandardOutput需要非空路径,因此不接受$null参数,但"NUL"是(或以\\NUL结尾的任何路径)。

In this example the output is suppressed and the file is not created:在这个例子中,输出被抑制,文件没有被创建:

> Start-Process -Wait -NoNewWindow ping localhost -RedirectStandardOutput ".\NUL" ; Test-Path ".\NUL"
False
> Start-Process -Wait -NoNewWindow ping localhost -RedirectStandardOutput ".\stdout.txt" ; Test-Path ".\stdout.txt"
True

-RedirectStandardOutput "NUL" works too. -RedirectStandardOutput "NUL"也有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM