Welcome to Spipp.net server Sign in | Join | Help

Kaisa's blog

On Reporting Services, Sharepoint, dotText and Larp

Writing to EventLog from PowerShell (and some language power)

I've played around with Vidas Matelis' PowerShell scripts for processing SSAS databases the last couple of months. The scripts have all sorts of useful output, but I've been looking for a way to write the error messages to the Event viewer instead of just the command prompt. (Mostly because if you start the script from a short cut, it will just fly through and be gone from the screen when it's finished, and you have no way of knowing what on Earth the output was.)

So, today I've learned 3 new things about PowerShell
1) You can add the parameter -noexit to the command prompt string to make sure the script prompt window stays open after the script has finished running. (You can't rerun the script, though.)
Got the tip from Hey, Scripting Guy!


2) There are two ways to write to the Event Log
Number 1:
Use a Cmdlet to to create an instance of the .NET Framework class System.Diagnostics.Eventlog and then write the entry.
$a = New-Object -type System.Diagnostics.Eventlog -argumentlist System
$a.Source = "Windows PowerShell"
$a.WriteEntry("This is a test.","Information")
More info


Number 2:
Create a new Soure type and then write to the EventLog.
new-eventlog -LogName Application -Source MySource
write-eventLog -LogName Application -Message "Hello Eventing World" -Source MySource -id 1234

More info


I've used the first version in the original ProcessSSASDB.ps1 script, but I'm inspired by the second one to make more changes.

3) I still remember enough of my French lessons 15 years ago to be able to figure out why my code failed from reading a French dev forum on the same thing. :)
http://powershell-scripting.com/index.php?option=com_joomlaboard&Itemid=76&func=view&id=1053&catid=5
J'ai trouvé il y a un espace de trop sur la ligne
$applog.WriteEntry ("Ceci est un message d'erreur.", "error", "1234")
entre WriteEntry et ("Ceci


I had a space too many between WriteEntry and (" as well.
Published 06-01-2010 04:39 by kaisa
Filed Under:

Comments

No Comments
New Comments to this post are disabled

News

Random blogging about my daily work and life in general. Locations of visitors to this page

Post Calendar

<januar 2010>
mtotfls
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Search

Go

Syndication

SkinName:iroha_Blog2
Powered by Community Server, by Telligent Systems