Thursday, 23 January 2014

Show-Message.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a function to display a message 
  4.     in a message block, then demonstrates its usage 
  5. .DESCRIPTION 
  6.     This script used Windows Forms to put up a message 
  7.     box containing text and a window title passed as  
  8.     parameters 
  9. .NOTES 
  10.     File Name  : Show-Message.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 3.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16. .EXAMPLE 
  17.     Left as an exercise to the Reader 
  18. #> 
  19.  
  20. Function Show-Message { 
  21.  
  22. [CmdletBinding()] 
  23. Param (  
  24.    [Parameter(Mandatory=$True,  
  25.               HelpMessage="Content of Message box")] 
  26.    [string]$Message
  27.  
  28.    [Parameter(Mandatory=$False
  29.              HelpMessage="Title for Message box")] 
  30.    [string]$BoxTitle = "Message" 
  31. )           
  32.  
  33. # Just in case, load the relevant assembly 
  34. $v1 = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"
  35.  
  36. # Now use the messagebox class to display the message 
  37. [Windows.Forms.MessageBox]::Show($Message, $BoxTitle,  
  38.        [Windows.Forms.MessageBoxButtons]::OK ,  
  39.        [Windows.Forms.MessageBoxIcon]::Information)  
  40.  
  41. } # End of function 
  42.  
  43. # Set an alias 
  44. Set-Alias sm Show-Message 
  45.  
  46. # call the function 
  47. sm 'testing' 'details, details' 
Technorati Tags: ,

Sunday, 5 January 2014

Show-TimeSpanFormatting.ps1

  1. <#  
  2. .SYNOPSIS  
  3.     This script demonstrates formatting System.TimeSpan objects 
  4.     Using PowerShell  
  5. .DESCRIPTION  
  6.     This script re-writes some MSDN Samples that demonstrate  
  7.     timespan formatting - the original article lacks PowerShell 
  8.     Examples. And sadly, the MSDN page no longer accepts community 
  9.     additions. 
  10.  
  11. .NOTES  
  12.     File Name  : Show-Timespanformatting 
  13.     Author     : Thomas Lee - tfl@psp.co.uk  
  14.     Requires   : PowerShell Version 3.0  
  15. .LINK  
  16.     This script posted to:  
  17.         http://www.pshscripts.blogspot.com 
  18.     Reference MSDN Page 
  19.          http://msdn.microsoft.com/en-us/library/ee372286%28v=vs.110%29.aspx 
  20. .EXAMPLE  
  21.     Psh> .\Show-TimeSpanFormatting.ps1  
  22.     Example 1 
  23.     Time of Travel: 1.12:24:02 
  24.     Time of Travel: 1.12:24:02 
  25.     Example 2 
  26.     Converted '1.03:14:56.1667' to 1.03:14:56.1667000 
  27.     Converted '1.03:14:56.1667' to 1.03:14:56.1667000 
  28.     Example 3 
  29.     07:45:16 - 18:12:38 = -10:27:22 
  30.     07:45:16 + 18:12:38 = 1.01:57:54 
  31.     00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756 
  32.     Example 4 
  33.     7:45:16 - 18:12:38 = -10:27:22 
  34.     7:45:16 + 18:12:38 = 1:1:57:54 
  35.     0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756 
  36.     Example 5 
  37.     0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000 
  38.     0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000 
  39.     0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756 
  40. #>  
  41.  
  42. # Show Time Span Format Strings 
  43.  
  44. # Example 1 - use both tostring() and -f operators 
  45. "Example 1" 
  46. # Create Timespan object 
  47. $duration = New-Object System.TimeSpan 1, 12, 23, 62 
  48.  
  49. # Now output using both tostring() and -f 
  50. "Time of Travel: " + $duration.ToString("c"
  51. "Time of Travel: {0:c}" -f $duration 
  52.  
  53. # Example 2 
  54. # Demonstrate the use of ParseExact and TryParseExact 
  55. "Example 2" 
  56.  
  57. $value = "1.03:14:56.1667" 
  58. $interval = New-Object System.TimeSpan 
  59.  
  60. Try { 
  61.      $interval = [System.TimeSpan]::ParseExact($value, "c", $null
  62.      "Converted '{0}' to {1}" -f $value, $interval 
  63. }    
  64. Catch [System.FormatException] {"{0}: Bad Format" -f $value}    
  65. Catch [System.OverflowException] {"{0}: Out of Range" -f $value   } 
  66.  
  67. If ([System.TimeSpan]::TryParseExact($value, "c", $null, [ref] $interval)) { 
  68.   "Converted '{0}' to {1}" -f $value, $interval 
  69. Else { 
  70. "Unable to convert {0} to a time interval." -f $value 
  71.  
  72. # Example 3 
  73. # Create two TimeSpan objects, perform arithmetic operations 
  74. # on them then displays the result using the 'C' format specifier 
  75. "Example 3" 
  76.  
  77. $interval1 = New-Object System.TimeSpan 7, 45, 16 
  78. $interval2 = New-Object System.TimeSpan 18, 12, 38 
  79.  
  80. "{0:c} - {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 - $interval2
  81. "{0:c} + {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 + $interval2
  82.  
  83. $interval1 = New-Object System.TimeSpan 0, 0, 1, 14, 365 
  84. $interval2 = [System.TimeSpan]::FromTicks(2143756) 
  85. "{0:c} + {1:c} = {2:c}" -f $interval1, $interval2, $($interval1 + $interval2
  86.  
  87. # Example 4 - The "g" Format Specifier 
  88. # This specifier returns the string representation of a TimeSpan value in a compact form 
  89. # by including only the elements that are necessary. 
  90. "Example 4" 
  91.  
  92. $interval1 = New-Object System.TimeSpan 7, 45, 16 
  93. $interval2 = New-Object System.TimeSpan 18, 12, 38 
  94.  
  95. "{0:g} - {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 - $interval2
  96.  
  97. # do it in French 
  98. $CI = New-object System.Globalization.CultureInfo 'fr-FR' 
  99. [System.String]::Format($CI, $("{0:g} + {1:g} = {2:g}") , 
  100.                         $interval1,$interval2,$($interval1 + $interval2) ) 
  101. # Another interval 
  102. $interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36 
  103. $interval2 = [System.TimeSpan]::FromTicks(2143756)       
  104. "{0:g} + {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 + $interval2
  105.  
  106. # Example 5 
  107. # Using the "G' Specifier 
  108. # 
  109. "Example 5" 
  110. $interval1 = new-object System.TimeSpan 7, 45, 16 
  111. $interval2 = new-object System.TimeSpan 18, 12, 38 
  112. "{0:G} - {1:G} = {2:G}" -f $interval1, $interval2, $($interval1 -$interval2) 
  113. [System.String]::Format($(New-Object CultureInfo("fr-FR")),  
  114.                         "{0:G} + {1:G} = {2:G}", $interval1,  
  115.                         $interval2, $($interval1 + $interval2)) 
  116.                          
  117. $interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36 
  118. $interval2 = [System.TimeSpan]::FromTicks(2143756)  
  119. "{0:G} + {1:G} = {2:G}" –f $interval1,$interval2, $($interval1 + $interval2

Thursday, 14 November 2013

Fix-FileName.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Defines a function to remove 'invalid' characters 
  4.     from a file name. 
  5. .DESCRIPTION 
  6.     Some programs do not like certain 'invalid' characters 
  7.     in a file name used by that application. The function 
  8.     takes a look at each the file name and replaces some invalid 
  9.     characters with '-'
  10.  
  11.     This function takes a file name and 'fixes' it and returns 
  12.     the 'fixed' file name. Needless to say the characters to match 
  13.     and what to replace them with is an application specific decision! 
  14. .NOTES 
  15.     File Name  : Fix-FileName.ps1 
  16.     Author     : Thomas Lee - tfl@psp.co.uk 
  17.     Requires   : PowerShell Version 3.0 
  18. .LINK 
  19.     This script posted to: 
  20.         http://www.pshscripts.blogspot.com 
  21. .EXAMPLE 
  22.     Psh> .\Fix-FileName.ps1 
  23.     File name was: 123{}{{{|[\] 
  24.     Fixed name is: 123-------- 
  25.  
  26. #> 
  27.  
  28.  
  29. Function Fix-FileName { 
  30. [CMdletbinding()] 
  31. Param ( 
  32. $fn = $(throw 'no file name specified - returning'
  33.  
  34. Switch -Regex ($fn) { 
  35.   "}"  { $fn = $fn -replace '{','-'  } 
  36.   "}"  { $fn = $fn -replace '}','-'  } 
  37.   "\]" { $fn = $fn -replace ']','-'  } 
  38.   "\[" { $fn = $fn -replace '\[','-'
  39.   "\\" { $fn = $fn -replace '\\','-' } 
  40.   "\|" { $fn = $fn -replace '\|','-' } 
  41. } 
  42. $fn 
  43. } 
  44.  
  45. $fn = "123{}{{{|[\]" 
  46. $fnf = Fix-FileName $fn 
  47. "File name was: $fn" 
  48. "Fixed name is: $fnf

Wednesday, 16 October 2013

Get-SQLServer2.ps1

  1. #Requires -Version 3.0 
  2. <# 
  3. .SYNOPSIS 
  4.     This script Gets a list of SQL Severs on the Subnet 
  5. .DESCRIPTION 
  6.     This script uses SMO to Find all the local SQL Servers  
  7.     and displays them 
  8.  
  9. .NOTES 
  10.     File Name  : Get-SQLServer2.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 3.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16. .EXAMPLE 
  17.     PS>  # On a Lync Server looking at Lync Implementation 
  18.     PS>  Get-SQLServer2 
  19.     There are 7 SQL Server(s) on the Local Subnet 
  20.  
  21.     ServerName      InstanceName Version      
  22.     ----------      ------------ -------      
  23.     2013-LYNC-MGT   MON          10.50.2500.0 
  24.     2013-LYNC-MGT   SCOM         10.50.2500.0 
  25.     2013-TS         RTCLOCAL     11.0.2100.60 
  26.     2013-SHAREPOINT SPSDB        11.0.3000.0  
  27.     2013-LYNC-FE    RTC          11.0.2100.60 
  28.     2013-LYNC-FE    RTCLOCAL     11.0.2100.60 
  29.     2013-LYNC-FE    LYNCLOCAL    11.0.2100.60 
  30.      
  31. #> 
  32. Import-Module SQLPS 
  33.  
  34. # Now get all the database servers on the local subnet 
  35.  
  36. $SQLservers = [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() 
  37. $Srvs= @() 
  38.  
  39. # Convert collection to an array 
  40. Foreach ($srv in $SQLservers) { 
  41. $srvs += $srv 
  42.  
  43. # Now display results 
  44. If ($Srvs.count -LE 0) { 
  45. "There are no SQL Servers on the Local Subnet" 
  46. return
  47.  
  48. # Now print server details 
  49. "There are {0} SQL Server(s) on the Local Subnet" -f $Srvs.count 
  50. $Srvs | Select ServerName, InstanceName, Version | Format-Table -AutoSize 

Saturday, 12 October 2013

Get-OLCalendarItem

  1. Function Get-OLCalendarItem { 
  2.  
  3.  
  4. <# 
  5. .SYNOPSIS 
  6.     A function to retreive Outlook Calender items between two dates.  
  7.     Returns PSobjects containing each item. 
  8. .DESCRIPTION 
  9.     The function opens one's outlook calender, then retrieves the items.  
  10.     The function takes 2 parameter: start, end - items are returned that  
  11.     start betweween these two dates. 
  12. .NOTES 
  13.     File Name  : Get-OLCalendarItem 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 3.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.      
  20. .EXAMPLE 
  21.     Left as an exercise for the reader      
  22.  
  23. #> 
  24.  
  25. [CmdletBinding()] 
  26. Param ( 
  27. $start = $(Get-Date) , 
  28. $end   = $((Get-date).AddMonths(1)) 
  29.  
  30. Write-Verbose "Returning objects between: $($start.tostring()) and $($end.tostring())" 
  31. # Load Outlook interop and Outlook iteslf 
  32. [Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null 
  33. $Outlook = new-object -comobject outlook.application 
  34.  
  35. # Get OL default folders 
  36. $OlFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type] 
  37. $Namespace = $Outlook.GetNameSpace("MAPI"
  38. $Calendar = $namespace.GetDefaultFolder($olFolders::olFolderCalendar) 
  39. Write-Verbose "There are $($calendar.items.count) items in the calender in total" 
  40.  
  41. # Now return psobjects for all items between 2 dates 
  42. ForEach ($citem in ($Calendar.Items | sort start)) { 
  43. #Write-Verbose "Processing [$($citem.Subject)]  Starting: [$($Citem.Start)]" 
  44.  
  45. If ($citem.start -ge $start -and $citem.start -LE $end) {  
  46.  
  47. $CalHT =[ordered]  @{ 
  48. Subject          =  $($Citem.Subject) 
  49. Location         =  $($Citem.Location); 
  50. Start            =  $(Get-Date $Citem.Start); 
  51. StartUTC         =  $(Get-Date $Citem.StartUTC);                                   
  52. End              =  $(Get-Date $Citem.End); 
  53. EndUTC           =  $(Get-Date $Citem.EndUTC); 
  54. Duration         =  $($Citem.Duration); 
  55. Importance       =  $($Citem.Importance); 
  56. IsRecurring      =  $($Citem.IsRecurring); 
  57. AllDayEvent      =  $($citem.AllDayEvent); 
  58. Sensitivity      =  $($Citem.Sensitivity); 
  59. ReminderSet      =  $($Citem.ReminderSet); 
  60. CreationTime     =  $($Citem.CreationTime); 
  61. LastModificationTime = $($Citem.LastModificationTime); 
  62. Body             =  $($Citem.Body); 
  63.  
  64.  
  65. # Write the item out as a custom item 
  66. $o=New-Object PSobject -Property $CalHT 
  67. Write-Output $o 
  68.  
  69. } #End of foreach 
  70.  
  71. }  # End of function 
  72.  
  73. Set-Alias GCALI Get-OLCalendarItem  
Technorati Tags: ,

Wednesday, 28 August 2013

Get-DHCPPerf.Ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays perf counters for DHCP 
  4. .DESCRIPTION 
  5.     This script uses the Get-Counter cmdlet to get all 
  6.     the counters for DHCP. The function does this for one 
  7.     DHCP server at a time. 
  8. .NOTES 
  9.     File Name  : Show-.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     C:\Foo> Get-DHCPPerformanceCounters -comp dhcp2 
  17.      Performance counters for: dhcp2 
  18.      \\dhcp1\dhcp server\failover: bndupd dropped.                                           0 
  19.      \\dhcp1\dhcp server\failover: transitions to recover state.                             8 
  20.      \\dhcp1\dhcp server\failover: transitions to partner-down state.                        0 
  21.      \\dhcp1\dhcp server\failover: transitions to communication-interrupted state.           9 
  22.      \\dhcp1\dhcp server\failover: bndupd pending in outbound queue.                         0 
  23.      \\dhcp1\dhcp server\failover: bndack received/sec.                                      0 
  24.      \\dhcp1\dhcp server\failover: bndack sent/sec.                                          0 
  25.      \\dhcp1\dhcp server\failover: bndupd received/sec.                                      0 
  26.      \\dhcp1\dhcp server\failover: bndupd sent/sec.                                          0 
  27.      \\dhcp1\dhcp server\denied due to nonmatch.                                             0 
  28.      \\dhcp1\dhcp server\denied due to match.                                                0 
  29.      \\dhcp1\dhcp server\offer queue length                                                  0 
  30.      \\dhcp1\dhcp server\releases/sec                                                        0 
  31.      \\dhcp1\dhcp server\declines/sec                                                        0 
  32.      \\dhcp1\dhcp server\nacks/sec                                                           0 
  33.      \\dhcp1\dhcp server\acks/sec                                                            0 
  34.      \\dhcp1\dhcp server\informs/sec                                                         0 
  35.      \\dhcp1\dhcp server\requests/sec                                                        0 
  36.      \\dhcp1\dhcp server\offers/sec                                                          0 
  37.      \\dhcp1\dhcp server\discovers/sec                                                       0 
  38.      \\dhcp1\dhcp server\conflict check queue length                                         0 
  39.      \\dhcp1\dhcp server\active queue length                                                 0 
  40.      \\dhcp1\dhcp server\milliseconds per packet (avg).                                     28 
  41.      \\dhcp1\dhcp server\packets expired/sec                                                 0 
  42.      \\dhcp1\dhcp server\duplicates dropped/sec                                              0 
  43. #> 
  44.  
  45.  
  46.  
  47.  
  48. # Get-DHCPPerformanceCounters function 
  49.  
  50. Function Get-DHCPPerformanceCounters { 
  51. Param ( 
  52. $comp = 'localhost'
  53.  
  54. # Get DHCP Counters 
  55. $set = get-counter -listset "DHCP Server" 
  56. $ctrs = $set.counter 
  57.  
  58. " Performance counters for: {0}" -f $comp 
  59. # For each counter, get the sample 
  60. foreach ($ctr in $ctrs) { 
  61.  $sample = (get-counter $ctr).countersamples 
  62.  
  63.  
  64.  "{0,-78} {1,10}" -f $sample.path, $sample.cookedvalue 
  65.  
  66.  
  67.  
  68. # Now test it 
  69. Get-DHCPPerformanceCounters -comp dhcp1 
  70. Get-DHCPPerformanceCounters -comp dhcp2 
Technorati Tags:

Monday, 29 July 2013

Get-Zip.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the Zip lib in .NET 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-ZIP.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-Zip 
  17.      
  18.     Directory: C:\example 
  19. Mode        LastWriteTime  Length Name                                                                                                                            
  20. ----        -------------  ------ ----                                                                                                                            
  21. d---- 7/29/2013   4:35 PM        extract                                                                                                                         
  22. d---- 7/29/2013   4:29 PM        start                                                                                                                           
  23. -a--- 7/29/2013   4:35 PM   1668 result.zip                                                                                                                      
  24.  
  25.  
  26.     Directory: C:\example\extract 
  27. Mode        LastWriteTime  Length Name                                                                                                                            
  28. ----        -------------  ------ ----                                                                                                                            
  29. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  30. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  31. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  32. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  33. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  34.  
  35.  
  36.     Directory: C:\example\start 
  37. Mode        LastWriteTime     Length Name                                                                                                                            
  38. ----        ------------     ------ ----                                                                                                                            
  39. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  40. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  41. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  42. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  43. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  44. #> 
  45.  
  46. # Load the compression namespace  
  47. # And yes, I know this usage is obsolete - but it works.  
  48. # Ignore the output  
  49. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | out-null 
  50.  
  51. # Set locations 
  52. $startPath = "c:\example\start" 
  53. $zipPath = "c:\example\result.zip" 
  54. $extractPath = "c:\example\extract" 
  55. Remove-Item $zipPath -ea SilentlyContinue 
  56. Remove-Item -Path $extractPath -inc * -Recurse -ea SilentlyContinue 
  57.  
  58. # Create the zip file 
  59. [System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath
  60.  
  61. # Extract from zip and show what's all there 
  62. [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath,$extractPath); 
  63. ls c:\example -Recurse 
  64.          

Sunday, 28 July 2013

Show-NumberGroupSizes.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the NumberGroupSizes 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-NumberGroupSizes.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupsizes%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberGroupSizes.ps1 
  17.     123,456,789,012,345.00 
  18.     12,3456,7890,123,45.00 
  19.     1234567890,123,45.00 
  20. #> 
  21.  
  22. # Get Number Format 
  23. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  24. $nfi = $nf.NumberFormat 
  25.  
  26. [Int64] $myInt = 123456789012345 
  27. $myInt.ToString( "N", $nfi
  28.  
  29. # Display the same value with different groupings 
  30. [int[]] $MySizes1 = 2,3,4 
  31. [int[]] $MySizes2 = 2,3,0 
  32.  
  33. $nfi.NumberGroupSizes = $mySizes1 
  34. $myInt.ToString( "N",$nfi
  35. $nfi.NumberGroupSizes = $mySizes2 
  36. $myInt.ToString( "N", $nfi )  

Friday, 28 June 2013

Show-CurrencyFormatting.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample showing the  
  4.     use of the NumberFormat class to nicely format things 
  5.     in this case, currency. 
  6. .DESCRIPTION 
  7.     This script iterates through the Windows cultures and 
  8.     displays those whose 2-letter ISO code is 'en' and  
  9.     displays how Windows formats currency in that culture.  
  10. .NOTES 
  11.     File Name  : Show-CurrencyFormatting.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN sample posted to: 
  18.         http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx 
  19. .EXAMPLE 
  20.     Psh > .\Show-CurrencyFormatting.ps1 
  21.     The currency symbol for 'English (United States)' is '$' 
  22.     The currency symbol for 'English (United Kingdom)' is '£' 
  23.     The currency symbol for 'English (Australia)' is '$' 
  24.     The currency symbol for 'English (Canada)' is '$' 
  25.     The currency symbol for 'English (New Zealand)' is '$' 
  26.     The currency symbol for 'English (Ireland)' is '€' 
  27.     The currency symbol for 'English (South Africa)' is 'R' 
  28.     The currency symbol for 'English (Jamaica)' is 'J$' 
  29.     The currency symbol for 'English (Caribbean)' is '$' 
  30.     The currency symbol for 'English (Belize)' is 'BZ$' 
  31.     The currency symbol for 'English (Trinidad and Tobago)' is 'TT$' 
  32.     The currency symbol for 'English (Zimbabwe)' is 'Z$' 
  33.     The currency symbol for 'English (Republic of the Philippines)' is 'Php' 
  34.     The currency symbol for 'English (Singapore)' is '$' 
  35.     The currency symbol for 'English (Malaysia)' is 'RM' 
  36.     The currency symbol for 'English (India)' is 'Rs.' 
  37.  
  38. #> 
  39.  
  40. #  Loop through all the specific cultures known to the CLR. 
  41. foreach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))  
  42.    { 
  43.      # Only show the currency symbols for cultures that speak English. 
  44.      if ($ci.TwoLetterISOLanguageName -eq "en") { 
  45.      # Display the culture name and currency symbol. 
  46.         $nfi = $ci.NumberFormat 
  47.         "The currency symbol for '{0}' is '{1}'" -f $ci.DisplayName, $nfi.CurrencySymbol 
  48.      } 
  49.    } 

Thursday, 20 June 2013

Show-CurrencyGroupSize.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reimplements a code sample from MSDN in PowerShell. 
  4.     This sample formats and display currency using standard and 
  5.     different currency groupings.  
  6. .DESCRIPTION 
  7.     This script displays a currency using standard, then two 
  8.     custom CurrencyGroupSizes. 
  9. .NOTES 
  10.     File Name  : Show-CurrencyGroupSize.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx 
  18. .EXAMPLE 
  19.     PSH:> .\Show-CurrencyGroupSize.ps1 
  20.     Default numeric format string "C" 
  21.     $123,456,789,012,345.00 
  22.  
  23.     Display with array = 2,3,4 
  24.     $12,3456,7890,123,45.00 
  25.  
  26.     Display with array = 2,3,0 
  27.     $1234567890,123,45.00 
  28. #> 
  29.  
  30. #     Get a NumberFormatInfo associated with the en-US culture. 
  31. $fi = new-object System.Globalization.CultureInfo "en-US", false 
  32. $nfi = $fi.NumberFormat 
  33.  
  34. #     Display a value with the default separator (".") 
  35. "Default numeric format string `"C`"" 
  36. [Int64] $myInt = 123456789012345 
  37. $myInt.ToString( "C", $nfi
  38.  
  39. #    Display the same value with different groupings. 
  40. [int[]] $mySizes1 = (2,3,4) 
  41. $mySizes = 2,3,0 
  42.  
  43. "";"Display with array = 2,3,4" 
  44. $nfi.CurrencyGroupSizes = $mySizes1 
  45. $myInt.ToString( "C", $nfi
  46.  
  47. "";"Display with array = 2,3,0" 
  48. $nfi.CurrencyGroupSizes = $mySizes2 
  49. $myInt.ToString( "C", $nfi