Monday 31 October 2011

Show-CurrencyFormatting.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample showing the  
  4.     use of the NumberFormatInfo 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.         } 

Show-CalendarAlgorithm.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample showing the  
  4.     CalendarAlgorithmType enumeration.  
  5. .DESCRIPTION 
  6.     This script creates there calendars and displays  
  7.     algorithm type, 
  8. .NOTES 
  9.     File Name  : Show-CalendarAlgorithm.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.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/system.globalization.calendaralgorithmtype.aspx 
  17. .EXAMPLE 
  18.      
  19. #> 
  20.  
  21. # Helper function 
  22. Function Display{ 
  23. Param ([System.Globalization.Calendar] $c
  24. $name = $c.ToString().PadRight(50, '.'
  25. "{0} {1}" -f $name, $c.AlgorithmType 
  26.  
  27. ## Start of script 
  28.  
  29. # Create three new calendars 
  30. $grCal = new-object System.Globalization.GregorianCalendar 
  31. $hiCal = new-object System.Globalization.HijriCalendar 
  32. $jaCal = new-object System.Globalization.JapaneseLunisolarCalendar 
  33.  
  34. # Display them 
  35. Display($grCal); 
  36. Display($hiCal); 
  37. Display($jaCal); 
  38.  
  39. [enum]::GetNames([System.Globalization.CalendarAlgorithmType]) 

Show-CurrencyGroupSize

  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 tot: 
  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

Show-StandardNFS-1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the first two samples on this MSDN Page, 
  4.     convered to PowerShell  
  5. .DESCRIPTION 
  6.     This script displays two lines of text using PowerShell 
  7.     and .NET formatting information 
  8. .NOTES 
  9.     File Name  : Show-StandardNFS-1.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.     MSDN sample posted tot: 
  16.         http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx 
  17. .EXAMPLE 
  18.     [Cookham8:C:\foo]> .\Show-StandardNFS.ps1 
  19.     $123.46 
  20.     Your account balance is $123.46. 
  21. #> 
  22.  
  23. # Show formatting a number as currency 
  24. $value = 123.456 
  25. $value.ToString("C2"
  26.  
  27. # Show the -f operator with currency formatting 
  28. $value = 123.456 
  29. "Your account balance is {0:C2}." -f $value 

Tuesday 11 October 2011

Get-OSInstallDate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets/displays the Sysgtem Uptime after 
  4.     converting it from WEBM time/date format  
  5. .DESCRIPTION 
  6.     This script creates an instance of a SWbemDateTime object,  
  7.     gets the OS install date and converts the date to a more 
  8.     useful format. 
  9. .NOTES 
  10.     File Name  : Get-OSInstallDate.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/aa393687%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh [C:\foo]> .\Get-OsInstallDate.ps1 
  20.     This OS was installed in the year 2008 
  21.     Full installation date (VT_DATE format) is 4/9/2008 8:17:23 PM 
  22.     Full installation date (FILETIME format) is 128522458430000000 
  23.  
  24. #> 
  25.  
  26. # Create swbemdatetime object 
  27. $datetime = New-Object -ComObject WbemScripting.SWbemDateTime 
  28.  
  29. #  Get OS installation time and assign to datetime object 
  30. $os = Get-WmiObject -Class Win32_OperatingSystem 
  31. $dateTime.Value = $os.InstallDate 
  32.  
  33. # Now display the time 
  34. "This OS was installed in the year {0}"           -f $dateTime.Year 
  35. "Full installation date (VT_DATE format) is {0}"  -f $dateTime.GetVarDate() 
  36. "Full installation date (FILETIME format) is {0}" -f $dateTime.GetFileTime()  
  37. ## 

Friday 7 October 2011

Register-Event1.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script registers for a WMI event 
  4. .DESCRIPTION 
  5.     This script used PowerShell to register for then display 
  6.     an event. This script is a re-write on an MSDN Sample. 
  7. .NOTES 
  8.     File Name  : Register-Event1.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN sample posted tot: 
  15.         http://msdn.microsoft.com/en-us/library/aa393013%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     [PSH] C:\FOO> Register-Event1.ps1 
  18.     Waiting for events 
  19.     Log Event Occured 
  20.     EVENT MESSAGE 
  21.     A logon was attempted using explicit credentials. 
  22.  
  23.     Subject: 
  24.     Security ID:        S-1-5-21-2824006062-479960714-4144511058-1105 
  25.     Account Name:       tfl 
  26.     ...  -> Reminder snipped for brevity 
  27.     
  28. #> 
  29.  
  30. # Define event Query 
  31. $query = "SELECT * FROM __InstanceCreationEvent  
  32.           WHERE TargetInstance ISA 'Win32_NTLogEvent' " 
  33.  
  34. # Register for event - also specify an action that 
  35. # displays the log event when the event fires. 
  36. Register-WmiEvent -Source Demo1 -Query $query -Action { 
  37.                 Write-Host "Log Event occured" 
  38.                 $global:myevent = $event 
  39.                 Write-Host "EVENT MESSAGE" 
  40.                 Write-Host $event.SourceEventArgs.NewEvent.TargetInstance.Message} 
  41. # So wait 
  42. "Waiting for events" 
Technorati Tags: ,,,