简体   繁体   中英

Select-Object with Out-GridView

I am creating a tool for our help desk to copy frequent resolution comments they may use when resolving tickets. I currently have:

Get-ChildItem ".\FileStore" | Out-GridView -PassThru -Title "Quick Notes" | Get-Content | Set-Clipboard

Which outputs something similar to (but in GridView):

Mode                LastWriteTime         Length Name                                                                                                                                          
----                -------------         ------ ----                                                                                                                                          
-a----       15/11/2018     14:38             14 1.txt                                                                                                                                         
-a----       15/11/2018     14:39             14 2.txt                                                                                                                                         
-a----       15/11/2018     14:39             14 3.txt                                                                                                                                         
-a----       15/11/2018     14:39             14 4.txt 

I am aiming to just have the Name column output, however I am unsure on how to achieve this. I have tried Select , Select-Object and Format-Table which do not work, as I receive the following:

Get-Content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of 
the parameters that take pipeline input.

Is it possible to output only the Name column to the GridView?

To allow Get-Content to find the file, you need to select more than just a Name , because Get-Content have no way to interpret the Name property. It have no matching parameter. Best thing to select is PSPath property, which contains fully qualified PowerShell path? and will match LiteralPath parameter of Get-Content cmdlet.

Sadly Out-GridView does not have direct way to specify which properties to display, but it use standard PowerShell mechanism for selecting them. So, we can use it instead. To do that you need to attach MemberSet property PSStandardMembers with property set DefaultDisplayPropertySet , which says which properties to display by default.

Get-ChildItem ".\FileStore" |
Select-Object Name, PSPath |
Add-Member -MemberType MemberSet `
           -Name PSStandardMembers `
           -Value ([System.Management.Automation.PSPropertySet]::new(
                      'DefaultDisplayPropertySet',
                      [string[]]('Name')
                  )) `
           -PassThru |
Out-GridView -PassThru -Title "Quick Notes" |
Get-Content | Set-Clipboard

That looks very like my answer to a deleted question from user Adam partly surfacing in a follow-up question

My answer (with a different path) was this:

Get-ChildItem -Path ".\FileStore" |
  Select-Object Name,FullName |
    Out-GridView -PassThru -Title "Quick Notes"|
      ForEach-Object{Get-Content $_.Fullname | Set-Clipboard -Append}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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