Sunday, 28 February 2010

Using-Culture.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a function to run culture sensitive operations
  4.     in some other culture. 
  5. .DESCRIPTION 
  6.     The Using-Culture function gets the current culture, saves it, then
  7.     runs the script block in the requested culture. The function then
  8.     restores the culture.  The script then ends with calls to
  9.     Using-Culture to demonstrate its use.
  10.  
  11.     The script is based on a blog note at: http://blogs.msdn.com/powershell/archive/2006/04/25/583235.aspx
  12. .NOTES 
  13.     File Name  : Using-Culture.ps1 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 2.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://pshscripts.blogspot.com/2010/02/using-cultureps1.html
  19. .EXAMPLE 
  20.     run the script and see! 
  21. #> 
  22.  
  23. # Defind the Using-Culture function 
  24. Function Using-Culture { 
  25. Param ( 
  26. [System.Globalization.CultureInfo]$culture = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"), 
  27. [ScriptBlock]$script= (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"
  28.     $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture 
  29.     trap  
  30.     { 
  31.         [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture 
  32.     } 
  33.     [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture 
  34.     Invoke-Command $script 
  35.     [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture 
  36. ## 
  37. # Now use the function 
  38. ## 
  39.  
  40. # Here is an example of Using-Culture 
  41. "Get-Date using ar-IQ" 
  42. using-culture ar-IQ {get-date} 
  43. "" 
  44. # Parse ar-IQ formatted date into local culture 
  45. "Parse IQ date to English and display" 
  46. $IQD = "30 تشرين الثاني, 2005 09:01:38 ص" 
  47. using-culture ar-IQ {$global:d=[DateTIme]::Parse($IQD)} 
  48. $IQD 
  49. $d 
  50. "" 
  51. # Now do it in German 
  52. "Get-Date using de-de" 
  53. using-culture de-de {get-date} 
  54. "" 
  55. "Parsing a german date into local" 
  56. $ded="Mittwoch, 30. November 2005 09:02:29" 
  57. using-culture de-de {$global:d=[DateTIme]::Parse($DED)} 
  58. $ded 
  59. $d 

No comments: