Monday, March 12, 2012

PowerShell and EventLog creation

Powershell provides easy cmdlets that allow you to query event logs, as well as create and delete event log sources. But I could not find a simple cmdLet that allows you to check for the existence of an event log source.

Here is what I came up with:

if ( -not [System.Diagnostics.EventLog]::SourceExists($eventSourceName, $computerName))

        Write-Host "Creating event source $eventSourceName on computer $computerName..."
        New-EventLog -ComputerName $computerName -LogName Application -Source $eventSourceName
        Write-Host "Event source $eventSourceName successfully created on computer $computerName"
}

No comments: