QND.DiscoveryEvent.WAT (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeMicrosoft.Windows.SerializedObjectData

Member Modules:

ID Module Type TypeId RunAs 
WA WriteAction Microsoft.Windows.PowerShellWriteAction Default

Source Code:

<WriteActionModuleType ID="QND.DiscoveryEvent.WAT" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="LogName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SourceName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="EventId" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Message" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Key" type="xsd:string"/>
</Configuration>
<ModuleImplementation>
<Composite>
<MemberModules>
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>New-DiscoveryEvent.ps1</ScriptName>
<ScriptBody><Script>
#TO SHOW VERBOSE MESSAGES SET $VerbosePreference="continue"
#SET ErrorLevel to 5 so show discovery info
#*************************************************************************
# Script Name -
# Author - Daniele Grandini - QND
# Version - 1.0 30-04-2016
# Purpose -
#
# Assumptions -
#
#
# Parameters - TraceLevel
# - ComputerName
# - SourceId
# - ManagedEntityId
# Command Line - .\test.ps1 4 "serverName" '{1860E0EB-8C21-41DA-9F35-2FE9343CCF36}' '{1860E0EB-8C21-41DA-9F35-2FE9343CCF36}'
# If discovery must be added the followinf parameters
# SourceId ($ MPElement $ )
# ManagedEntityId ($ Target/Id $)
#
# Output properties
#
# Status
#
# Version History
# 1.0 06.08.2010 DG First Release
# 1.5 15.02.2014 DG minor cosmetics
#
# (c) Copyright 2010, Progel srl, All Rights Reserved
# Proprietary and confidential to Progel srl
#
#*************************************************************************


# Get the named parameters
param([int]$traceLevel=2,
[Parameter (Mandatory=$true)] [int]$EventID,
[Parameter (Mandatory=$false)] [string]$EventSource='QND Script',
[Parameter (Mandatory=$false)][string]$EventLog='Operations Manager',
[Parameter (Mandatory=$false)][string]$Message='',
[Parameter (Mandatory=$false)][string]$Key=''
)

[Threading.Thread]::CurrentThread.CurrentCulture = "en-US"
[Threading.Thread]::CurrentThread.CurrentUICulture = "en-US"

#region Constants
#Constants used for event logging
$SCRIPT_NAME = "New-DiscoveryEvent"
$SCRIPT_VERSION = "1.0"

#Trace Level Costants
$TRACE_NONE = 0
$TRACE_ERROR = 1
$TRACE_WARNING = 2
$TRACE_INFO = 3
$TRACE_VERBOSE = 4
$TRACE_DEBUG = 5

#Event Type Constants
$EVENT_TYPE_SUCCESS = 0
$EVENT_TYPE_ERROR = 1
$EVENT_TYPE_WARNING = 2
$EVENT_TYPE_INFORMATION = 4
$EVENT_TYPE_AUDITSUCCESS = 8
$EVENT_TYPE_AUDITFAILURE = 16

#Standard Event IDs
$FAILURE_EVENT_ID = 4000 #errore generico nello script
$SUCCESS_EVENT_ID = 1101
$START_EVENT_ID = 1102
$STOP_EVENT_ID = 1103

#endregion

#region Logging

if ([System.Diagnostics.EventLog]::SourceExists($EventSource) -eq $false) {
[System.Diagnostics.EventLog]::CreateEventSource($EventSource, $eventlog)
}

function Log-Params
{
param($Invocation)
$line=''
foreach($key in $Invocation.BoundParameters.Keys) {$line += "$key=$($Invocation.BoundParameters[$key]) "}
Log-Event $START_EVENT_ID $EVENT_TYPE_INFORMATION ("Starting script. Invocation Name:$($Invocation.InvocationName)`n Parameters`n $line") $TRACE_INFO
}

function Create-Event
{
param(
[int] $eventID,
[int] $eventType,
[string] $msg,
[string[]] $parameters)

switch ($eventType) {
$EVENT_TYPE_SUCCESS {$nativeType=[System.Diagnostics.EventLogEntryType]::Information}
$EVENT_TYPE_ERROR {$nativeType=[System.Diagnostics.EventLogEntryType]::Error}
$EVENT_TYPE_WARNING {$nativeType=[System.Diagnostics.EventLogEntryType]::Warning}
$EVENT_TYPE_INFORMATION {$nativeType=[System.Diagnostics.EventLogEntryType]::Information}
$EVENT_TYPE_AUDITSUCCESS {$nativeType=[System.Diagnostics.EventLogEntryType]::AuditSuccess}
$EVENT_TYPE_AUDITFAILURE {$nativeType=[System.Diagnostics.EventLogEntryType]::AuditFailure}
default {
Write-Verbose 'match?'
$nativeType=[System.Diagnostics.EventLogEntryType]::Information
}
}
$event = New-Object System.Diagnostics.EventInstance($eventID,1,$nativeType)

$evtObject = New-Object System.Diagnostics.EventLog;
$evtObject.Log = $EventLog;
$evtObject.Source = $EventSource;
$parameters = @($msg) + $parameters
$evtObject.WriteEvent($event, $parameters)
}


function Log-Event
{
param($eventID, $eventType, $msg, $level)

Write-Verbose ("Logging event. " + $SCRIPT_NAME + " EventID: " + $eventID + " eventType: " + $eventType + " Version:" + $SCRIPT_VERSION + " --&gt; " + $msg)
if($level -le $P_TraceLevel)
{
Write-Host ("Logging event. " + $SCRIPT_NAME + " EventID: " + $eventID + " eventType: " + $eventType + " Version:" + $SCRIPT_VERSION + " --&gt; " + $msg)
Create-Event -eventID $eventID -eventType $eventType -msg ($msg + "`n" + "Version :" + $SCRIPT_VERSION) -parameters @($SCRIPT_NAME,$SCRIPT_VERSION)
#$g_API.LogScriptEvent($SCRIPT_NAME,$eventID,$eventType, ($msg + "`n" + "Version :" + $SCRIPT_VERSION))
}
}
#endregion


#Start by setting up API object.
$P_TraceLevel = $TRACE_VERBOSE
$g_Api = New-Object -comObject 'MOM.ScriptAPI'
#$g_RegistryStatePath = "HKLM\" + $g_API.GetScriptStateKeyPath($SCRIPT_NAME)

$dtStart = Get-Date
$P_TraceLevel = $traceLevel
Log-Params $MyInvocation

Create-Event -eventID $eventID -eventType $EVENT_TYPE_INFORMATION -msg ('{0} - {1}' -f $message, $key) -parameters @($Key)

Log-Event $STOP_EVENT_ID $EVENT_TYPE_INFORMATION ("has completed successfully in " + ((Get-Date)- ($dtstart)).TotalSeconds + " seconds.") $TRACE_INFO




</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>EventId</Name>
<Value>$Config/EventId$</Value>
</Parameter>
<Parameter>
<Name>EventSource</Name>
<Value>$Config/SourceName$</Value>
</Parameter>
<Parameter>
<Name>EventLog</Name>
<Value>$Config/LogName$</Value>
</Parameter>
<Parameter>
<Name>Message</Name>
<Value>$Config/Message$</Value>
</Parameter>
<Parameter>
<Name>Key</Name>
<Value>$Config/Key$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>60</TimeoutSeconds>
<StrictErrorHandling>false</StrictErrorHandling>
<!-- true only for Tasks -->
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>Windows!Microsoft.Windows.SerializedObjectData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>