Use-Culture.ps1 by Lee Holmes 17 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2226"></script>download | new post
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- #############################################################################
- ##
- ## Use-Culture
- ##
- ## From Windows PowerShell Cookbook (O'Reilly)
- ## by Lee Holmes (http://www.leeholmes.com/guide)
- ##
- #############################################################################
- <#
- .SYNOPSIS
- Invoke a scriptblock under the given culture
- .EXAMPLE
- Use-Culture fr-FR { [DateTime]::Parse("25/12/2007") }
- mardi 25 decembre 2007 00:00:00
- #>
- param(
- ## The culture in which to evaluate the given script block
- [Parameter(Mandatory = $true)]
- [System.Globalization.CultureInfo] $Culture,
- ## The code to invoke in the context of the given culture
- [Parameter(Mandatory = $true)]
- [ScriptBlock] $ScriptBlock
- )
- Set-StrictMode -Version Latest
- ## A helper function to set the current culture
- function Set-Culture([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 { Set-Culture $oldCulture }
- ## Set the current culture to the user's provided
- ## culture.
- Set-Culture $culture
- ## Invoke the user's scriptblock
- & $ScriptBlock
- ## Restore the original culture information.
- Set-Culture $oldCulture
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository