Search code examples
c#powershellpowershell-module

PowerShell Custom Object, ignore Properties at standard output


I write a PowerShell Module and I return a few custom objects. Now I want not to display every property, like the behavior of Get-ChildItem.

enter image description here

If I Display the members, there is no difference between e.g. Length and Exists. So why is Exists not displayed if I type gci "C:\temp\u_ex150113.log"? enter image description here

This is my custom object

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; }
}

Edit: Thanks to the answer I have now a much cleaner standard out.

For this I added three files to the Project:

  • 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')

# [...]

}

Solution

  • The output of Get-ChildItem is controlled by the PS1XML files.

    See:

    Get-Help PS1XML
    

    For your application I think adding a PSStandardMemebers property to your objects will probably do what you want:

    http://blogs.msdn.com/b/powershell/archive/2010/02/18/psstandardmembers-the-stealth-property.aspx