Installation

PowerShell is supported using the Raygun4Net provider. Most features and options documented under Raygun4Net can also be used in PowerShell.

Installation

1. Install the NuGet package

The best way to install Raygun4Net is to use the NuGet package manager.

Visit https://nuget.org/packages/Mindscape.Raygun4Net/ for instructions on installation using the package manager console.

2. Add Raygun4Net and create a RaygunClient

To report exceptions in a PowerShell script you will need to add the Mindscape.Raygun4Net4.dll using the Add-Type cmdlet. You then use the New-Object cmdlet to create a RaygunClient with your API key.

Add-Type -Path '[PATH-TO-DLL]\Mindscape.Raygun4Net4.dll'
$raygun = New-Object -TypeName Mindscape.Raygun4Net.RaygunClient -arg "[YOUR-API-KEY]"

3. Set ErrorAction to stop

ErrorAction needs to be set to stop to ensure the script execution terminates immediately and is then allowed to be handled by a Try-Catch-Finally block.

$ErrorActionPreference = "Stop"

# Or for a given cmdlet
Get-Content 'c:\notfound.txt' -ErrorAction Stop

3. Catch an error and report

To catch an exception and report it to Raygun you need to use a Try-Catch-Finally block. The RaygunClient instance can then be used to send the error to Raygun.

Try
{
  Get-Content 'c:\notfound.txt'
}
Catch [Exception]
{
  $raygun.Send($_.Exception)
}


Tags

An overload of Send and SendInBackground allows you to include a list of tags with each manually sent exception:

$tags = New-Object Collections.Generic.List[String]
$tags.Add("tag1")
$raygun.Send($_.Exception, $tags)

Custom data

You can include key-value custom data using an overload of the Send or SendInBackground method. Values can be primitive types or rich object structures. All properties of objects and their children will be sent to Raygun. Cyclic object references will be detected and handled as appropriate, and any property getter that produces an exception will cause that property value to be displayed as the exception message.

$customData = @{"key1"="value1"; "key2"="value2"; };
$raygun.Send($_.Exception, $null, $customData)

The second parameter is the list of tags (mentioned above) which can be null if you don’t have tags for the current exception.


Version numbering

To set a version for reported errors you can do so by setting the ApplicationVersion property of the RaygunClient.

$raygun.ApplicationVersion = "1.3.37.0"