重定向到'NUL'失败:FileStream无法打开Win32设备

mal*_*lat 5 powershell chocolatey appveyor

我试图从choco installAppVeyor中的命令中删除一些冗长.这是我一直在尝试(如这里建议):

if (Test-Path "C:/ProgramData/chocolatey/bin/swig.exe") {
    echo "using swig from cache"
} else {
    choco install swig > NUL
}
Run Code Online (Sandbox Code Playgroud)

然而它失败了:

Redirection to 'NUL' failed: FileStream will not open Win32 devices such as
disk partitions and tape drives. Avoid use of "\\.\" in the path.
At line:4 char:5
+     choco install swig > NUL
+     ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : RedirectionFailed

Command executed with exception: Redirection to 'NUL' failed: FileStream will not open Win32 devices such as disk partitions and tape drives. Avoid use of "\\.\" in the path.
Run Code Online (Sandbox Code Playgroud)

所以我的问题是有一种方法可以抑制choco installAppVeyor上PowerShell中命令的冗长程度吗?

Ans*_*ers 8

nul是批处理语法.在PowerShell中,您可以使用$null,正如Mathias R. Jessen在他的评论中指出的那样:

choco install swig > $null
Run Code Online (Sandbox Code Playgroud)

其他选项是管道进入Out-Nullcmdlet,在变量中收集输出,或将其转换为void:

choco install swig | Out-Null
$dummy = choco install swig
[void](choco install swig)
Run Code Online (Sandbox Code Playgroud)

  • 它不是特定于shell.它是`NUL`设备的有效本机Win32文件名.但是大多数.NET都不允许它们. (2认同)