自定义 PowerShell 管理单元:自定义格式不起作用

发布于 2024-09-12 04:08:27 字数 1657 浏览 11 评论 0原文

我使用本文编写了我的第一个自己的 Powershell Cmdlet 和 Snapin。而且效果很好。

但我从自己的数据类返回一组对象,该数据类具有四个属性,并且我希望 Powershell 默认情况下仅显示其中一个属性。所以我使用文章的这一部分来创建这种格式file:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
    <ViewDefinitions>
        <View>
            <Name>RemoteFile</Name>
            <ViewSelectedBy>
                <TypeName>MyFullNamespace.RemoteFileData</TypeName>
            </ViewSelectedBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader />
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Filename</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>

并在管理单元中链接它:

    public override string[] Formats
    {
        get { return new string[] { "MyFormatFilename.ps1xml" }; }
    }

但是当我使用 installutil 安装管理单元、使用 Add-PSSnapin 并调用我的 Cmdlet 时,会显示对象的所有属性。

我做错了什么?

I used this article to write my first own Powershell Cmdlet and Snapin. And it works fine.

But I return a set of objects from my own data class, which has four properties and I want Powershell to display just one of these properties by default. So I used this part of the article to create this format file:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
    <ViewDefinitions>
        <View>
            <Name>RemoteFile</Name>
            <ViewSelectedBy>
                <TypeName>MyFullNamespace.RemoteFileData</TypeName>
            </ViewSelectedBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader />
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Filename</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>

and link it in the Snapin:

    public override string[] Formats
    {
        get { return new string[] { "MyFormatFilename.ps1xml" }; }
    }

But when I install the Snapin with installutil, use Add-PSSnapin and call my Cmdlet, all Properties of the objects are displayed.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2024-09-19 04:08:27

一切看起来都正确,只是我不确定在没有定义列标题标签的情况下它的行为如何。尝试添加此节点而不是空节点:

<TableColumnHeader>
  <Label>FileName</Label>
</TableColumnHeader>

另外,当通过 Add-PSSnapin 加载文件时,请确保文件 MyFormatFilename.ps1xml 与管理单元位于同一目录中。另外,可能是一个废话,但请仔细检查 元素中指定的类型名称中的拼写错误。

更新:我尝试了上面列出的 XML,它对我有用。我将其复制到 Notepad2 中并将其保存到 C:\temp\test.ps1xml 然后执行:

1# $obj = new-object psobject
2# $obj.psobject.TypeNames.Insert(0, 'MyFullNamespace.RemoteFileData')
3# Add-Member -InputObject $obj -MemberType NoteProperty -Name Filename `
              -Value 'some-remotefile.txt'
4# Add-Member -InputObject $obj -MemberType NoteProperty -Name Dummy `
              -Value 'dummy prop'
5# $obj.psobject.TypeNames
MyFullNamespace.RemoteFileData
System.Management.Automation.PSCustomObject
System.Object
6# $obj

Filename                                                    Dummy
--------                                                    -----
some-remotefile.txt                                         dummy prop


7# Update-FormatData C:\temp\test.ps1xml
8# $obj

Filename
--------
some-remotefile.txt

我会仔细检查完整的类型名 instance.GetType().FullName 并仔细检查格式的内容文件。确保它位于您注册管理单元的同一目录中。

Everything looks correct except that I'm not sure how it behaves with no column header label defined. Try adding this node instead of your empty one:

<TableColumnHeader>
  <Label>FileName</Label>
</TableColumnHeader>

Also make sure the file MyFormatFilename.ps1xml is in the same dir with the snapin when it is being loaded via Add-PSSnapin. Also, probably a duh, but double check for typos in the type name specified in the <TypeName> element.

Update: I tried your XML as listed above and it works for me. I copied it into Notepad2 and saved it to C:\temp\test.ps1xml then executed:

1# $obj = new-object psobject
2# $obj.psobject.TypeNames.Insert(0, 'MyFullNamespace.RemoteFileData')
3# Add-Member -InputObject $obj -MemberType NoteProperty -Name Filename `
              -Value 'some-remotefile.txt'
4# Add-Member -InputObject $obj -MemberType NoteProperty -Name Dummy `
              -Value 'dummy prop'
5# $obj.psobject.TypeNames
MyFullNamespace.RemoteFileData
System.Management.Automation.PSCustomObject
System.Object
6# $obj

Filename                                                    Dummy
--------                                                    -----
some-remotefile.txt                                         dummy prop


7# Update-FormatData C:\temp\test.ps1xml
8# $obj

Filename
--------
some-remotefile.txt

I would double check the full typename instance.GetType().FullName and also double check the contents of the format file. Make sure it is in the same dir that you registered the snapin from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文