简体   繁体   English

通过 Out-GridView 管道后获取原始对象

[英]Getting original object after piping via Out-GridView

I need to show a list of file names without paths and open the selected file.我需要显示没有路径的文件名列表并打开所选文件。

I can get it to work with full paths:我可以让它与完整路径一起工作:

Get-ChildItem *.txt -Recurse | Sort-Object Name| Out-GridView -PassThru | Invoke-Item

But when I try to show only the file names it fails:但是当我尝试只显示文件名时,它失败了:

Get-ChildItem *.txt -Recurse | Sort-Object Name| Select-Object Name | Out-GridView -PassThru | Invoke-Item

By piping it through Get-Member I understand that Select-Object Name striped all non-Name properties.通过通过 Get-Member 进行管道传输,我了解到Select-Object Name所有非 Name 属性条带化。 So how can I trace the original file object from what I got from GridView?那么如何从 GridView 得到的文件对象中追踪原始文件对象呢?

The problem is, that the Invoke-Item needs the path and not only the filename.问题是,调用项需要路径而不仅仅是文件名。 You could store the get-childitem in a temporary variable:您可以将 get-childitem 存储在临时变量中:

$tmp = Get-ChildItem *.txt -Recurse | Sort-Object Name
$tmp | Select-Object Name | Out-GridView -PassThru
$tmp | Invoke-Item

Is that what you wanted?那是你想要的吗? Please let me know if it worked, and if it did please mark my post as the answer.请让我知道它是否有效,如果有效,请将我的帖子标记为答案。 :) :)

You might want to use the DefaultDisplayPropertySet property of the hidden PSStandardMembers set for this:您可能希望PSStandardMembers设置隐藏的PSStandardMembersDefaultDisplayPropertySet属性:

$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]@('Name'))
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)

Get-ChildItem *.txt -Recurse | Select-Object * |
ForEach-Object {
    $_ | Add-Member MemberSet PSStandardMembers $PSStandardMembers; $_
} | Out-Gridview -PassThru | Select-Object FullName
  • Get-ChildItem *.txt has a default display propery set of: LastWriteTime , Length , Name Get-ChildItem *.txt有一个默认的显示属性集: LastWriteTimeLengthName
  • Select-Object * strips off the complete property set (displays all properties) Select-Object *剥离完整的属性集(显示所有属性)
  • Add-Member MemberSet PSStandardMembers $PSStandardMembers adds a new display set with just the Name property and keeps the rest of the properties hidden Add-Member MemberSet PSStandardMembers $PSStandardMembers添加一个只有Name属性的新显示集,并隐藏其余的属性
  • Select-Object FullName reveals the hidden FullName property Select-Object FullName显示隐藏的FullName属性

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

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