Monday 2 August 2010

Get-ConvertedTime.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script converts time using ConvertTime method.  
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample. 
  6. .NOTES 
  7.     File Name  : Get-ConvertedTime.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/08/get-convertedtimeps1.html
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/bb382835.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-ConvertedTime.ps1 
  17.     Local time zone: (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London 
  18.  
  19.     Converted 1/1/2010 12:01:00 AM Unspecified to 12/31/2009 07:01:00 PM 
  20.     Converted 1/1/2010 12:01:00 AM Utc to 12/31/2009 07:01:00 PM 
  21.     Converted 1/1/2010 12:01:00 AM Local to 12/31/2009 07:01:00 PM 
  22.     Converted 11/6/2010 11:30:00 PM Unspecified to 11/6/2010 07:30:00 PM 
  23.     Converted 11/7/2010 02:30:00 AM Unspecified to 11/6/2010 10:30:00 PM 
  24. #> 
  25. #Create an array of times to convert 
  26. [DateTime[]] $times = (New-Object system.dateTime 2010, 1, 1, 0, 1, 0),  
  27.                       (New-Object system.DateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Utc)),  
  28.                       (New-Object system.dateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Local)),                             
  29.                       (New-Object system.DateTime(2010, 11, 6, 23, 30, 0)), 
  30.                       (New-Object system.DateTime(2010, 11, 7, 2, 30, 0) ); 
  31.  
  32. # Retrieve the time zone for Eastern Standard Time (U.S. and Canada). 
  33. try { 
  34.   $Est = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time"); 
  35. catch { 
  36.   "Unable to retrieve the Eastern Standard time zone." 
  37.   return
  38.  
  39. #  Display the current time zone name. 
  40. "Local time zone: {0}`n" -f [system.TimeZoneInfo]::Local.DisplayName 
  41.  
  42. # Convert and display each time in the $times array 
  43.  foreach ($timeToConvert in $times)  { 
  44.   $TargetTime = [System.TimeZoneInfo]::ConvertTime($TimeToConvert, $Est
  45.   "Converted {0} {1} to {2}" -f $timeToConvert,$timeToConvert.Kind, $targetTime 
  46. }                         

No comments: