- <#
- .SYNOPSIS
- This script, a re-implementation of an MSDN sample, shows
- details of a timespan object
- .DESCRIPTION
- This script re-implements a simple MSDN script that creates a
- timespan object and displays its properties.
- .NOTES
- File Name : Show-TimeSpan.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.timespan.aspx
- .EXAMPLE
- Psh> .\Show-TimeSpan.ps1
- 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
- Value of Days Component: 229
- Total Number of Days: 229.229340277778
- Value of Hours Component: 5
- Total Number of Hours: 5501.50416666667
- Value of Minutes Component: 30
- Total Number of Minutes: 330090.25
- Value of Seconds Component: 15
- Total Number of Seconds: 19,805,415
- Value of Milliseconds Component: 0
- Total Number of Milliseconds: 19,805,415,000
- Ticks: 198,054,150,000,000
- #>
- # Define two dates
- $date1 = new-object system.datetime 2010, 1, 1, 8, 0, 15
- $date2 = new-object system.datetime 2010, 8, 18, 13, 30, 30
- # Create a time Interval
- $interval = New-Timespan -start $date1 -end $date2
- #Display the interval
- "{0} - {1} = {2}" -f $date2, $date1, $interval.ToString()
- # Display individual properties of the resulting TimeSpan object.
- " {0,-35} {1,20}" -f "Value of Days Component:", $interval.Days
- " {0,-35} {1,20}" -f "Total Number of Days:", $interval.TotalDays
- " {0,-35} {1,20}" -f "Value of Hours Component:", $interval.Hours
- " {0,-35} {1,20}" -f "Total Number of Hours:", $interval.TotalHours
- " {0,-35} {1,20}" -f "Value of Minutes Component:", $interval.Minutes
- " {0,-35} {1,20}" -f "Total Number of Minutes:", $interval.TotalMinutes
- " {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $interval.Seconds
- " {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $interval.TotalSeconds
- " {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $interval.Milliseconds
- " {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $interval.TotalMilliseconds
- " {0,-35} {1,20:N0}" -f "Ticks:", $interval.Ticks
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Tuesday, 8 November 2011
Show-TimeSpan.ps1
Labels:
Power,
PowerShell scripts,
system.datetime,
System.TImespan
Show-UnicodeCharacters.ps1
- <#
- .SYNOPSIS
- This script, a re-implementation of an MSDN sample, shows the
- Unicode details of a unicode character, using PowerShell.
- .DESCRIPTION
- This script re-implements a simple MSDN script that takes a Unicode Character
- and uses CharUnicodeInfo class to get details of that character, which are then
- displayed.
- .NOTES
- File Name : Show-UnicodeCharacters.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/11/show-unicodecharactersps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.charunicodeinfo.aspx
- .EXAMPLE
- Psh > .\show-unicodecharacters.ps1
- c Num Dig Dec UnicodeCategory
- U+0061 LATIN SMALL LETTER A a -1 -1 -1 LowercaseLetter
- U+0393 GREEK CAPITAL LETTER GAMMA Γ -1 -1 -1 UppercaseLetter
- U+0039 DIGIT NINE 9 9 9 9 DecimalDigitNumber
- U+00B2 SUPERSCRIPT TWO ² 2 2 -1 OtherNumber
- U+00BC VULGAR FRACTION ONE QUARTER ¼ 0.25 -1 -1 OtherNumber
- U+0BEF TAMIL DIGIT NINE ௯ 9 9 9 DecimalDigitNumber
- U+0BF0 TAMIL NUMBER TEN ௰ 10 -1 -1 OtherNumber
- U+0F33 TIBETAN DIGIT HALF ZERO ༳ -0.5 -1 -1 OtherNumber
- U+2788 CIRCLED SANS-SERIF DIGIT NINE ➈ 9 9 -1 OtherNumber
- #>
- # Helper Function
- Function PrintProperties {
- param ($char)
- $fmtstring = " {0,-5} {1,-8} {2,-9} {3,-9} {4,-9}"
- $a = $char
- $b = [System.Globalization.CharUnicodeInfo]::GetNumericValue( $char )
- $c = [System.Globalization.CharUnicodeInfo]::GetDigitValue( $char )
- $d = [System.Globalization.CharUnicodeInfo]::GetDecimalDigitValue( $char )
- $e = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory( $char )
- $fmtstring -f $a, $b, $c, $d, $e
- }
- " c Num Dig Dec UnicodeCategory"
- "U+0061 LATIN SMALL LETTER A " + (PrintProperties "a")
- "U+0393 GREEK CAPITAL LETTER GAMMA " + (PrintProperties ([Char] 0x0393) )
- "U+0039 DIGIT NINE " + (PrintProperties "9")
- "U+00B2 SUPERSCRIPT TWO " + (PrintProperties $([Char] 0x00B2) )
- "U+00BC VULGAR FRACTION ONE QUARTER " + (PrintProperties $([Char] 0x00BC) )
- "U+0BEF TAMIL DIGIT NINE " + (PrintProperties $([Char] 0x0BEF) )
- "U+0BF0 TAMIL NUMBER TEN " + (PrintProperties $([Char] 0x0BF0) )
- "U+0F33 TIBETAN DIGIT HALF ZERO " + (PrintProperties $([Char] 0x0F33) )
- "U+2788 CIRCLED SANS-SERIF DIGIT NINE " + (PrintProperties $([Char] 0x2788) )
Tuesday, 1 November 2011
Show-DateTimeFormatInfo.ps1
- <#
- .SYNOPSIS
- This script re-implements and MSDN sample that shows
- the different Date/Time Formatting characters and
- how they are used in formatting date/time objects.
- .DESCRIPTION
- This script Creates a date/time object, then shows formatting
- using the key Date/Time Format strings.
- .NOTES
- File Name : Show-DateTimeFormatInfo.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
- .EXAMPLE
- Left as an exercise for the reader.
- #>
- # Create and initialise a DateTimeFormatInfo associated with the en-US culture.
- $MyDTFI = (new-object System.Globalization.CultureInfo "en-US", $false ).DateTimeFormat
- # Create a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3)
- # The Gregorian calendar is the default calendar for the en-US culture
- $MyDT = new-object System.DateTime 2002, 1, 3
- # Display the format pattern associated with each format character
- "FORMAT en-US EXAMPLE"
- "CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY"
- " d {0}" -f $MyDT.ToString("d", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.ShortDatePattern, "(ShortDatePattern)"
- " D {0}" -f $MyDT.ToString("D", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.LongDatePattern, "(LongDatePattern)"
- " f {0}`n" -f $MyDT.ToString("f", $MyDTFI)
- " F {0}" -f $MyDT.ToString("F", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.FullDateTimePattern, "(FullDateTimePattern)"
- " g {0}`n" -f $MyDT.ToString("g", $MyDTFI)
- " G {0}`n" -f $MyDT.ToString("G", $MyDTFI)
- " m {0}" -f $MyDT.ToString("m", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.MonthDayPattern, "(MonthDayPattern)"
- " M {0}" -f $MyDT.ToString("M", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.MonthDayPattern, "(MonthDayPattern)"
- " o {0}`n" -f $MyDT.ToString("o", $MyDTFI)
- " r {0}" -f $MyDT.ToString("r", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.RFC1123Pattern, "(RFC1123Pattern)"
- " R {0}" -f $MyDT.ToString("R", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.RFC1123Pattern, "(RFC1123Pattern)"
- " s {0}" -f $MyDT.ToString("s", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.SortableDateTimePattern, "(SortableDateTimePattern)"
- " t {0}" -f $MyDT.ToString("t", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.ShortTimePattern, "(ShortTimePattern)"
- " T {0}" -f $MyDT.ToString("T", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.LongTimePattern, "(LongTimePattern)"
- " u {0}" -f $MyDT.ToString("u", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)"
- " U {0}`n" -f $MyDT.ToString("U", $MyDTFI)
- " y {0}" -f $MyDT.ToString("y", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.YearMonthPattern, "(YearMonthPattern)"
- " Y {0}" -f $MyDT.ToString("Y", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.YearMonthPattern, "(YearMonthPattern)"
Show-CurrencyDecimalSeparator.ps1
- <#
- .SYNOPSIS
- This script re-implements an MSDN script which
- shows the use of the CurrencyDecimalSeparator
- property of a numeric format info object.
- .DESCRIPTION
- This script displays a currency value using
- the default decimal separator then shows using a
- custom separator. This is repeated using a German
- culture.
- .NOTES
- File Name : Show-CurrencyDecimalSeparator.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencydecimalseparator.aspx
- .EXAMPLE
- Psh> .\Show-CurrencyDecimalSeparator.ps1
- $123,456,789.00
- $123,456,789 00
- 123.456.789,00 €
- 123.456.789 00 €
- #>
- # Get a NumberFormatInfo associated with the en-US culture
- $nfi = (new-object System.Globalization.CultureInfo "en-US", $false ).NumberFormat
- # Display a value with the default separator (".").
- $myInt = 123456789
- $myInt.ToString( "C", $nfi )
- # Display the same value with a blank as the separator.
- $nfi.CurrencyDecimalSeparator = " "
- $myInt.ToString( "C", $nfi )
- # Now get a NumberFormatInfo associated with the de-DE culture
- $nfi = (new-object System.Globalization.CultureInfo "de-DE", $false ).NumberFormat
- # Display a value with the default separator (".")
- $myInt = 123456789
- $myInt.ToString( "C", $nfi )
- # Display the same value with a blank as the separator
- $nfi.CurrencyDecimalSeparator = " "
- $myInt.ToString( "C", $nfi )
Technorati Tags: PowerShell,script,formatting,System.Globalization.Cultureinfo,CurrencyDecimalSeparator
Monday, 31 October 2011
Show-CurrencyFormatting.ps1
- <#
- .SYNOPSIS
- This script re-implements an MSDN Sample showing the
- use of the NumberFormatInfo class to nicely format things
- in this case, currency.
- .DESCRIPTION
- This script iterates through the Windows cultures and
- displays those whose 2-letter ISO code is 'en' and
- displays how Windows formats currency in that culture.
- .NOTES
- File Name : Show-CurrencyFormatting.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
- .EXAMPLE
- Psh > .\Show-CurrencyFormatting.ps1
- The currency symbol for 'English (United States)' is '$'
- The currency symbol for 'English (United Kingdom)' is '£'
- The currency symbol for 'English (Australia)' is '$'
- The currency symbol for 'English (Canada)' is '$'
- The currency symbol for 'English (New Zealand)' is '$'
- The currency symbol for 'English (Ireland)' is '€'
- The currency symbol for 'English (South Africa)' is 'R'
- The currency symbol for 'English (Jamaica)' is 'J$'
- The currency symbol for 'English (Caribbean)' is '$'
- The currency symbol for 'English (Belize)' is 'BZ$'
- The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
- The currency symbol for 'English (Zimbabwe)' is 'Z$'
- The currency symbol for 'English (Republic of the Philippines)' is 'Php'
- The currency symbol for 'English (Singapore)' is '$'
- The currency symbol for 'English (Malaysia)' is 'RM'
- The currency symbol for 'English (India)' is 'Rs.'
- #>
- # Loop through all the specific cultures known to the CLR.
- foreach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))
- {
- # Only show the currency symbols for cultures that speak English.
- if ($ci.TwoLetterISOLanguageName -eq "en") {
- # Display the culture name and currency symbol.
- $nfi = $ci.NumberFormat
- "The currency symbol for '{0}' is '{1}'" -f $ci.DisplayName, $nfi.CurrencySymbol
- }
- }
Show-CalendarAlgorithm.ps1
- <#
- .SYNOPSIS
- This script re-implements an MSDN Sample showing the
- CalendarAlgorithmType enumeration.
- .DESCRIPTION
- This script creates there calendars and displays
- algorithm type,
- .NOTES
- File Name : Show-CalendarAlgorithm.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.calendaralgorithmtype.aspx
- .EXAMPLE
- #>
- # Helper function
- Function Display{
- Param ([System.Globalization.Calendar] $c)
- $name = $c.ToString().PadRight(50, '.')
- "{0} {1}" -f $name, $c.AlgorithmType
- }
- ## Start of script
- # Create three new calendars
- $grCal = new-object System.Globalization.GregorianCalendar
- $hiCal = new-object System.Globalization.HijriCalendar
- $jaCal = new-object System.Globalization.JapaneseLunisolarCalendar
- # Display them
- Display($grCal);
- Display($hiCal);
- Display($jaCal);
- [enum]::GetNames([System.Globalization.CalendarAlgorithmType])
Show-CurrencyGroupSize
- <#
- .SYNOPSIS
- This script reimplements a code sample from MSDN in PowerShell.
- This sample formats and display currency using standard and
- different currency groupings.
- .DESCRIPTION
- This script displays a currency using standard, then two
- custom CurrencyGroupSizes.
- .NOTES
- File Name : Show-CurrencyGroupSize.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx
- .EXAMPLE
- PSH:> .\Show-CurrencyGroupSize.ps1
- Default numeric format string "C"
- $123,456,789,012,345.00
- Display with array = 2,3,4
- $12,3456,7890,123,45.00
- Display with array = 2,3,0
- $1234567890,123,45.00
- #>
- # Get a NumberFormatInfo associated with the en-US culture.
- $fi = new-object System.Globalization.CultureInfo "en-US", false
- $nfi = $fi.NumberFormat
- # Display a value with the default separator (".")
- "Default numeric format string `"C`""
- [Int64] $myInt = 123456789012345
- $myInt.ToString( "C", $nfi )
- # Display the same value with different groupings.
- [int[]] $mySizes1 = (2,3,4)
- $mySizes = 2,3,0
- "";"Display with array = 2,3,4"
- $nfi.CurrencyGroupSizes = $mySizes1
- $myInt.ToString( "C", $nfi )
- "";"Display with array = 2,3,0"
- $nfi.CurrencyGroupSizes = $mySizes2
- $myInt.ToString( "C", $nfi )
Show-StandardNFS-1.ps1
- <#
- .SYNOPSIS
- This script shows the first two samples on this MSDN Page,
- convered to PowerShell
- .DESCRIPTION
- This script displays two lines of text using PowerShell
- and .NET formatting information
- .NOTES
- File Name : Show-StandardNFS-1.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
- .EXAMPLE
- [Cookham8:C:\foo]> .\Show-StandardNFS.ps1
- $123.46
- Your account balance is $123.46.
- #>
- # Show formatting a number as currency
- $value = 123.456
- $value.ToString("C2")
- # Show the -f operator with currency formatting
- $value = 123.456
- "Your account balance is {0:C2}." -f $value
Labels:
code,
powershell,
PowerShell scripts,
ToString()
Tuesday, 11 October 2011
Get-OSInstallDate.ps1
- <#
- .SYNOPSIS
- This script gets/displays the Sysgtem Uptime after
- converting it from WEBM time/date format
- .DESCRIPTION
- This script creates an instance of a SWbemDateTime object,
- gets the OS install date and converts the date to a more
- useful format.
- .NOTES
- File Name : Get-OSInstallDate.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa393687%28VS.85%29.aspx
- .EXAMPLE
- Psh [C:\foo]> .\Get-OsInstallDate.ps1
- This OS was installed in the year 2008
- Full installation date (VT_DATE format) is 4/9/2008 8:17:23 PM
- Full installation date (FILETIME format) is 128522458430000000
- #>
- # Create swbemdatetime object
- $datetime = New-Object -ComObject WbemScripting.SWbemDateTime
- # Get OS installation time and assign to datetime object
- $os = Get-WmiObject -Class Win32_OperatingSystem
- $dateTime.Value = $os.InstallDate
- # Now display the time
- "This OS was installed in the year {0}" -f $dateTime.Year
- "Full installation date (VT_DATE format) is {0}" -f $dateTime.GetVarDate()
- "Full installation date (FILETIME format) is {0}" -f $dateTime.GetFileTime()
- ##
Labels:
powershell,
PowerShell scripts,
SWbemDateTime,
wmi
Friday, 7 October 2011
Register-Event1.ps1
- <#
- .SYNOPSIS
- This script registers for a WMI event
- .DESCRIPTION
- This script used PowerShell to register for then display
- an event. This script is a re-write on an MSDN Sample.
- .NOTES
- File Name : Register-Event1.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/aa393013%28VS.85%29.aspx
- .EXAMPLE
- [PSH] C:\FOO> Register-Event1.ps1
- Waiting for events
- Log Event Occured
- EVENT MESSAGE
- A logon was attempted using explicit credentials.
- Subject:
- Security ID: S-1-5-21-2824006062-479960714-4144511058-1105
- Account Name: tfl
- ... -> Reminder snipped for brevity
- #>
- # Define event Query
- $query = "SELECT * FROM __InstanceCreationEvent
- WHERE TargetInstance ISA 'Win32_NTLogEvent' "
- # Register for event - also specify an action that
- # displays the log event when the event fires.
- Register-WmiEvent -Source Demo1 -Query $query -Action {
- Write-Host "Log Event occured"
- $global:myevent = $event
- Write-Host "EVENT MESSAGE"
- Write-Host $event.SourceEventArgs.NewEvent.TargetInstance.Message}
- # So wait
- "Waiting for events"
Labels:
powershell,
PowerShell scripts,
wmi
Subscribe to:
Posts (Atom)