Documentation
DocumentationDiscussions
Documentation
These docs are for v2021.2. Click to read the latest docs for v2024.3.

PowerShell

It can be useful to write events from Windows PowerShell in some scenarios. For this purpose a sample PowerShell module Seq is provided inline below.

Using the Seq module

With the Seq.psm1 module in the same folder as the script, the example below shows how events are written:

Import-Module .\Seq
  
# Specify the Seq server URL; an -apiKey can also be provided.
# Any properties set here will be attached to all events.
  
$seq = Open-Seq "http://my-seq" -properties @{ Machine = $env:ComputerName }
  
# Simple information method
  
Send-SeqEvent $seq "Hello from PowerShell"
  
# Specify additional properties and a level value (Verbose, Debug, Warning, Error and Fatal are allowed)
  
Send-SeqEvent $seq "Something is broken!" -level Error -properties @{ User = $env:Username }
  
# Use the -template switch if the message is a Serilog-style template
  
Send-SeqEvent $seq "Leonard is {Age}" -properties @{ Age = 42 } -template
If the module is in the module path replace the first line with Import-Module Seq.
The Seq module

The Seq.psm1 module is provided below, and is licensed under the Apache 2.0 License. Patches/enhancements are gratefully accepted via the Seq GitHub tracker.

# Seq.psm1

function Open-Seq ([string] $url, [string] $apiKey, $properties = @{})
{
  return @{ Url = $url; ApiKey = $apiKey; Properties = $properties.Clone() }
}
  
function Send-SeqEvent (
    $seq,
    [string] $text,
    [string] $level,
    $properties = @{},
    [switch] $template,
    [System.Exception] $exception = $null)
{
  if (-not $level) {
    $level = 'Information'
  }
   
  if (@('Verbose', 'Debug', 'Information', 'Warning', 'Error', 'Fatal') -notcontains $level) {
    $level = 'Information'
  }
  
  $allProperties = $seq["Properties"].Clone()
  $allProperties += $properties
  
  $messageTemplate = "{Text}"
  
  if ($template) {
    $messageTemplate = $text;
  } else {
    $allProperties += @{ Text = $text; }
  }

  $ex = "null";
  if ($exception) {
    $ex = ($exception.ToString() | ConvertTo-Json)
  }
  
  $body = "{""Events"": [ {
    ""Timestamp"": ""$([System.DateTimeOffset]::Now.ToString('o'))"",
    ""Level"": ""$level"",
    ""Exception"": $ex,
    ""MessageTemplate"": $($messageTemplate | ConvertTo-Json),
    ""Properties"": $($allProperties | ConvertTo-Json) }]}"
  
  $target = "$($seq["Url"])/api/events/raw?apiKey=$($seq["ApiKey"])"
  
  Invoke-RestMethod -Uri $target -Body $body -ContentType "application/json" -Method POST
}

Alternative scripts

An alternative set of scripts, Use-SeqServer, Send-SeqEvent, Send-SeqScriptEvent, is maintained by Brian Lalonde here on GitHub.