2016-12-29 79 views
0

我見過類似問題的一些答案,但我無法讓他們工作。可能是因爲我使用了powershell V2,無法重定向流。我的問題很簡單,我想記錄中刪除項cmdlet的詳細流在下面的腳本:Powershell V2 - 記錄刪除項目

$CSVList = (Get-Content "C:\Users\Leeds TX 11\Desktop\Test folder\Watchfolder\DAZN Production Numbers - purgelist.csv" | select -Skip 1) -split ','| Where {$_} 

$Netappdirectory = "C:\Users\Leeds TX 11\Desktop\Test folder\NetApp" 
$Logfile = "C:\Users\Leeds TX 11\Desktop\Test folder\logfile.txt" 

Get-ChildItem $Netappdirectory | 
    Where-Object {$CSVList -contains $_.BaseName} | 
    Remove-Item -Verbose 

回答

1

的PowerShell V2只允許成功(STDOUT)和錯誤(錯誤)輸出流的重定向。 Redirection for other streamsnot available prior to PowerShell v3。此外,Remove-Item沒有用於爲詳細(或調試)輸出定義變量的參數,因此無法像使用警告和錯誤輸出一樣在變量中捕獲該輸出。

你的,如果你不能升級到PowerShell的V3或更新最好的選擇可能是創建操作的transcript

Start-Transcript $Logfile -Append 
Get-ChildItem $Netappdirectory | ... | Remove-Item -Verbose 
Stop-Transcript 

否則,你將需要在一個單獨的PowerShell進程運行操作。當輸出返回到父進程時,額外的外部進程流會被轉移到成功和錯誤輸出流(STDOUT,STDERR)中。

powershell.exe -Command "&{Get-ChildItem $Netappdirectory | ... | Remove-Item -Verbose}" >> $Logfile 

雖然這是一個非常醜陋的方法,所以我不會推薦它。


旁註:即使PowerShell的V2具有Import-Csv cmdlet的,所以我不明白你爲什麼會想通過Get-Content-split效仿。