简体   繁体   English

Powershell V2-记录删除项

[英]Powershell V2 - Logging remove-item

I've seen a few answers to similar problems however I cannot get any of them to work. 我已经看到了一些类似问题的答案,但是我无法解决其中的任何问题。 Probably as I'm using V2 of powershell and cannot redirect streams. 可能是因为我使用的是Powershell V2,并且无法重定向流。 My question is simple, I wish to log the verbose stream of remove-item cmdlet in the following script: 我的问题很简单,我希望在以下脚本中记录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 allows only redirection of the Success (STDOUT) and Error (STDERR) output streams. PowerShell v2仅允许重定向成功(STDOUT)和错误(STDERR)输出流。 Redirection for other streams is not available prior to PowerShell v3 . 在PowerShell v3之前 ,其他流的重定向 不可用 Also, Remove-Item doesn't have a parameter for defining a variable for verbose (or debug) output, so you can't capture that output in a variable like you can with warning and error output. 另外, Remove-Item没有用于定义详细(或调试)输出变量的参数,因此您无法像警告和错误输出一样捕获该输出到变量中。

Your best option if you can't upgrade to PowerShell v3 or newer is probably creating a transcript of the operation: 如果你不能升级到PowerShell的V3或更新的版本可能创造一个你最好的选择成绩单操作:

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

Otherwise you would need to run the operation in a separate PowerShell process. 否则,您将需要在单独的PowerShell进程中运行该操作。 Additional streams of external processes are mangled into the Success and Error output streams (STDOUT, STDERR) when the output is returned to the parent process. 当输出返回到父流程时,其他外部流程流将合并到成功和错误输出流(STDOUT,STDERR)中。

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

That's a pretty ugly approach, though, so I wouldn't recommend it. 但是,这是一个非常丑陋的方法,所以我不建议这样做。


Side note: Even PowerShell v2 has an Import-Csv cmdlet, so I fail to see why you would want to emulate it via Get-Content and -split . 旁注:即使PowerShell v2也具有Import-Csv cmdlet,所以我看不到您为什么要通过Get-Content-split对其进行仿真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM