Tuesday, October 23, 2012

SharePoint PowerShell Regional Settings Gotcha

I was trying to work out how to change the regional settings for a site using PowerShell and got the following working:

$web = Get-SPWeb http://foo
$web.Locale = 3081
$web.RegionalSettings.TimeZone.ID = 76
$web.Update()
$web.Dispose()

However when I put the setting values into our XML config file I got the following error on the setting of the Locale.

Exception setting "Locale": "Cannot convert value "3081" to type "System.Global
ization.CultureInfo". Error: "Culture name '3081' is not supported.
Parameter name: name""
At line:2 char:6
+ $web. <<<< Locale = "3081"
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

The problem is that anything read from the XML file was coming across as a string.

The Locale property is of type CultureInfo which has an overloaded constructor that takes either an integer or a string. PowerShell attempts to coerce the type using best fit and so uses the constructor that takes a string. So to fix this I either needed to first coerce the string value to integer using something like:

$web.Locale = 0 + $locale

or used the Culture Name rather than the identifier, so the equivalent of:

$web.Locale = "en-au"

I changed the 3081 to "en-au" as I thought that was more informative.

The interesting thing is though that the TimeZone.ID property is a unsigned 16 bit integer so powershell had no problem converting and it works fine.

Incidently the default time zone for 3081 appears to be Hobart (42) while I'm setting this to Sydney, Melbourne, Canberra (76). Considering the collective portions of Australia that both time zones represent I was a little suprised by this choice. I suspect it takes the first matching timezone in order of ID.

No comments:

Post a Comment