2011-02-18 58 views
2

我使用DU.exe在PowerShell脚本捕捉到一个远程文件夹的大小,如下面的代码:PowerShell的Du.exe错误

$Duexe ="c:\du\du.exe" 

$unc = "\\$server\$Letter$\$Name" 

write-host "Processing: " $unc 

$stuff = du -q "\\$server\$Letter$\$Name" 2>&1 

$formated = $stuff | Format-Table -auto 

write-host $stuff 

我重定向stderror停止造成的错误“-q”开关。然而输出连续发生如下错误:

System.Management.Automation.RemoteException 

In context: 

Files:  290215 Directories: 2246 Size:   128,529,542,967 bytes Size on disk: 128,529,542,967 bytes System.Management.Automation.RemoteException 

这是为什么?如果我在powershell之外运行du,那么在相同的unc路径上不会发生错误。

+0

我刚刚发现,如果我trunror重定向它并没有显示它显示的messgae howevere: – Daniel 2011-02-18 12:24:45

回答

2

您并没有太多停止错误,因为将错误“重定向”到您在变量$stuff中捕获的输出流。尝试只是错误流重定向到$null忽略它:

$stuff = du -q "\\$server\$Letter$\$Name" 2> $null 

$stuff | Format-Table -auto 

BTW,你不需要写承办的“格式化”的东西。 Format-Table将自动输出到主机。

1

您不会在批量样式为>的Powershell中重定向输出。只需使用$stuff = du -q $unc即可将du输出转换为变量。

顺便说一句,你打印$stuff,但将格式化的内容设置为$formated。这是打算?