Sunday, 28 November 2010

PowerShell Bug?

A customer of mine had an interest in automatically sending a welcome email to newly created mailboxes. They had found Pat Richard's script to do just this, and asked me to test in a lab environment.

On running the script manually to test functionality, I received the following error:

Get-Date : A parameter cannot be found that matches parameter name '11/28/2010 08:55:45'


What I noticed straight away is that the date format wasn't in UK format, even though I confirmed that the server was set to use this format.

Digging deeper, I found out that the issue only occured when the script used either the New-ItemProperty or Set-ItemProperty cmdlets to manipulate the registry. Pat's script had the following line in it, to create a new registry key used to determine when the script was last run:

New-ItemProperty -path HKLM:\Software\Innervation\$strScriptName -Name "LastRun" -Value (Get-Date) -propertyType String Out-Null

As you can see from this line of code, the value of the registry key is set to the output of the Get-Date cmdlet. I opened regedit and looked at the value stored:


That's definitely not in UK format even though I knew the server was configured accordingly. It would seem that this is a bug with PowerShell. My workaround was to change the format of the Get-Date cmdlet in the script such that the 'value' parameter was set like this:

-Value (Get-Date -Format G)

I had to make this change in the script for the line that used the Set-ItemProperty cmdlet. Once I'd made the change I deleted the registry key that had been created earlier and re-run the script. The result was:


I've no idea why PowerShell does this and can only assume it is a bug. There's certainly nothing wrong wih Pat's script - it works just great!

2 comments:

Thomas said...

Small typo -

Between the last two graphics, you have a line that says:

-Value (Get-Date Format G)

This should of course be:

-Value (Get-Date -Format G)

Note the dash before the 'Format' parameter.

Thomas

Neil Hobson said...

Thanks Thomas. Yes, I'll correct that. I've also noticed I've missed the pipe character on the New-ItemProperty cmdlet. Fixed. :) Not that this changes my opinion this is a PowerShell bug. ;)