2014-08-29 31 views
1

我使用下面的代碼輸出共享文件夾結構到一個文本文件:PowerShell的 - 輸出目錄遞歸安慰和文件

$path = "\\server\share" 
$file = "c:\share-folders.txt" 
get-childitem "$path" -recurse -directory | select -expand fullname >> "$file" 

我也想在同一時間它被寫入到控制檯一種跟蹤狀態的方法。我用發球對象追加文件然後通過管道寫主機試過,但充其量我也只能夠輸出到文本文件,或者到控制檯,但不能同時一次。有誰知道完成這個的正確方法?


解決方案:添加到-append T恤的對象cmdlet的最後解決了問題。

$path = "\\server\share" 
$file = "c:\share-folders.txt" 
Get-ChildItem "$Path" -Recurse -Directory | Select -Expand FullName | Tee-Object -FilePath "$File" -Append 
+3

見三通對象的文檔。 – 2014-08-29 09:12:01

+0

您是否嘗試過http://ss64.com/ps/tee-object.html中的所有示例? – Pieter21 2014-08-29 09:12:19

+0

我感謝所有的答覆,併爲延遲迴複道歉 - 項目得到了擱置了幾個星期來處理一對夫婦他人起火。我曾嘗試過Tee-Object失敗,但我會再次嘗試使用下面的建議和ss64.com文檔,並稍後發佈更新。 – AKAJim 2014-09-22 16:22:11

回答

4

這對我的作品

get-childitem "$path" -recurse -directory | select -expand fullname | %{ Write-Output $_ ; $_ >> "$file"} 

在此之後,與T恤命令行:

get-childitem "$path" -recurse -directory | select -expand fullname | tee -file "$file" 
+1

這個工作一旦我添加 - 附加到Tee cmdlet。謝謝! – AKAJim 2014-09-23 05:24:00

0

或者試試這個:
首先選擇的數據,然後的foreach對象添加它將文本文件寫入控制檯。

$path = "\\server\share" 
$file = "c:\share-folders.txt" 

Get-ChildItem "$path" -Recurse -Directory | Select-Object -ExpandProperty FullName | ForEach-Object {Add-Content -Value $_ -Path $file; Write-Host -Object $_}