Powershell - 格式脚本输出与控制台相同

时间:2018-04-11 10:47:11

标签: powershell formatting

我在控制台和脚本中运行PS cmdlet,但我看到的输出格式不同。

代码:

function Print-UnhealthySubServiceEntities($unhealthyServices)
{
    if($unhealthyServices -eq $null)
    {
        return
    }

    write-Output "- Unhealthy Services:"
    $unhealthyServices | Get-ServiceFabricServiceHealth

    $unhealthyPartitions = $unhealthyServices | Get-ServiceFabricPartition | where { $_.HealthState -ne "Ok" }
    write-Output "- Unhealthy Partitions:"
    $unhealthyPartitions | Get-ServiceFabricPartitionHealth

    $unhealthyReplicas = $unhealthyPartitions | Get-ServiceFabricReplica | where { $_.HealthState -ne "Ok" }
    write-Output "- Unhealthy Replicas:"
    $unhealthyReplicas | Get-ServiceFabricReplicaHealth
}

function Print-UnhealthyEntities
{
    # non-system apps
    write-output "$(Get-TimeAndLineNum) Printing details of unhealthy entities for Non-System Applications:"

    $unhealthyApps = Get-ServiceFabricApplication | where { $_.HealthState -ne "Ok" }

    write-Output "- Unhealthy Applications:"
    $unhealthyApps | Get-ServiceFabricApplicationHealth

    $unhealthyServices = $unhealthyApps | Get-ServiceFabricService | where { $_.HealthState -ne "Ok" }
    Print-UnhealthySubServiceEntities $unhealthyServices

    # system app (there is only one)
    write-output "$(Get-TimeAndLineNum) Printing details of unhealthy entities for System Applications:"

    write-Output "- Unhealthy Application:"
    Get-ServiceFabricApplicationHealth fabric:/System | where { $_.AggregatedHealthState -ne "Ok" }

    $unhealthySysServices = Get-ServiceFabricService -ApplicationName "fabric:/System" | where { $_.HealthState -ne "Ok" }
    Print-UnhealthySubServiceEntities $unhealthySysServices

    # nodes
    write-output "$(Get-TimeAndLineNum) Printing details of unhealthy nodes:"

    Get-ServiceFabricNode  | where { $_.HealthState -ne "Ok" } | Get-ServiceFabricNodeHealth

}

来自控制台的输出:

PS> $unhealthySysServices | Get-ServiceFabricPartition | 
      Get-ServiceFabricReplica | Get-ServiceFabricReplicaHealth


PartitionId           : a181d7d3-9828-4287-a183-de7727bdc147
InstanceId            : 131678681384668237
AggregatedHealthState : Warning
UnhealthyEvaluations  :
                        Unhealthy event: SourceId='System.FabricDnsService', Property='Environment', HealthState='Warning', ConsiderWarningAsError=false.

HealthEvents          :
                        SourceId              : System.RA
                        Property              : State
                        HealthState           : Ok
                        SequenceNumber        : 131678681385448280
                        SentAt                : 4/10/2018 9:08:58 PM
                        ReceivedAt            : 4/10/2018 9:09:03 PM
                        TTL                   : Infinite
                        Description           : Replica has been created on_Node_0.
                        For more information see: http://aka.ms/sfhealth
                        RemoveWhenExpired     : False
                        IsExpired             : False
                        Transitions           : Warning->Ok = 4/10/2018 9:09:03 PM, LastError = 1/1/0001 12:00:00 AM

                        SourceId              : System.FabricDnsService
                        Property              : Socket
                        HealthState           : Ok
                        SequenceNumber        : 131678681487009623
                        SentAt                : 4/10/2018 9:09:08 PM
                        ReceivedAt            : 4/10/2018 9:09:08 PM
                        TTL                   : Infinite
                        Description           : DnsService is listening on port 53.
                        RemoveWhenExpired     : False
                        IsExpired             : False
                        Transitions           : Warning->Ok = 4/10/2018 9:09:08 PM, LastError = 1/1/0001 12:00:00 AM

                        SourceId              : System.FabricDnsService
                        Property              : Environment
                        HealthState           : Warning
                        SequenceNumber        : 131679155816154400
                        SentAt                : 4/11/2018 10:19:41 AM
                        ReceivedAt            : 4/11/2018 10:19:41 AM
                        TTL                   : Infinite
                        Description           : FabricDnsService is not preferred DNS server on the node.
                        RemoveWhenExpired     : False
                        IsExpired             : False
                        Transitions           : Ok->Warning = 4/11/2018 10:19:41 AM, LastError = 1/1/0001 12:00:00 AM

请注意,HealthEvents格式正确。

现在,从脚本输出:

HealthEvents          : {SourceId: 'System.RA', Property: 'State', Ok, "Replica has been created on_Node_0.
                        For more information see: http://aka.ms/sfhealth", TimeToLive 10675199.02:48:05.4775807, RemoveWhenExpired False, SequenceNumber 131678681385448280: IsExpired
                        False, SourceId: 'System.FabricDnsService', Property: 'Socket', Ok, "DnsService is listening on port 53.", TimeToLive 10675199.02:48:05.4775807, RemoveWhenExpired
                        False, SequenceNumber 131678681487009623: IsExpired False, SourceId: 'System.FabricDnsService', Property: 'Environment', Warning, "FabricDnsService is not preferred
                        DNS server on the node.", TimeToLive 10675199.02:48:05.4775807, RemoveWhenExpired False, SequenceNumber 131679155816154400: IsExpired False}
UnhealthyEvaluations  : {HealthState: Warning: Unhealthy event: SourceId='System.FabricDnsService', Property='Environment', HealthState='Warning', ConsiderWarningAsError=false.}
InstanceId            : 131678681384668237
Kind                  : Stateless
PartitionId           : a181d7d3-9828-4287-a183-de7727bdc147
Id                    : 131678681384668237
AggregatedHealthState : Warning

这是从同一控制台作为PS脚本运行时。请注意,阅读HealthEvents更加困难。

如何将脚本输出格式化为与控制台类似的可读性?

0 个答案:

没有答案