I've been enthusiastically educating myself on PowerShell and how it can be used to monitor SQL Server and in the process discovered Trevor Sullivan's fantastic PowerEvents modules (thanks to a colleague - yo Chris!) and examples. Specifically, Monitoring DB Mirroring and Monitor and Respond to Windows PowerEvents and Monitoring SQL Server and Using PowerEvents to Monitor Change in Database Options and DBA Solutions Using PowerShell.
These are all excelent examples and have helped tremendously to get me going in the right direction. But... (and you knew there was going to be a but)... the examples I've found have fallen short of what I'd like to be able to do... like my topic header states: <o:p></o:p>
Does PowerEvents (or, for that matter, any other PowerShell permanent eventing
subsystem) populate and pass along the automatic variables $Event, $SourceArgs,
etc so that one can consume and manipulate the triggering event's properties? <o:p></o:p>
My initial thought that this should be quite straightforward given Trevor's documentation andPowerEvents: WMI Event Consumers via using the CommandLineEventConsumer and passing event details via the special WMI %targetinstance% variable (would prefer not to have to write VBScript as that seems like a step backwards).
Problem (goal)<o:p></o:p>
Production SQL Server environments tend to encounter issues with intermittent and extended blocking which I'd like to persist to tables in an Admin database so that these can be analyzed later
at a more convenient time. The link above by Junior Laerte was excellent in that it showed how to use sp_whoisactive to do this... and I've been successful in getting that to work (with some minor changes). In that link Junior suggests (and I would prefer)
to accomplish this via more permanent eventing (ie, using the PowerEvents filter/consumer/binding model) versus the not-so-permanent method as he demonstrated here (Using
PowerShell and WMI Events Queries) and here (Using PowerShell and SQL Server Events - Blocked Process Report).
However, before attempting to do the CommandLineConsumer to fire off PowerShell and capture sp_whoisactive (see last link above), I wanted to ensure that I can get to the point where a SQL Server blocking event fired off a permanent event that at least captures the details of the event.. then, modifying it so that it captures the actual blocking information. So, this interim step - simple test to capture blocking event details - is what is described in what follows.
Here's what I've done so far and where it's failing:
- Created a simple PowerShell script to be executed by the event consumer named test.ps1 located at C:\scripts folder
"Made it to test.ps1 at $(get-date)" $args | gm
- Successfully tested this from both PowerShell and DOS command prompt. NOTE: The %CD% will be replaced by %TargetInstance% WMI token once this works.
C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "C:\Scripts\Test.ps1 `"%CD%`" >>C:\Scripts\Test.out"
Made it to test.ps1 () at 04/05/2013 15:08:20
Z:\
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB)
Contains Method bool Contains(string value)
CopyTo Method System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool EndsWith(string value, bool ignore...
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.StringComparison comparisonType)
GetEnumerator Method System.CharEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int startIndex, int count), int IndexOf(string ...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf, int startIndex, int count)
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int startIndex, int count), int Las...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int LastIndexOfAny(char[] anyOf, int startIndex, int ...
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
Remove Method string Remove(int startIndex, int count), string Remove(int startIndex)
Replace Method string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)
Split Method string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, System.StringSplit...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType), bool StartsWith(string value, bool ...
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToLower Method string ToLower(), string ToLower(System.Globalization.CultureInfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToString Method string ToString(), string ToString(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property System.Int32 Length {get;} - Created WMI event filter
ipmo powerevents $DurationMilliSecs = 20000 #Duration of blocking in milliseconds; the WMI events gets kicked off at this interval $WQLEventQuery = "select * from BLOCKED_PROCESS_REPORT where duration > $DurationMilliSecs" #Set the filter based on the blocked processes query $Filter = New-WmiEventFilter -Name "SQLSERVER: Blocked Processes Duration" -Query $WQLEventQuery
- Created WMI event consumer and bound the filter to it
$Consumer = New-WmiEventConsumer -Name 'SQLServerblocking' -ConsumerType CommandLine ` -ExecutablePath C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe `
-CommandLineTemplate " -command `"C:\Scripts\Test.ps1 %TargetInstance% >>C:\Scripts\test.out`""
#Bind filter to the consumer New-WmiFilterToConsumerBinding -Filter $Filter -Consumer $Consumer –Verbose
- Created simple blocking condition within SQL Server using the following two scripts (in separate SSMS windows of course)... (note: if you're following this, it can be easily duplicated using whatever database/table you like)
--Execute this in 1st window (this creates exclusive lock on table given that it's within a transaction)
begin tran update tbl set textdata = '' from [dbo].[RIC1PDWSWSQL01_20120227] tbl where textdata is null --To stop testing... rollback --rollback
--Execute this in 2nd window (this session will be blocked by the update statement within an open tran)
select * from [dbo].[RIC1PDWSWSQL01_20120227] tbl
- Confirmed via wmieventhelper.exe that the permanent event was configured
- Confirmed blocking condition was triggering the WMI event via using wbemtest.exe tool
- Could not see any events firing via the WMIExplorer.exe tool
But the test.ps1 script apparently never fires as I see no update to the test.out file. I'm not sure if there's an error (don't see any via any of the WMI exploration tools mentioned) and, again, I tested it to ensure there would be no issues. I feared that the issue may be the reference to the %TargetInstance% WMI token but that shouldn't keep test.ps1 from running and at least outputting an error (or a null parameter passed).
Any help would be appreciated.
Thanks,
Raul