关闭echo out Powershell

关闭echo out Powershell,powershell,Powershell,我有一个获取共享大小的命令,但是它会向控制台发送错误消息,因为权限取决于自然运行脚本的人 $shareSize = [math]::round((Get-ChildItem $($share.path) -Recurse -Force | Measure-Object -Property Length -Sum ).Sum/1GB) 我想抑制错误,如关闭回声,如果可能的话 您可以通过将错误流重定向到$null来抑制错误消息,例如: [math]::round((Get-ChildItem $

我有一个获取共享大小的命令,但是它会向控制台发送错误消息,因为权限取决于自然运行脚本的人

 $shareSize = [math]::round((Get-ChildItem $($share.path) -Recurse -Force | Measure-Object -Property Length -Sum ).Sum/1GB)

我想抑制错误,如关闭回声,如果可能的话

您可以通过将错误流重定向到
$null
来抑制错误消息,例如:

[math]::round((Get-ChildItem $($share.path) -Recurse -Force 2>$null

您可以通过将错误流重定向到
$null
来抑制错误消息,例如:

[math]::round((Get-ChildItem $($share.path) -Recurse -Force 2>$null

您可以将ErrorAction参数添加到调用中以获取ChildItem(我假设这就是错误的来源),如下所示:

 $shareSize = [math]::round((Get-ChildItem $($share.path) -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum ).Sum/1GB)

有关更多详细信息,请查看ErrorAction和$ErrorActionPreference内置变量(获取有关\u Preference\u变量的帮助)。注意这些选项——隐藏错误通常不是一个好主意。

您可以将ErrorAction参数添加到调用中以获取ChildItem(我假设这就是错误的来源),如下所示:

 $shareSize = [math]::round((Get-ChildItem $($share.path) -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum ).Sum/1GB)
有关更多详细信息,请查看ErrorAction和$ErrorActionPreference内置变量(获取有关\u Preference\u变量的帮助)。注意这些选项——隐藏错误通常不是一个好主意