Powershell V2 - 记录删除项目

Powershell V2 - Logging remove-item

我已经看到了一些类似问题的答案,但是我无法使它们中的任何一个起作用。可能是因为我使用的是 Powershell 的 V2,无法重定向流。我的问题很简单,我希望在以下脚本中记录 remove-item cmdlet 的详细流:

$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

PowerShell v2 仅允许重定向成功 (STDOUT) 和错误 (STDERR) 输出流。 Redirection for other streams is not 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.