Restart Application Pool in Powershell and write it to EventLog

Sometimes on Continuous Integration or other automated scenarios you want to restart Application pool in order to recycle everything clean. This can be done by calling Powershell command Restart-WebAppPool like this: “Restart-WebAppPool AppPoolNameHere”. But you might also want to write log entry to EventLogs so you know what is going on.

In order to write to the event log, one can use the Write-EventLog cmdlet but there are mandatory parameters needed to write the event log information. These are:

  • LogName
  • Source
  • EventID
  • Message

If you want to create an entry log message “Format was called” into the Application Log and store this into your custom source “MyScript” with a custom event ID like “3001”, you will need to type it as:

Write-EventLog -LogName Application -Source MyScript -EventId 3001 -Message "Format was called"

If you have not registered your custom event source, you will encounter an error. In order to register a custom event source, you will need to call the New-EventLog cmdlet like the example below:

New-EventLog -LogName Application -Source MyScript

After this you can call Write-eventLog as following:

Write-EventLog -LogName Application -Source MyScript -EventId 3001 -Message “Format was called”

Advertisements