create event in powershell

Try
{
#Define the event log and your custom event source
$evtlog = “Application”
$source = “MyEventSource”
$evtID = 911
$myMessage = “Test hari powershell”

#Load the event source to the log if not already loaded. This will fail if the event source is already assigned to a different log.
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false)
{
[System.Diagnostics.EventLog]::CreateEventSource($source, $evtlog)
}

$evtID = New-Object System.Diagnostics.EventInstance($evtID,1);
$evtObject = New-Object System.Diagnostics.EventLog;
$evtObject.Log = $evtlog;
$evtObject.Source = $source;
$evtObject.WriteEvent($evtID, $myMessage)
}

Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host $ErrorMessage
#Send-MailMessage -From hari@Mygeek.Com -To geekhari@MyCompany.Com -Subject “Geek Hari is caught in exception!” -SmtpServer EXCH01.AD.here.Com -Body “We failed to read file $FailedItem. The error message was $ErrorMessage”
#Break
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s