Don’t hire ghost hunters who are afraid of ghosts

Your house is haunted. You can’t stand living with the ghosts anymore and decide to call in the professional, a ghost hunter. The ghost hunter comes, nicely equipped with all kinds of shiny ghost hunting equipment, and proceeds to immediately pass out and fall to to the floor with a loud thud at first sight of the ghosts.

Well, in a nutshell, that is the story of the new Event Viewer in Windows Vista and Windows Server 2008. The new Event Viewer looks very nice and all. Trouble is, it now won’t work when there is a syntax error in machine.config or other .NET Framework problems. Apparently, in Windows Vista and Windows 2008, someone at Microsoft decided to rewrite the Event Viewer utility (a perfectly usable and solid utility in previous versions of Windows) in .NET.

MMC could not create the snap-in. The snap-in might not have been installed correctly. Name: Event Viewer.

So, the lesson I learned from this is: don’t hire ghost hunters who are afraid of ghosts. Or, don’t write a utility designed to view monitoring and troubleshooting messages that can fail due to unnecessary dependencies. A critical system troubleshooting utility such as Event Viewer should be the last thing that fails.

What to do when you cannot run Event Viewer in Windows Vista/Windows Server 2008/or Windows 7?

If you get the following error:

MMC could not create the snap-in. The snap-in might now have been installed correctly. Name: Event Viewer. CLSID: FX:{b05566ad-fe9c-4363-be05-7a4cbb7cb510}

Try the following:

  • Fix any syntax errors in machine.config. Restore to a previously known working copy.
  • Rename EventVwr.exe.config in %WINDIR%\System32.

SQL Server Implicit String Conversion/Concatenation (XML parsing: line <x>, character <y>, unexpected end of input)

If you are getting this error in your SQL Server T-SQL script:, you may be running into an issue with implicit string conversion in SQL Server:

declare @xml varchar(max), @doc XML, @stringVariable varchar(256)
set @stringVariable = 'a string value'

-- @doc is set by concatenating multiple string literals
-- and string variables, with all the variables having less than
-- or equal to 8000 characters
set @xml = '<root>' +
... + @stringVariable +
...
'</root>'

print len(@xml)

set @doc = @xml

Output

8000
Msg 9400, Level 16, State 1, Line 4
XML parsing: line 64, character 74, unexpected end of input

As you can see in the output, the @xml variable was truncated to 8000 characters, resulting in an invalid XML. This is due to the way SQL Server performs implicit string conversions when concatenating strings. When all the string literals/variables involved in the concatenation are 8000 characters or less, the resulting string will be exactly 8000 characters.

The same issue occurs with NVARCHAR data type. Instead of the 8000-character limit, it’s 4000 characters.

A simple fix is to make sure at least one of the string variables is of type VARCHAR(MAX):

declare @xml varchar(max), @doc XML, @stringVariable varchar(256)
declare @x varchar(max)

set @stringVariable = 'a string value'
set @x = ''

set @xml = @x + '<root>' +
... + @stringVariable +
...
'</root>'

print len(@xml)

set @doc = @xml

 

More Info

Instant Messaging Etiquette for the Workplace

The use of instant messaging at the workplace is very prevalent these days. While instant messaging is very convenient for everyday communication, don’t treat it the same as email, face-to-face meetings, or phone calls. There are several significant differences between instant messaging and traditional forms of communications:

  • Instant messaging is not necessarily one-one-one. At any given moment, one person may be engaged in several simultaneous instant messaging conversations.
  • Even though your messages will be displayed on the recipient’s screen immediately, the recipient may not be able to read the messages and respond immediately.
  • With most instant messaging applications, the recipient cannot read your messages until you press the Enter key to send it. This limitation inherently makes instant messaging significantly slower than voice conversations.
  • Most people can speak faster than they can type.

Here are some guidelines on basic/everyday instant messaging etiquette that will help you and your co-workers make the most out of this communication medium.

Instant Messaging apps - AIM, MSN Messenger, Windows Live Messenger, Yahoo Messenger, Google Talk, ICQ 

Include Relevant Info/Questions in The First Message

Avoid typing greetings or non-essential messages separately first. Include the question or relevant information in your first message.

Not-so-good examples (avoid this)

Conversation 1

  • Jane (10:54:50 AM): Hi
  • Mike (10:55:01 AM): Hello
  • Jane (10:55:06 AM): The test server will be restarted in 1 minute.
  • Mike (10:56:11 AM): Thanks for the info

Conversation 2

  • Jane (2:15:08 PM): Good afternoon
  • Mike (2:15:13 PM): Good afternoon
  • Jane (2:15:13 PM): Do you have a few minutes to talk on the phone re project A?
  • Mike (2:15:18 PM): Sure, let me call you.

Conversation 3

  • Jane (4:03:30 PM): You there?
  • Mike (4:03:35 PM): Yes
  • Jane (4:03:42 PM): Can we have a short team meeting in conf room A
  • Mike (4:03:50 PM): Sure. Be there in 5.

Good examples

Conversation 1

  • Jane (10:54:50 AM): Hi, the test server will be restarted in 1 minute.
  • Mike (10:54:55 AM): Thanks for the info

Conversation 2

  • Jane (2:15:08 PM): Good afternoon, do you have a few minutes to talk on the phone re project A?
  • Mike (2:15:13 PM): Sure I will call you.

Conversation 3

  • Jane (4:03:30 PM): Can we have a short team meeting in conf room A?
  • Mike (4:03:35 PM): Sure, be right there.

Every time you send a message, the recipient is disrupted from whatever he/she is doing. The more you can delay this disruption the better, even if it’s only seconds. It’s also not necessary to ask the recipient if he/she is there. That’s what the online status (away/available) is for. You can just type your message or ask your question. One exception to this would be if your message contains sensitive information.

Send Complete Messages

Good:

  • Jane (9:15:23 AM): Hi, all integration tests are failing in the integration environment for the admin user group. Can you take a look?”

No so good:

  • Jane (9:15:23 AM): Hi, all integration tests are failing…
  • Jane (9:15:27 AM): the integration environment…
  • Jane (9:15:32 AM): for the admin user group…”
  • Jane (9:15:38 AM): Can you take a look?

If you have to send several sentences in sequence, compose them in a separate editor first (notepad would do), then type them out quickly in succession. That way the recipient does not have to wait for you while you type your next message, make editing corrections, etc.

Do Not Expect Responses Immediately

Unless the recipient reports to you, do not expect a response immediately after you type a message. The recipient may be in the middle of five other instant message conversations, on the phone, or working on something else more important. If the recipient’s status is not set to “Away”, and you have not received a response after a few minutes, it’s ok to ping him/her again. Maybe they forgot about your message.

Update Your Status and Respect Others’ Status

If you need to step out, change your status to “Away”, or “In a Meeting”, or “Back in an Hour”, or whatever is appropriate for the situation. This tells everyone else that you are not available to respond to messages immediately and save them from having to wait for your responses.

Pick Up the Phone

If the instant messaging conversation starts to go into lots of details and may make take longer than a few minutes, consider picking up the phone and continue the conversation there.

Using a Different Configured Binding in WCF Client

To programmatically switch bindings on the fly, you can do it via the constructor of the generated client:

var client = new WeatherClient(“MyEndpoint”);

“MyEndpoint” is the name of the endpoint defined in your config file:

<client>
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="http1" contract="MyContract" name="MyEndpoint" />
</client>

Show Your Papers!

They say a man’s home is his castle, and since my computer is my virtual home, I must have complete control over what goes on in it.  When I see a stranger person walking around in my yard, he’s better be ready to tell me who he is, what company he works for, why he’s in there. Likewise, when I see a strange window running in my computer, I must have the ability to easily tell what it is, who makes it, when it was installed, etc.

Yes, one would think that being able to quickly identify any running window would be a basic feature of any modern so called window operation system. It’s 25 years after the first release of Microsoft Windows, and the sad truth is that you still often cannot easily identify running windows.

Look at the example below. If you are not familiar with this utility, and you came back to your laptop seeing this, would you know what it’s about? Should you click Yes or No? Is this a legitimate application, or something more sinister?

image 

The first obvious problem is the missing message. That’s forgivable however. Bugs happen, files get corrupted, language resource files go missing, etc. What’s not acceptable is for the Windows OS not to provide any method to identify misbehaving windows.

So how about it Microsoft? Let us easily find out identifying information about any running Windows. Perhaps with with a click of a button, we can see:

  • Name of owning application/process
  • Name of vendor (if available)
  • Folder where executable resides
  • Date the application was installed
  • User who installed the application
  • If the user didn’t run the application himself, identify the parent process or service that launches the application (shortcut in Startup folder, registry, etc.)
  • Available code signatures

For now, if you want to identify any visible window, use Sysinternals’ Process Explorer. Drag the “Find Window’s Process” icon and drop it on top of the target window and Process Explorer will highlight the owner process in its window. From there, you can get the executable name, company name, folder location, etc.

Process Explorer Find Window's Process

WCF Client Error “The connection was closed unexpectedly” Calling Java/WebSphere 7 Web Service

If you get the following exception calling a WebSphere web service from your .NET WCF Client (service reference):

System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. —>  System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.

Try adding this code before the service call:

System.Net.ServicePointManager.Expect100Continue = false;

More info on the 100-Continue behavior from MSDN.

Give The Power of Speech and Sound to Your PowerShell Scripts

Do you ever have the problem where you start a long running script (such as running a code build), multi-task on something else on another monitor while waiting for the script to finish, and then totally forget about the script until half an hour later? Well, here’s a solution your problem: have your script give you holler at you when it’s done.

Hello sir! I am done running your command!

In my library script file, I have the following functions to play sound files and to speak any text:

function PlayMp3($path) {
    # Use the default player to play. Hide the window.
    $si = new-object System.Diagnostics.ProcessStartInfo
    $si.fileName = $path
    $si.windowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
    $process = New-Object System.Diagnostics.Process
    $process.startInfo=$si
    $process.start() 
}

function PlayWav($path) {
    $sound = new-Object System.Media.SoundPlayer;
    $sound.SoundLocation="$path";
    $sound.Play();
}

function Say($msg) {
    $Voice = new-object -com SAPI.SpVoice
    $Voice.Speak($msg, 1 )
}

If you like the text-to-speech feature but find Windows’ speech engine lacking, check out Ivona. It’s a commercial text-to-speech engine but you are allow to generate and download short speech files for free personal use. Now, my script can nicely interrupt me to tell me when it’s done. Other online text-to-speech engines: vozMe, SpokenText.

If Making Noise Is Not Your Thing

If making noise is not your thing, consider displaying a message in the Notification Area. Here’s the code (courtesy Microsoft TechNet):

Build complete!

function Get-ScriptName {
    $MyInvocation.ScriptName
} 

function DisplayNotificationInfo($msg, $title, $type) {
    # $type - "info" or "error"
    if ($type -eq $null) {$type = "info"}
    if ($title -eq $null) {$title = Get-ScriptName}

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 
    # Specify your own icon below
    $objNotifyIcon.Icon = "C:CdoScriptsFolder.ico"
    $objNotifyIcon.BalloonTipIcon = "Info"  
    $objNotifyIcon.BalloonTipTitle = $title
    $objNotifyIcon.BalloonTipText = $msg

    $objNotifyIcon.Visible = $True 
    $objNotifyIcon.ShowBalloonTip(10000)
}

Taking Control of Your Thermostat

Once in a while I come across a new product that solves a problem so elegantly that I just have to ask myself, why didn’t think of this before? It’s been very cold recently in the East Coast and when it gets very cold, my house’s gas heating system goes completely nuts. If I set the thermostat desired temperature to 70 degrees, the temperature in the bedroom will be in the roasting 80’s. The temperature differential depends how how cold it is outside, so I can’t just simply set the thermostat to a specific offset and forget either. I constantly need to get up in the middle of the night to adjust the thermostat downstairs when it gets cold outside. Why do I have to do this? I guess nobody told my house that we are in the 21st century.

image

So, the first thing I thought of is a remote control for the thermostat. Well, no surprise, they do make them. Apparently, my problem is fairly common for two-story homes with a single HVAC system. This Lux TX9000RF Programmable Thermostat with Remote looked very promising to me. A product like this would allow adjusting the thermostat temperature from anywhere in the house.

That still requires some work however. Hmm… what if there is a thermostat that can read the current temperature from a remote sensor? Bingo: they make those too. There are not many to be found, and after searching around, I decided to go for the  Honeywell YTHX9321R5003 Prestige HD Thermostat Kit and I’ve been very happy with the result so far. This kit is expensive, but very well made and it works as advertised. It also looks very nice. The kit includes the thermostat, a remote control/sensor, and an outdoor sensor. This kit is in Honeywell Pro Install line, which means it’s sold mainly through HVAC contractors and installers. I found the installation process only slightly more complicated than a regular programmable thermostat. The only thing you need to watch out for is that this thermostat requires a 24vac Common wire (commonly black in color), which may not be available in your setup. If that is the case, then you will need to run/fish a new wire from your furnace – a pretty big job.

Now with this cool new gadget hooked up and everything humming, all I have to do is bring the remote with me to the bedroom and push the button on it named “Read temp from this device” and I am set for the night. If I ever want to tweak the temperature for some reason, I can do it right there with the remote. If only everything else was this easy!

clip_image001

Automating Maven with PowerShell

I found out the hard way that mvn (Maven) on Windows always return a success code of true, which means you cannot use the return code ($?) to check whether the mvn command succeeded or failed. Why they decided to break this seemingly basic program contract is a mystery. A work-around is to scan the mvn output and look for specific strings such as “BUILD SUCCESSFUL”.

Here’s how:

function InvokeAndCheckStdOut($cmd, $successString, $failString) {
    Write-Host "====> InvokeAndCheckStdOut"
    $fullCmd = "$cmd|Tee-Object -variable result" 

    Invoke-Expression $fullCmd
    $found = $false
    $success = $false
    foreach ($line in $result) {
      if ($line -match $failString) {
       $found = $true
       $success = $false
       break
      }
      else {
       if ($line -match $successString) {
        $found = $true
        $success = $true
        #"[InvokeAndCheckStdOut] FOUND MATCH: $line"
        break
       }
       else {
        #"[InvokeAndCheckStdOut] $line"
       }
      }
    }

    if (! $success) {
      PlayWav "${env:windir}\Media\ding.wav"
      throw "Mvn command failed."
    }

    Write-Host "InvokeAndCheckStdOut <===="
}

function InvokeMvn($cmd) {
    InvokeAndCheckStdOut $cmd "BUILD SUCCESSFUL" "BUILD FAILED"
}

InvokeMvn "mvn clean install"

See Also

Tee-Object and Invoke-Expression in PowerShell

The PowerShell Tee-Object Cmdlet allows you to send command output to a file or a variable, and display it in the console at the same time. This is very useful for those instances where you need to parse the text output of a command. I had a hard time getting it to work with Invoke-Expression. After trying different things, I finally found the solution. To get Tee-Object to work with Invoke-Expression in PowerShell 1, include the Tee command in the Invoke-Expression command like this:

Invoke-Expression "mvn clean install | Tee –variable result”

The following, which I guess is what most people try first, doesn’t work (at least in PowerShell V1). I guess because you are storing the result of the “Invoke-Expression” command itself into the variable instead of “mvn clean install”.

    Invoke-Expression "mvn clean install” | Tee -variable result

Wrapping Invoke-Expression in parenthesis (see below) works, but has a drawback: the output is not written to Standard Out until the whole command finishes.

    (Invoke-Expression "mvn clean install”) | Tee -variable result