New PowerShell Event Creator for Operations Manager 2007

New PowerShell Event Creator for Operations Manager 2007

  • Comments 2
  • Likes

 

Did you remember the Event Creator for MOM 2005 which was available in the Resource Kit? You could use this tool to create events for testing Management Packs. Event Creator enables you to choose event sources for the existing event logs that are currently registered. You can probably use this tool also for testing in your OpsMgr environment but why not use PowerShell?

You have at least installed PowerShell on you Management Server(s) so why not use PowerShell to create events for testing?

With some help from the PowerShell Guy I created a PowerShell script to create events in the eventlog.

# Description:     Create Events in Application log
# Date:                03-05-2007
# Author:            Stefan Stranger
# Explanation:    If you only wish to write to the event log you must do two things. The first is to create or specify a Source. 
#                         The second is to call the WriteEntry method. The source would be your application name, by default,
 
#                         if you create a source that is new then your log entry will be written to the Application Log.
 
#                         To WriteEntry method does the actual writing to the Event Log.

#                         ev.WriteEntry(My event text, System.Diagnostics.EventLogEntryType.Information, myeventid)

#Check if user is admin
function get-Admin {
 
$ident = [Security.Principal.WindowsIdentity]::GetCurrent()
 
 
foreach ( $groupIdent in $ident.Groups ) {
 
if ( $groupIdent.IsValidTargetType([Security.Principal.SecurityIdentifier]) ) {
  
$groupSid = $groupIdent.Translate([Security.Principal.SecurityIdentifier])
  
if ( $groupSid.IsWellKnown("AccountAdministratorSid") -or $groupSid.IsWellKnown("BuiltinAdministratorsSid")){
   
return $TRUE
  
}
 
}
 
}
 
return $FALSE
}

$Result = get-Admin

if ($Result -eq $FALSE) 
 
{

   
write-host "Better be an admin for this script."
   
#exit

 
}

function Write-EventLog { 
   
param ([string]$msg = $(read-host "Please enter a Event Description"), [string]$source = $(read-host "Please enter Event Source"), [string]$type = $(read-host "Please enter Event Type [Information, Warning, Error]"), [int]$eventid = $(read-host "Please enter EventID"))

   
# Create the source, if it does not already exist.
       
if(![System.Diagnostics.EventLog]::SourceExists($source))
       
{
           
[System.Diagnostics.EventLog]::CreateEventSource($source,'Application')
       
}
       
else 
       
{

           
write-host "Source exists"
       
}

        
   
# Check if Event Type is correct
   
switch ($type) 
   
{
 
        "Information"
{}
 
        "Warning"
{}
 
        "Error"
{}
 
       
default {"Event type is invalid";exit}

   
}

      
   
$log = New-Object System.Diagnostics.EventLog 
   
$log.set_log("Application")
 
   
$log.set_source($source)

   
$log.WriteEntry($msg,$type,$eventid)
}

Write-Eventlog
Write-Host "Event created"

Or you can download the PowerShell script from the PowerShell Code Repository.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Some time ago I showed you can use PowerShell to create Events for OpsMgr 2007. And according to the

  • Hi

    Nice one, already using this. But, how to do it the other way around? I have a costumer asking me how to make an event activate a Powershell script? Example:

    You get event 1000 in the application log, which makes a Powershell script run.

    How to do that?

    Thanks!

    Regards

    Michael