2013-07-30 61 views
1

我有一个命令可以获取共享大小 - 但是它会根据谁自然运行脚本的权限来错误地控制台发生错误。关闭回显Powershell

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

我想抑制错误,如可能的话将ECHO关闭?

回答

5

您可以通过错误流重定向到$null,如取消错误消息:

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

您可以将ErrorAction参数添加到你的电话给Get-ChildItem(我认为这是错误的来源),像这样:

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

请看ErrorAction和更详细的$ ErrorActionPreference内置变量(获取帮助参阅about_Preference_Variables)。并且要小心这些选项 - 隐藏错误通常不是一个好主意。