如何设置PowerShell Cmdlet的默认输出格式?

时间:2017-07-21 10:34:39

标签: c# powershell

我在C#中创建一个PowerShell Cmdlet来读取JSON文件中的字段并输出一些对象。对象是一个简单的POCO类型:

public class FieldDefinition
{
    public FieldDefinition(string name, TypeCode code)
    {
        Name = name;
        Code = code;
    }

    public string Name { get; }

    public TypeCode Code { get; }
}

编写输出时,条目显示为:

PS ~Debug> Get-Field

Name : Session
Code : Int64

Name : Instance
Code : String

理想情况下,我希望默认演示文稿为表格:

PS ~Debug> Get-Field | Format-Table

Name     Code
----     ----
Session  Int64
Instance String

当我调用Get-Process之类的命令时,我总是会看到表格格式。如何让命令将其输出显示为表格?

4 个答案:

答案 0 :(得分:4)

每次命令输出数据表时,输出实际上(无形地)通过管道输出到Out-Default命令。这反过来管道数据(无形) 到defualt目的地,默认情况下是“out-host”,即终端。

out-host使用以下文件中包含的格式化信息将数据格式化为表格形式:

C:\ Windows \ systems32 \ windowspowershell \ V1.0 \ DotNetTypes.format.ps1xml (注意:不要编辑这个文件,因为它是经过数字签名的。所以如果你进行最微小的改动,powershell将停止工作)

它通过查找xml文件来查找匹配对象的条目(可以使用get-member找到)

所有out- *命令也使用相同的xml文件(尽管write-host是一个例外,稍后会介绍)。

但是,命令可能会输出一个在xml文件中没有预定义视图的表。在这种情况下,out- *命令 查找辅助xml文件以获取格式指导:

C:\ Windows \ systems32 \ windowspowershell \ V1.0 \ Types.ps1xml

参考:https://codingbee.net/tutorials/powershell/powershell-changing-a-command-outputs-default-formatting

答案 1 :(得分:3)

您需要添加一个format.ps1xml文件来设置输出的布局。

https://msdn.microsoft.com/en-us/library/gg574424(v=vs.85).aspx

我以前为脚本模块做过这个。

可以在本地计算机上找到示例 C:\ Windows \ System32下\ WindowsPowershell \ V1.0

上述位置有一些* .ps1xml文件。

答案 2 :(得分:1)

来自DHCP Microsoft模块的清单文件示例:

        @{

        GUID = '90eaa9df-133a-450c-8728-91055cd946a1'

        Author = 'Microsoft Corporation'

        CompanyName = 'Microsoft Corporation'

        Copyright = '© Microsoft Corporation. All rights reserved.'

        ModuleVersion = '2.0.0.0'

        PowerShellVersion = '3.0'

        ClrVersion = "4.0"

        FormatsToProcess = 'DhcpServerPsProvider.Format.ps1xml' 

        HelpInfoUri="http://go.microsoft.com/fwlink/?linkid=285742"

        TypesToProcess = 'DhcpServerPsProvider.Types.PS1XML'    

        NestedModules = @("PS_DhcpServerAuditLog_v1.0.0.cdxml", "PS_DhcpServerDatabase_v1.0.0.cdxml", "PS_DhcpServerInDC_v1.0.0.cdxml", "PS_DhcpServerSetting_v1.0.0.cdxml", "PS_DhcpServerV4Binding_v1.0.0.cdxml", "PS_DhcpServerv4Class_v1.0.0.cdxml", "PS_DhcpServerv4DnsSetting_v1.0.0.cdxml", "PS_DhcpServerV4ExclusionRange_v1.0.0.cdxml", "PS_DhcpServerv4FailoverReplication_v1.0.0.cdxml", "PS_DhcpServerv4FailoverScope_v1.0.0.cdxml", "PS_DhcpServerv4Failover_v1.0.0.cdxml", "PS_DhcpServerv4FilterList_v1.0.0.cdxml", "PS_DhcpServerv4Filter_v1.0.0.cdxml", "PS_DhcpServerv4FreeIPAddress_v1.0.0.cdxml", "PS_DhcpServerv4Lease_v1.0.0.cdxml", "PS_DhcpServerV4OptionDefinition_v1.0.0.cdxml", "PS_DhcpServerV4OptionValue_v1.0.0.cdxml", "PS_DhcpServerv4PolicyIPRange_v1.0.0.cdxml", "PS_DhcpServerv4Policy_v1.0.0.cdxml", "PS_DhcpServerV4Reservation_v1.0.0.cdxml", "PS_DhcpServerV4SuperScopeStatistics_v1.0.0.cdxml", "PS_DhcpServerV4Scope_v1.0.0.cdxml", "PS_DhcpServerv4Statistics_v1.0.0.cdxml", "PS_DhcpServerV4Superscope_v1.0.0.cdxml", "PS_DhcpServerV6Binding_v1.0.0.cdxml", "PS_DhcpServerv6Class_v1.0.0.cdxml", "PS_DhcpServerv6DnsSetting_v1.0.0.cdxml", "PS_DhcpServerV6ExclusionRange_v1.0.0.cdxml", "PS_DhcpServerv6FreeIPAddress_v1.0.0.cdxml", "PS_DhcpServerv6Lease_v1.0.0.cdxml", "PS_DhcpServerV6OptionDefinition_v1.0.0.cdxml", "PS_DhcpServerV6OptionValue_v1.0.0.cdxml", "PS_DhcpServerV6Reservation_v1.0.0.cdxml", "PS_DhcpServerV6ScopeStatistics_v1.0.0.cdxml", "PS_DhcpServerV6Scope_v1.0.0.cdxml", "PS_DhcpServerv6StatelessStatistics_v1.0.0.cdxml", "PS_DhcpServerv6StatelessStore_v1.0.0.cdxml", "PS_DhcpServerv6Statistics_v1.0.0.cdxml", "PS_DhcpServerVersion_v1.0.0.cdxml", "PS_DhcpServer_v1.0.0.cdxml", "PS_DhcpServerSecurityGroup_v1.0.0.cdxml", "PS_DhcpServerv4IPRecord_v1.0.0.cdxml", "PS_DhcpServerV4ScopeStatistics_v1.0.0.cdxml" , "PS_DhcpServerV4MulticastScope_v1.0.0.cdxml", "PS_DhcpServerv4MulticastExclusionRange_v1.0.0.cdxml" , "PS_DhcpServerv4MulticastLease_v1.0.0.cdxml" , "PS_DhcpServerv4MulticastScopeStatistics_v1.0.0.cdxml" , "PS_DhcpServerDnsCredential_v1.0.0.cdxml" , "DhcpServerMigration.psm1")

    FunctionsToExport = @("Add-DhcpServerInDC", "Add-DhcpServerv4Class", "Add-DhcpServerv4ExclusionRange", "Add-DhcpServerv4Failover", "Add-DhcpServerv4FailoverScope", "Add-DhcpServerv4Filter", "Add-DhcpServerv4Lease", "Add-DhcpServerv4OptionDefinition", "Add-DhcpServerv4Policy", "Add-DhcpServerv4PolicyIPRange", "Add-DhcpServerv4Reservation", "Add-DhcpServerv4Scope", "Add-DhcpServerv4Superscope", "Add-DhcpServerv6Class", "Add-DhcpServerv6ExclusionRange", "Add-DhcpServerv6Lease", "Add-DhcpServerv6OptionDefinition", "Add-DhcpServerv6Reservation", "Add-DhcpServerv6Scope", "Backup-DhcpServer", "Export-DhcpServer", "Get-DhcpServerAuditLog", "Get-DhcpServerDatabase", "Get-DhcpServerInDC", "Get-DhcpServerSetting", "Get-DhcpServerv4Binding", "Get-DhcpServerv4Class", "Get-DhcpServerv4DnsSetting", "Get-DhcpServerv4ExclusionRange", "Get-DhcpServerv4Failover", "Get-DhcpServerv4Filter", "Get-DhcpServerv4FilterList", "Get-DhcpServerv4FreeIPAddress", "Get-DhcpServerv4Lease", "Get-DhcpServerv4OptionDefinition", "Get-DhcpServerv4OptionValue", "Get-DhcpServerv4Policy", "Get-DhcpServerv4PolicyIPRange", "Get-DhcpServerv4Reservation", "Get-DhcpServerv4Scope", "Get-DhcpServerv4SuperScopeStatistics", "Get-DhcpServerv4Statistics", "Get-DhcpServerv4Superscope", "Get-DhcpServerv6Binding", "Get-DhcpServerv6Class", "Get-DhcpServerv6DnsSetting", "Get-DhcpServerv6ExclusionRange", "Get-DhcpServerv6FreeIPAddress", "Get-DhcpServerv6Lease", "Get-DhcpServerv6OptionDefinition", "Get-DhcpServerv6OptionValue", "Get-DhcpServerv6Reservation", "Get-DhcpServerv6Scope", "Get-DhcpServerv6ScopeStatistics", "Get-DhcpServerv6StatelessStatistics", "Get-DhcpServerv6StatelessStore", "Get-DhcpServerv6Statistics", "Get-DhcpServerVersion", "Import-DhcpServer", "Invoke-DhcpServerv4FailoverReplication", "Remove-DhcpServerInDC", "Remove-DhcpServerv4Class", "Remove-DhcpServerv4ExclusionRange", "Remove-DhcpServerv4Failover", "Remove-DhcpServerv4FailoverScope", "Remove-DhcpServerv4Filter", "Remove-DhcpServerv4Lease", "Remove-DhcpServerv4OptionDefinition", "Remove-DhcpServerv4OptionValue", "Remove-DhcpServerv4Policy", "Remove-DhcpServerv4PolicyIPRange", "Remove-DhcpServerv4Reservation", "Remove-DhcpServerv4Scope", "Remove-DhcpServerv4Superscope", "Remove-DhcpServerv6Class", "Remove-DhcpServerv6ExclusionRange", "Remove-DhcpServerv6Lease", "Remove-DhcpServerv6OptionDefinition", "Remove-DhcpServerv6OptionValue", "Remove-DhcpServerv6Reservation", "Remove-DhcpServerv6Scope", "Restore-DhcpServer", "Set-DhcpServerAuditLog", "Set-DhcpServerDatabase", "Set-DhcpServerSetting", "Set-DhcpServerv4Binding", "Set-DhcpServerv4Class", "Set-DhcpServerv4DnsSetting", "Set-DhcpServerv4Failover", "Set-DhcpServerv4FilterList", "Set-DhcpServerv4OptionDefinition", "Set-DhcpServerv4OptionValue", "Set-DhcpServerv4Policy", "Set-DhcpServerv4Reservation", "Set-DhcpServerv4Scope", "Set-DhcpServerv6Binding", "Set-DhcpServerv6Class", "Set-DhcpServerv6DnsSetting", "Set-DhcpServerv6OptionDefinition", "Set-DhcpServerv6OptionValue", "Set-DhcpServerv6Reservation", "Set-DhcpServerv6Scope", "Set-DhcpServerv6StatelessStore", "Add-DhcpServerSecurityGroup", "Repair-DhcpServerv4IPRecord" , "Rename-DhcpServerv4Superscope" , "Get-DhcpServerv4ScopeStatistics" , "Add-DhcpServerv4MulticastScope" , "Set-DhcpServerv4MulticastScope" , "Get-DhcpServerv4MulticastScope" , "Remove-DhcpServerv4MulticastScope" , "Get-DhcpServerv4MulticastScopeStatistics" , "Add-DhcpServerv4MulticastExclusionRange" , "Get-DhcpServerv4MulticastExclusionRange" , "Remove-DhcpServerv4MulticastExclusionRange" , "Get-DhcpServerv4MulticastLease" , "Remove-DhcpServerv4MulticastLease" , "Get-DhcpServerDnsCredential" , "Set-DhcpServerDnsCredential" , "Remove-DhcpServerDnsCredential" )

    }

注意:

FormatsToProcess = 'DhcpServerPsProvider.Format.ps1xml'

这会链接输出模板。

答案 3 :(得分:1)

尝试使用此方法而不是使用格式表。您将一个成员集添加到您的对象。

https://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/