Connect PowerShell to Bugsink
The PowerShell SDK reports caught errors with their PowerShell source stack and the context needed to identify the failed operation.
This guide adds Bugsink to an existing inventory batch. One order contains an unknown product; the script reports its real error record, then continues with the rest of the batch. To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.
Install the SDK
Install the open-source Sentry module from PowerShell Gallery in the scope
where your script runs. Bugsink accepts the Sentry event protocol, so the
module can send events directly to your Bugsink project:
Install-Module Sentry -RequiredVersion 0.5.0 -Scope CurrentUserInstall-Module Sentry -RequiredVersion 0.5.0 -Scope CurrentUser
Initialize Bugsink
Import the module and call Start-Sentry before processing work that can fail.
Reading the DSN from the environment keeps it out of source control. The
release identifies this version of the script in Bugsink:
Import-Module Sentry -RequiredVersion 0.5.0 Start-Sentry { $_.Dsn = $env:SENTRY_DSN $_.Release = 'inventory-report@1.0.0' }Import-Module Sentry -RequiredVersion 0.5.0 Start-Sentry { $_.Dsn = $env:SENTRY_DSN $_.Release = 'inventory-report@1.0.0' }
Set SENTRY_DSN to the DSN shown by your Bugsink project before running the
script:
$env:SENTRY_DSN = 'https://key@bugsink.example/1'$env:SENTRY_DSN = 'https://key@bugsink.example/1'
Report a caught error
Out-Sentry reports the actual ErrorRecord from the catch block, preserving
its exception and PowerShell frames. -EditScope adds a searchable component
tag and order details to this event only; the following warning and loop
behavior remain ordinary application code:
try { # Process the current $order here. } catch { $caught = $_ $orderId = [string] $order.id $sku = [string] $order.sku $caught | Out-Sentry -EditScope { $_.SetTag('component', 'inventory') $_.SetExtra('order_id', $orderId) $_.SetExtra('sku', $sku) } | Out-Null Write-Warning "Rejected order ${orderId}: $($caught.Exception.Message)" }try { # Process the current $order here. } catch { $caught = $_ $orderId = [string] $order.id $sku = [string] $order.sku $caught | Out-Sentry -EditScope { $_.SetTag('component', 'inventory') $_.SetExtra('order_id', $orderId) $_.SetExtra('sku', $sku) } | Out-Null Write-Warning "Rejected order ${orderId}: $($caught.Exception.Message)" }
Breadcrumbs record useful steps before the error without creating their own issues. This one records the batch size; the completed example also records the rejected order before reporting it:
Add-SentryBreadcrumb -Message 'Loaded the order batch' -Category 'inventory' -Data @{ orders = [string] $orders.Count }Add-SentryBreadcrumb -Message 'Loaded the order batch' -Category 'inventory' -Data @{ orders = [string] $orders.Count }
Close the SDK in finally so the short-lived process sends queued events even
when reading or processing the batch fails:
try { # Read and process the batch here. } finally { Stop-Sentry }try { # Read and process the batch here. } finally { Stop-Sentry }
Run the batch
Run the script normally after setting the DSN:
./inventory-report.ps1./inventory-report.ps1
It reserves the first order, warns about the missing product, and still reserves the final order. Reporting the caught error does not change the script’s recovery behavior.
See the error in Bugsink
Open your Bugsink project. The new System.InvalidOperationException issue
contains the PowerShell source line, both batch breadcrumbs, the component tag,
and the order-specific extra data. The breadcrumbs show both earlier batches.
inventory
Loaded the order batch
event time
inventory
Rejected an order
event time
That’s it: caught PowerShell errors can now be reported to Bugsink without
changing how the script continues. This guide proves the Sentry 0.5.0 module
on PowerShell 7 running on Linux. It does not yet prove Windows or macOS runs,
automatic capture of uncaught errors, or non-terminating error streams. The
stored event retains inventory-report.ps1 and its line number, but the current
Bugsink stack view leaves the PowerShell frame’s filename label blank.
