Initialize-EventLogging.ps1


Description

Purpose

Initializes event logging by creating a new event log source if it does not exist.

Detailed Description

No detailed description provided.

Back to Top

Usage

No usage examples provided.

Back to Top

Notes

Author: Unknown Last Edit: Unknown

Back to Top


Script

function Initialize-EventLogging {
    <#
    .SYNOPSIS
        Initializes event logging by creating a new event log source if it does not exist.
    
    .PARAMETER logName
        Specifies the name of the event log. The default value is "NinjaOneDeployments".
        Valid values are "Application", "System", "NinjaOneDeployments", and "AutomatedDeployment".
    
    .PARAMETER source
        Specifies the name of the event log source. The default value is "NinjaOneScripts".
        Valid values are "NinjaOneScripts" and "AutomatedDeployment".
    
    .NOTES
        Author: Unknown
        Last Edit: Unknown
    #>
    param (
        [Parameter(Mandatory = $false)]
        [ValidateSet("Application", "System", "NinjaOneDeployments", "AutomatedDeployment")]
        [string]$logName = "NinjaOneDeployments",

        [Parameter(Mandatory = $false)]
        [ValidateSet("NinjaOneScripts", "AutomatedDeployment")]
        [string]$source = "NinjaOneScripts"
    )

    if (![string]::IsNullOrWhiteSpace($logName) -and ![string]::IsNullOrWhiteSpace($source)) {
        try {
            # Create the source if it does not exist
            if (![System.Diagnostics.EventLog]::SourceExists($source)) {
                $Message = "Initialize-EventLogging @ " + (Get-Date) + ": Creating LogSource for EventLog..."
                Write-Verbose $message
                [System.Diagnostics.EventLog]::CreateEventSource($source, $logName)
            }
            else {
                $Message = "Initialize-EventLogging @ " + (Get-Date) + ": LogSource exists already."
                Write-Verbose $message
            }
        }
        catch {
            Write-Error "An error occurred while initializing logging: $_"
        }
    }
    else {
        Write-Error "Invalid parameters. LogName and Source cannot be empty."
    }
}

Back to Top

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.

Issue


Back to Top