forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Send-SeqEvent.ps1
78 lines (68 loc) · 2.46 KB
/
Send-SeqEvent.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
<#
.SYNOPSIS
Send an event to a Seq server.
.INPUTS
System.Collections.Hashtable
or System.Collections.Specialized.OrderedDictionary
or System.Data.DataRow
or an object
.FUNCTIONALITY
Seq
.LINK
Invoke-RestMethod
.LINK
https://getseq.net/
.EXAMPLE
Send-SeqEvent.ps1 'Hello from PowerShell' -Server http://my-seq -LiteralMessage
.EXAMPLE
Send-SeqEvent.ps1 'Event: {User} on {Machine}' @{ User = $env:UserName; Machine = $env:ComputerName } -Server http://my-seq
.EXAMPLE
Send-SeqEvent.ps1 -Properties @{ Message = $Error[0].Exception.Message } -Level Error -Server http://my-seq
#>
#requires -Version 4
[CmdletBinding()][OutputType([void])] Param(
<#
The text to use as the log message, a Seq template unless -LiteralMessage is present.
By default, the value of the Message property will be used.
#>
[Parameter(Position=0)][Alias('Text')][string] $Message = '{Message}',
<#
Logging properties to record in Seq, as an OrderedDictionary, Hashtable, DataRow,
or any object with properties to use.
#>
[Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)][Alias('Parameters')] $Properties,
<#
The type of event to record.
Information by default.
#>
[ValidateSet('Verbose','Debug','Information','Warning','Error','Fatal')][string] $Level = 'Information',
# The URL of the Seq server.
[uri] $Server,
# The Seq API key to use.
[string] $ApiKey,
# When present, indicates the Message parameter is to be used verbatim, not as a Seq template.
[switch] $LiteralMessage
)
Process
{
if($Properties -is [hashtable]) {}
elseif($Properties -is [Collections.Specialized.OrderedDictionary]) {}
elseif($Properties -is [Data.DataRow]) {$Properties = ConvertFrom-DataRow.ps1 $Properties -AsHashtable}
else {$Properties = ConvertTo-OrderedDictionary.ps1 $Properties}
if($LiteralMessage) { $Properties += @{Message=$Message}; $Message = "{Message}" }
@{
Uri = New-Object uri ([uri]$Server,"/api/events/raw?apiKey=$ApiKey")
Method = 'POST'
ContentType = 'application/json'
Body = @{
Events = @(
@{
Timestamp = Get-Date -Format o
Level = $Level
MessageTemplate = $Message
Properties = $Properties
}
)
} |ConvertTo-Json -Depth 5 -Compress
} |ForEach-Object {Invoke-RestMethod @_ |Write-Verbose}
}