具有Out-GridView的选择对象

时间:2018-11-15 16:10:18

标签: powershell

我正在为我们的服务台创建一个工具,用于复制解决票证时可能会使用的频繁解决方案注释。我目前有:

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

哪个输出类似于(但在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 

我的目标只是输出 Name 列,但是我不确定如何实现这一点。我尝试了SelectSelect-ObjectFormat-Table无效,因为收到以下消息:

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.

是否可以仅将名称列输出到GridView?

2 个答案:

答案 0 :(得分:1)

要允许Get-Content查找文件,您需要选择的不只是Name,因为Get-Content无法解释Name属性。没有匹配的参数。最好选择PSPath属性,其中包含完全限定的PowerShell路径?并将与LiteralPath cmdlet的Get-Content参数匹配。

不幸的是,Out-GridView没有直接指定要显示的属性的方法,但是它使用标准的PowerShell机制来选择它们。因此,我们可以改用它。为此,您需要将MemberSet属性PSStandardMembers附加到属性集DefaultDisplayPropertySet,该属性集会默认显示要显示的属性。

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

答案 1 :(得分:0)

这很像我对用户deleted questionAdam的回答,部分显示在follow-up question

我的回答(使用不同的路径)是这样:

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