2011-02-18 34 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。這是打算?