Month: June 2011

Windows Presentation Foundation via PowerShell – Introduction

I did a survey (on GUI for PowerShell Scripts) a while ago to understand what frameworks do people use when they need to create a user interface (UI) for their PowerShell scripts. The responses clearly showed WinForms was one of the favorite choices for several reasons I mentioned in that post. Most used UI frameworks Both WPK and PowerBoots were powerful frameworks which enabled creation of Windows Presentation Framework (WPF) based user interfaces in PowerShell. But, in my opinion, lack of community examples and WPF awareness made people to stay away from these frameworks. However, this perception may slowly change. The brains behind WPK and PowerBoots, came together to create what is known as ShowUI. This new framework promises to make the complicated world of WPF easy to use in PowerShell. ShowUI is currently in version 1.0 and can be downloaded from http://showui.codeplex.com/.  James, Joel, and Doug have been doing several blog posts and videos to show the power of ShowUI. So, this is the right time to start exploring ShowUI, ask questions, or provide …

Attaching scripts or tasks to Windows event log entries using PowerShell and WMI

During a few load test iterations on a SharePoint farm, I started seeing some SQL exceptions in the application log of SharePoint servers. If you are familiar with SharePoint platform, you may have seen these events such as event ID 3355. This event complains that the SharePoint server cannot connect to SQL server. This need not really mean that the DB server is offline. So, to find out the real reason behind these event logs, I needed to start some trace activities whenever event ID 3355 gets logged. Initially, I was looking for eventtriggers.exe which is meant for attaching a script or task to an event log. However, I could neither find this on Windows Server 2008 R2 nor an external download. So, I wanted to look at other options I had.  I found that there are multiple ways to achieve this. Attach to script or task to the event in Windows Event Viewer You can find this option in event viewer. This link is available in the actions pane of event viewer upon selecting …

Do you want -Continuous parameter with Test-Connection cmdlet?

In my work inside the lab, I end up using ping -t almost everyday while performing load test or checking network connectivity issues, etc. I use ping.exe as Test-Connection cmdlet has no -Continuous parameter. This means that I cannot ping a computer until it goes down or comes alive. Test-Connection cmdlet has -Count parameter and we can end up using a BIG (at present this is [int] type) number to ping for a longer time. However, fixed count is always fixed. It does not mean testing the connection until the system comes alive or goes offline. The latter is important in many scenarios. For example, think about a scenario where you would be able to test a remote system to see if there is a connection loss and trigger an event when you detect that loss. So, I was discussing this with @ShayLevy and he quickly came up with the logic for a proxy function.

*This works but there is not clear error checking as this is just proof-of-concept code. Use it at your …

Passing variables or arguments to an event action in PowerShell

Update: Based on feedback from @ShayLevy, we don’t have to send the background job object to Event action as a variable. It is already available as $event.Sender or just $sender. You can see that in the image below. I just picked up a wrong example to explain the -MessageData parameter. Although, the article originally showed a background job as an example, it was just to show how any object can be passed to event action. For example, if all you want to access is a background job object which you are monitoring using Register-ObjectEvent, you can access the job name using $event.Sender.Name or $sender.Name, job Id using $event.Sender.Id or $sender.Id, and so on. To avoid any confusion, I removed the initial example of background job and updated it with a more generic example. While working with @LaerteSQLDBA on a SQL SMO related script, I was asked a question on how to pass variables or arguments and access them within an event action script block. The solution is simple. We just use the -MessageData parameter of Register-ObjectEvent. …