繁体   English   中英

具有Out-GridView的选择对象

[英]Select-Object with Out-GridView

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

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 

我的目标只是输出“ 名称”列,但是我不确定如何实现这一点。 我已经尝试了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.

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

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

遗憾的是, Out-GridView没有直接指定要显示的属性的方法,但是它使用标准的PowerShell机制来选择它们。 因此,我们可以改用它。 为此,您需要为MemberSet属性PSStandardMembers附加属性set 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

看起来非常像我对用户Adam 删除的问题的回答,部分出现在后续问题中

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

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

暂无
暂无

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

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