自定义 PowerShell 管理单元:自定义格式不起作用
我使用本文编写了我的第一个自己的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一切看起来都正确,只是我不确定在没有定义列标题标签的情况下它的行为如何。尝试添加此节点而不是空节点:
另外,当通过 Add-PSSnapin 加载文件时,请确保文件
MyFormatFilename.ps1xml
与管理单元位于同一目录中。另外,可能是一个废话,但请仔细检查
元素中指定的类型名称中的拼写错误。更新:我尝试了上面列出的 XML,它对我有用。我将其复制到 Notepad2 中并将其保存到 C:\temp\test.ps1xml 然后执行:
我会仔细检查完整的类型名
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:
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:
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.