24/7/365 Support

Program: Invoke a Script Block with Alternate Culture Settings

Given PowerShell’s diverse user community, scripts that you share will often be run on a system set to a language other than English. To ensure that your script runs properly in other languages, it is helpful to give it a test run in that culture. Example 126 lets you run the script block you provide in a culture of your choosing.

Example 126. UseCulture.ps1

############################################################################## ## ## UseCulture.ps1 ## ## Invoke a scriptblock under the given culture ## ## ie: ## ## PS >UseCulture frFR { [DateTime]::Parse("25/12/2007") } ## ## mardi 25 décembre 2007 00:00:00## ## ##############################################################################

Example 126. UseCulture.ps1 (continued)

param( [System.Globalization.CultureInfo] $culture = $(throw "Please specify a culture"), [ScriptBlock] $script = $(throw "Please specify a scriptblock") )

## A helper function to set the current culture function SetCulture([System.Globalization.CultureInfo] $culture) {

[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture }

## Remember the original culture information $oldCulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture

## Restore the original culture information if ## the user's script encounters errors. trap { SetCulture $oldCulture }

## Set the current culture to the user's provided ## culture. SetCulture $culture

## Invoke the user's scriptblock & $script

## Restore the original culture information. SetCulture $oldCulture

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say