Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 格式化Remove Item命令的详细输出_Powershell - Fatal编程技术网

Powershell 格式化Remove Item命令的详细输出

Powershell 格式化Remove Item命令的详细输出,powershell,Powershell,我有以下命令: Get-ChildItem $build_path ` -Include *.bak, *.orig, *.txt, *.chirp.config ` -Recurse | Remove-Item -Verbose 从VS解决方案的生成文件夹中清除某些文件。我使用Verbose开关,以便查看哪些文件正在被删除。它工作正常,但输出太冗长: VERBOSE: Performing operation "Remove File" on Target "R:\Visua

我有以下命令:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | Remove-Item -Verbose
从VS解决方案的生成文件夹中清除某些文件。我使用Verbose开关,以便查看哪些文件正在被删除。它工作正常,但输出太冗长:

VERBOSE: Performing operation "Remove File" on Target "R:\Visual Studio 2010\Projects\SomeProject\SomeProject.Web.build\App_Readme\glimpse.mvc3.readme.txt".
VERBOSE: Performing operation "Remove File" on Target "R:\Visual Studio 2010\Projects\SomeProject\SomeProject.Web.build\App_Readme\glimpse.readme.txt".
我只想看看这样的东西:

Removing file \App_Readme\glimpse.mvc3.readme.txt".
Removing file \App_Readme\glimpse.readme.txt".
...

我知道我可以用foreach语句和Write Host命令来实现这一点,但我相信它可以通过一些管道或其他方式来实现。有什么想法吗?

为了能够修改消息,您需要首先将输出转换为不那么容易的内容。您可以参考本页上的答案:
捕捉输出。从那时起,您可以修改消息并以您的格式显示它,但foreach选项对我来说更容易

使用
foreach对象
非常简单:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | foreach{ "Removing file $($_.FullName)"; Remove-Item $_}
正如@user978511所指出的,使用详细输出更加复杂:

$ps = [PowerShell]::Create()

$null = $ps.AddScript(@'
    Get-ChildItem $build_path `
        -Include *.bak, *.orig, *.txt, *.chirp.config `
        -Recurse | Remove-Item -Verbose
'@)

$ps.Invoke()
$ps.Streams.Verbose -replace '(.*)Target "(.*)"(.*)','Removing File $2'

在PowerShell 3.0中,您可以将详细流写入输出流(例如4>&1),然后替换消息:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | Remove-Item -Verbose 4>&1 | Foreach-Object{ `
        Write-Host ($_.Message -replace'(.*)Target "(.*)"(.*)','Removing File $2') -ForegroundColor Yellow
}

这已经晚了几年,但它可能会帮助像我这样偶然发现它的其他人,所以我无论如何都会提供它

我会尝试删除文件,然后报告成功或失败。见下文:

$FileList = $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse

foreach ($File in $FileList)
{
    Try
    {
        Remove-Item $File.FullName -Force -ErrorAction Stop
        Write-Output "Deleted: $($File.Parent)\$($File.Name)"
    }
    Catch
    {
        Write-Output "Error deleting: $($File.Parent)\$($File.Name); Error Message: $($_.Exception.Message)"
    }
}
如果您想同时输出到控制台和记录到文件,可以在每行末尾使用Tee对象,从上面的Write output开始

| Tee-Object -FilePath $your_log_file -Append