PowerShell 自定义对象,忽略标准输出中的属性
PowerShell Custom Object, ignore Properties at standard output
我编写了一个 PowerShell 模块和 return 一些自定义对象。
现在我不想显示每个 属性,就像 Get-ChildItem
.
的行为一样
如果我显示成员,例如Length
和 Exists
。那么为什么我输入gci "C:\temp\u_ex150113.log"
却没有显示Exists
呢?
这是我的自定义对象
public class LogEntry
{
public DateTime Date { get; set; }
public DateTime Time { get; set; }
public DateTime DateTime => new DateTime(this.Date.Year, this.Date.Month, this.Date.Day, this.Time.Hour, this.Time.Minute, this.Time.Second);
public DateTime DateTimeLocalTime => DateTime.SpecifyKind(this.DateTime, DateTimeKind.Utc);
public IPAddress SourceIpAddress { get; set; }
public string Method { get; set; }
public string UriStem { get; set; }
public string UriQuery { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public IPAddress ClientIpAddress { get; set; }
public string UserAgent { get; set; }
public string Referrer { get; set; }
public string HttpStatus { get; set; }
public string ProtocolSubstatus { get; set; }
public string SystemErrorCodes { get; set; }
public int ServerSentBytes { get; set; }
public int ServerReceivedBytes { get; set; }
public int TimeTaken { get; set; }
}
编辑:
多亏了答案,我现在有了一个更清晰的标准。
为此,我向项目添加了三个文件:
- ConvertFromIISLogFile.format.ps1xml
- ConvertFromIISLogFile.ps1xml
- ConvertFromIISLogFile.psd1
ConvertFromIISLogFile.format.ps1xml
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>ConvertFromIISLogFile.LogEntry</Name>
<ViewSelectedBy>
<TypeName>ConvertFromIISLogFile.LogEntry</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<!-- Timestamp -->
<TableColumnHeader>
<Width>19</Width>
</TableColumnHeader>
<!-- IP -->
<TableColumnHeader>
<Width>15</Width>
</TableColumnHeader>
<!-- Port -->
<TableColumnHeader>
<Width>4</Width>
</TableColumnHeader>
<!-- Status -->
<TableColumnHeader>
<Width>6</Width>
</TableColumnHeader>
<!-- Method -->
<TableColumnHeader>
<Width>6</Width>
</TableColumnHeader>
<!-- Target -->
<TableColumnHeader/>
<!-- Query -->
<TableColumnHeader/>
<!-- UserAgent -->
<TableColumnHeader/>
<!-- Sent -->
<TableColumnHeader>
<Width>7</Width>
</TableColumnHeader>
<!-- Receive -->
<TableColumnHeader>
<Width>7</Width>
</TableColumnHeader>
<!-- TimeTaken -->
<TableColumnHeader>
<Width>9</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Timestamp</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>IP</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Port</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Status</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Method</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Target</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Query</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>UserAgent</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Sent</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Receive</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>TimeTaken</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
ConvertFromIISLogFile.ps1xml
<?xml version="1.0" encoding="utf-8"?>
<Types>
<Type>
<Name>ConvertFromIISLogFile.LogEntry</Name>
<Members>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<PropertySet>
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>Timestamp</Name>
<Name>IP</Name>
<Name>Port</Name>
<Name>Status</Name>
<Name>Method</Name>
<Name>Target</Name>
<Name>Query</Name>
<Name>UserAgent</Name>
<Name>Sent</Name>
<Name>Receive</Name>
<Name>TimeTaken</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</MemberSet>
<AliasProperty>
<Name>Timestamp</Name>
<ReferencedMemberName>DateTimeLocalTime</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>IP</Name>
<ReferencedMemberName>ClientIpAddress</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Status</Name>
<ReferencedMemberName>HttpStatus</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Sent</Name>
<ReferencedMemberName>ServerSentBytes</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Receive</Name>
<ReferencedMemberName>ServerReceivedBytes</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Target</Name>
<ReferencedMemberName>UriStem</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Query</Name>
<ReferencedMemberName>UriQuery</ReferencedMemberName>
</AliasProperty>
</Members>
</Type>
</Types>
ConvertFromIISLogFile.psd1
@{
# Script module or binary module file associated with this manifest.
RootModule = 'ConvertFromIISLogFile.dll'
# [...]
# Type files (.ps1xml) to be loaded when importing this module
TypesToProcess = @('ConvertFromIISLogFile.ps1xml')
# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = @('ConvertFromIISLogFile.format.ps1xml')
# [...]
}
Get-ChildItem 的输出由 PS1XML 文件控制。
参见:
Get-Help PS1XML
对于您的应用程序,我认为向您的对象添加 PSStandardMemebers 属性 可能会达到您想要的效果:
http://blogs.msdn.com/b/powershell/archive/2010/02/18/psstandardmembers-the-stealth-property.aspx
我编写了一个 PowerShell 模块和 return 一些自定义对象。
现在我不想显示每个 属性,就像 Get-ChildItem
.
如果我显示成员,例如Length
和 Exists
。那么为什么我输入gci "C:\temp\u_ex150113.log"
却没有显示Exists
呢?
这是我的自定义对象
public class LogEntry
{
public DateTime Date { get; set; }
public DateTime Time { get; set; }
public DateTime DateTime => new DateTime(this.Date.Year, this.Date.Month, this.Date.Day, this.Time.Hour, this.Time.Minute, this.Time.Second);
public DateTime DateTimeLocalTime => DateTime.SpecifyKind(this.DateTime, DateTimeKind.Utc);
public IPAddress SourceIpAddress { get; set; }
public string Method { get; set; }
public string UriStem { get; set; }
public string UriQuery { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public IPAddress ClientIpAddress { get; set; }
public string UserAgent { get; set; }
public string Referrer { get; set; }
public string HttpStatus { get; set; }
public string ProtocolSubstatus { get; set; }
public string SystemErrorCodes { get; set; }
public int ServerSentBytes { get; set; }
public int ServerReceivedBytes { get; set; }
public int TimeTaken { get; set; }
}
编辑: 多亏了答案,我现在有了一个更清晰的标准。
为此,我向项目添加了三个文件:
- ConvertFromIISLogFile.format.ps1xml
- ConvertFromIISLogFile.ps1xml
- ConvertFromIISLogFile.psd1
ConvertFromIISLogFile.format.ps1xml
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>ConvertFromIISLogFile.LogEntry</Name>
<ViewSelectedBy>
<TypeName>ConvertFromIISLogFile.LogEntry</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<!-- Timestamp -->
<TableColumnHeader>
<Width>19</Width>
</TableColumnHeader>
<!-- IP -->
<TableColumnHeader>
<Width>15</Width>
</TableColumnHeader>
<!-- Port -->
<TableColumnHeader>
<Width>4</Width>
</TableColumnHeader>
<!-- Status -->
<TableColumnHeader>
<Width>6</Width>
</TableColumnHeader>
<!-- Method -->
<TableColumnHeader>
<Width>6</Width>
</TableColumnHeader>
<!-- Target -->
<TableColumnHeader/>
<!-- Query -->
<TableColumnHeader/>
<!-- UserAgent -->
<TableColumnHeader/>
<!-- Sent -->
<TableColumnHeader>
<Width>7</Width>
</TableColumnHeader>
<!-- Receive -->
<TableColumnHeader>
<Width>7</Width>
</TableColumnHeader>
<!-- TimeTaken -->
<TableColumnHeader>
<Width>9</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Timestamp</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>IP</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Port</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Status</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Method</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Target</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Query</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>UserAgent</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Sent</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Receive</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>TimeTaken</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
ConvertFromIISLogFile.ps1xml
<?xml version="1.0" encoding="utf-8"?>
<Types>
<Type>
<Name>ConvertFromIISLogFile.LogEntry</Name>
<Members>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<PropertySet>
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>Timestamp</Name>
<Name>IP</Name>
<Name>Port</Name>
<Name>Status</Name>
<Name>Method</Name>
<Name>Target</Name>
<Name>Query</Name>
<Name>UserAgent</Name>
<Name>Sent</Name>
<Name>Receive</Name>
<Name>TimeTaken</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</MemberSet>
<AliasProperty>
<Name>Timestamp</Name>
<ReferencedMemberName>DateTimeLocalTime</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>IP</Name>
<ReferencedMemberName>ClientIpAddress</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Status</Name>
<ReferencedMemberName>HttpStatus</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Sent</Name>
<ReferencedMemberName>ServerSentBytes</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Receive</Name>
<ReferencedMemberName>ServerReceivedBytes</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Target</Name>
<ReferencedMemberName>UriStem</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Query</Name>
<ReferencedMemberName>UriQuery</ReferencedMemberName>
</AliasProperty>
</Members>
</Type>
</Types>
ConvertFromIISLogFile.psd1
@{
# Script module or binary module file associated with this manifest.
RootModule = 'ConvertFromIISLogFile.dll'
# [...]
# Type files (.ps1xml) to be loaded when importing this module
TypesToProcess = @('ConvertFromIISLogFile.ps1xml')
# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = @('ConvertFromIISLogFile.format.ps1xml')
# [...]
}
Get-ChildItem 的输出由 PS1XML 文件控制。
参见:
Get-Help PS1XML
对于您的应用程序,我认为向您的对象添加 PSStandardMemebers 属性 可能会达到您想要的效果:
http://blogs.msdn.com/b/powershell/archive/2010/02/18/psstandardmembers-the-stealth-property.aspx