且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell PipelineVariable参数仅包含PSCustomObject集合中的第一个值

更新时间:2023-11-24 09:35:28

不幸的是,-PipelineVariable不适用于Remove-VBComments之类的脚本cmdlet.这就是为什么将Select-Object运行成功的原因-这是C#cmdlet.但是您实际上并不需要它,因为foreach循环中的$_具有$files变量中应包含的所有信息.更新代码看起来像

Unfortunately -PipelineVariable doesn't work with script cmdlets like Remove-VBComments. That's why moving if to Select-Object worked - it's a C# cmdlet. But you don't actually need it since $_ in the foreach loop has all of the information that would have been in the $files variable. The update code would look something like

$filesToStripComment |
    Remove-VBComments |
    &{Process{
                $UncommentedFileName = $_.UncommentedFileName
                Get-Content -Path $UncommentedFileName |
                    Measure-Object -Line |
                        Select-Object -Property `
                            @{n="FileName";e={$UncommentedFileName}}, 
                            @{n="LoC";e={$_.Lines}} `
     } }