functions/message/Get-PSFMessage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function Get-PSFMessage
{
    <#
        .SYNOPSIS
            Returns log entries for the PSFramework
         
        .DESCRIPTION
            Returns log entries for the PSFramework. Handy when debugging or developing a script using it.
         
        .PARAMETER FunctionName
            Default: "*"
            Only messages written by similar functions will be returned.
         
        .PARAMETER ModuleName
            Default: "*"
            Only messages written by commands from similar modules will be returned.
         
        .PARAMETER Target
            Only messags handling the specified target will be returned.
         
        .PARAMETER Tag
            Only messages containing one of these tags will be returned.
         
        .PARAMETER Last
            Only messages written by the last X executions will be returned.
            Uses Get-History to determine execution. Ignores Get-PSFmessage commands.
            By default, this will also include messages from other runspaces. If your command executes in parallel, that's useful.
            If it doesn't and you were offloading executions to other runspaces, consider also filtering by runspace using '-Runspace'
         
        .PARAMETER Skip
            How many executions to skip when specifying '-Last'.
            Has no effect without the '-Last' parameter.
         
        .PARAMETER Runspace
            The guid of the runspace to return messages from.
            By default, messages from all runspaces are returned.
            Run the following line to see the list of guids:
     
            Get-Runspace | ft Id, Name, InstanceId -Autosize
     
        .PARAMETER Level
            Limit the message selection by level.
            Message levels have a numeric value, making it easier to select a range:
             
              -Level (1..6)
     
            Will select the first 6 levels (Critical - SomewhatVerbose).
         
        .PARAMETER Errors
            Instead of log entries, the error entries will be retrieved
         
        .EXAMPLE
            Get-PSFMessage
             
            Returns all log entries currently in memory.
     
        .EXAMPLE
            Get-PSFMessage -Target "a" -Last 1 -Skip 1
     
            Returns all log entries that targeted the object "a" in the second last execution sent.
     
        .EXAMPLE
            Get-PSFMessage -Tag "fail" -Last 5
     
            Returns all log entries within the last 5 executions that contained the tag "fail"
    #>

    [CmdletBinding()]
    param (
        [string]
        $FunctionName = "*",
        
        [string]
        $ModuleName = "*",
        
        [AllowNull()]
        $Target,
        
        [string[]]
        $Tag,
        
        [int]
        $Last,
        
        [int]
        $Skip = 0,
        
        [guid]
        $Runspace,
        
        [PSFramework.Message.MessageLevel[]]
        $Level,
        
        [switch]
        $Errors
    )
    
    begin
    {
        
    }
    
    process
    {
        if ($Errors) { $messages = [PSFramework.Message.LogHost]::GetErrors() | Where-Object { ($_.FunctionName -like $FunctionName) -and ($_.ModuleName -like $ModuleName) } }
        else { $messages = [PSFramework.Message.LogHost]::GetLog() | Where-Object { ($_.FunctionName -like $FunctionName) -and ($_.ModuleName -like $ModuleName) } }
        
        if (Test-PSFParameterBinding -ParameterName Target)
        {
            $messages = $messages | Where-Object TargetObject -EQ $Target
        }
        
        if (Test-PSFParameterBinding -ParameterName Tag)
        {
            $messages = $messages | Where-Object { $_.Tags | Where-Object { $_ -in $Tag } }
        }
        
        if (Test-PSFParameterBinding -ParameterName Runspace)
        {
            $messages = $messages | Where-Object Runspace -EQ $Runspace
        }
        
        if (Test-PSFParameterBinding -ParameterName Last)
        {
            $history = Get-History | Where-Object CommandLine -NotLike "Get-PSFMessage*" | Select-Object -Last $Last -Skip $Skip
            $start = $history[0].StartExecutionTime
            $end = $history[-1].EndExecutionTime
            
            $messages = $messages | Where-Object { ($_.Timestamp -gt $start) -and ($_.Timestamp -lt $end) -and ($_.Runspace -eq ([System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.InstanceId))}
        }
        
        if (Test-PSFParameterBinding -ParameterName Level)
        {
            $messages = $messages | Where-Object Level -In $Level
        }
        
        return $messages
    }
    
    end
    {
        
    }
}