Log-Event.ps1
19 Sep 2025Description
Purpose
A function to log events.
Detailed Description
This function writes an event to the event log.
Usage
Example 1
Log-Event -message "This is a test event."
Notes
No additional notes.
Script
Function Log-Event {
<#
.SYNOPSIS
A function to log events.
.DESCRIPTION
This function writes an event to the event log.
.PARAMETER logName
The name of the log where the event will be written. Default is "NinjaOneDeployments".
.PARAMETER source
The source of the event. Default is "NinjaOneScripts".
.PARAMETER entryType
The type of the event. Must be one of "Error", "Warning", "Information", "SuccessAudit", "FailureAudit". Default is "Information".
.PARAMETER eventId
The ID of the event. Default is 1847.
.PARAMETER message
The message of the event. This parameter is mandatory.
.EXAMPLE
Log-Event -message "This is a test event."
#>
param (
[Parameter(Mandatory = $false)]
[string]$logName = "NinjaOneDeployments",
[Parameter(Mandatory = $false)]
[string]$source = "NinjaOneScripts",
[Parameter(Mandatory = $false)]
[ValidateSet("Error", "Warning", "Information", "SuccessAudit", "FailureAudit")]
[string]$entryType = "Information",
[Parameter(Mandatory = $false)]
[int]$eventId = 1847,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$message
)
try {
Write-EventLog -LogName $logName -Source $source -EntryType $entryType -EventId $eventId -Message $message
}
catch {
Write-Error "Failed to write to event log: $_"
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.