-
Notifications
You must be signed in to change notification settings - Fork 29
/
Send-SeqScriptEvent.ps1
71 lines (65 loc) · 2.33 KB
/
Send-SeqScriptEvent.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
<#
.SYNOPSIS
Sends an event (often an error) from a script to a Seq server, including script info.
.FUNCTIONALITY
Seq
.LINK
Send-SeqEvent.ps1
.LINK
https://getseq.net/
.EXAMPLE
try { Connect-Thing } catch { Send-SeqScriptEvent.ps1 'Trying to connect' $_ -Level Error -Server http://my-seq }
#>
#Requires -Version 3
[CmdletBinding()][OutputType([void])] Param(
# A description of what was being attempted.
[Parameter(Position=0,Mandatory=$true)][string]$Action,
<#
An optional PowerShell ErrorRecord object to record.
Will try to automatically find $_ in a calling "catch{}"" block.
#>
[Parameter(Position=1)][Management.Automation.ErrorRecord]$ErrorRecord =
((Get-Variable _ -Scope 1 -ValueOnly -EA SilentlyContinue) -as [Management.Automation.ErrorRecord]),
<#
The type of event to record.
Defaults to Error if an ErrorRecord is found, Information otherwise.
#>
[Parameter(Position=2)][ValidateSet('Verbose','Debug','Information','Warning','Error','Fatal')][string] $Level = 'Error',
<#
The scope of the script InvocationInfo to use.
Defaults to 1 (the script calling Send-SeqScriptEvent.ps1).
Sending a 2 will try to use the script calling the script calling this one.
#>
[Alias('Scope')][string] $InvocationScope = '1',
# The URL of the Seq server.
[uri] $Server,
# The Seq API key to use.
[string] $ApiKey
)
if(!($ErrorRecord -or $PSBoundParameters.ContainsKey('Level'))) { $Level = 'Information' }
$caller = try{Get-Variable MyInvocation -Scope $InvocationScope -ValueOnly -EA Stop}catch{$MyInvocation}
$SeqEvent = @{ Level = $Level }
if($Server){[void]$SeqEvent.Add('Server',$Server)}
if($ApiKey){[void]$SeqEvent.Add('ApiKey',$ApiKey)}
$Properties = @{
Script = if($caller.ScriptName){Split-Path $caller.ScriptName -Leaf}else{$caller.MyCommand.Name}
CommandName = $caller.MyCommand.Name
Invocation = $caller
Action = $Action
CommandLine = [Environment]::GetCommandLineArgs()
ComputerName = $env:COMPUTERNAME
}
if($ErrorRecord)
{
$Properties += @{
Message = $ErrorRecord.Exception.Message
Error = $ErrorRecord
}
[void]$SeqEvent.Add('Message','{Script}: {Action}: {Message}')
}
else
{
[void]$SeqEvent.Add('Message','{Script}: {Action}')
}
[void]$SeqEvent.Add('Properties',$Properties)
Send-SeqEvent.ps1 @SeqEvent