Thursday, 23 January 2014

Show-Message.ps1

<#
.SYNOPSIS
This script creates a function to display a message
in a message block, then demonstrates its usage
.DESCRIPTION
This script used Windows Forms to put up a message
box containing text and a window title passed as
parameters
.NOTES
File Name : Show-Message.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 3.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
.EXAMPLE
Left as an exercise to the Reader
#>

Function Show-Message {

[CmdletBinding()]
Param (
[Parameter(Mandatory=$True,
HelpMessage="Content of Message box")]
[string]$Message ,

[Parameter(Mandatory=$False,
HelpMessage="Title for Message box")]
[string]$BoxTitle = "Message"
)

# just in case, load the relevant assembly
$v1 = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

# now use the messagebox class to display the message
[Windows.Forms.MessageBox]::Show($Message, $BoxTitle,
[Windows.Forms.MessageBoxButtons]::OK ,
[Windows.Forms.MessageBoxIcon]::Information)

} # End of function

# Set an alias
Set-Alias sm Show-Message

# call the function
sm 'testing' 'details, details'

Sunday, 5 January 2014

Show-TimeSpanFormatting.ps1

<# 
.SYNOPSIS
This script demonstrates formatting System.TimeSpan objects
Using PowerShell
.DESCRIPTION
This scipt re-writes some MSDN Samples that demostarate
timespan formatting - the original article lacks PowerShell
Examples. And sadly, the MSDN page no longer accepts community
additions.

.NOTES
File Name : Show-TimeSpanFormatting
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 3.0
.LINK
This script posted to:
http://pshscripts.blogspot.co.uk/2014/01/show-timespanformattingps1.html
Reference MSDN Page
http://msdn.microsoft.com/en-us/library/ee372286%28v=vs.110%29.aspx
.EXAMPLE
Psh> .\Show-TimeSpanFormatting.ps1
Example 1
Time of Travel: 1.12:24:02
Time of Travel: 1.12:24:02
Example 2
Converted '1.03:14:56.1667' to 1.03:14:56.1667000
Converted '1.03:14:56.1667' to 1.03:14:56.1667000
Example 3
07:45:16 - 18:12:38 = -10:27:22
07:45:16 + 18:12:38 = 1.01:57:54
00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756
Example 4
7:45:16 - 18:12:38 = -10:27:22
7:45:16 + 18:12:38 = 1:1:57:54
0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756
Example 5
0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000
0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000
0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756
#>

# Show Time Span Format Strings

# Example 1 - use both tostring() and -f operators
"Example 1"
# Create Timespan object
$duration = New-Object System.TimeSpan 1, 12, 23, 62

# Now output using both tostring() and -f
"Time of Travel: " + $duration.ToString("c")
"Time of Travel: {0:c}" -f $duration

# Example 2
# Demonstrate the use of ParseExact and TryParseExact
"Example 2"

$value = "1.03:14:56.1667"
$interval = New-Object System.TimeSpan

Try {
$interval = [System.TimeSpan]::ParseExact($value, "c", $null)
"Converted '{0}' to {1}" -f $value, $interval
}
Catch [System.FormatException] {"{0}: Bad Format" -f $value}
Catch [System.OverflowException] {"{0}: Out of Range" -f $value }

If ([System.TimeSpan]::TryParseExact($value, "c", $null, [ref] $interval)) {
"Converted '{0}' to {1}" -f $value, $interval
}
Else {
"Unable to convert {0} to a time interval." -f $value
}

# Example 3
# Create two TimeSpan objects, perform arithmetic operations
# on them then displays the result using the 'C' format specifier
"Example 3"

$interval1 = New-Object System.TimeSpan 7, 45, 16
$interval2 = New-Object System.TimeSpan 18, 12, 38

"{0:c} - {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 - $interval2)
"{0:c} + {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 + $interval2)

$interval1 = New-Object System.TimeSpan 0, 0, 1, 14, 365
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:c} + {1:c} = {2:c}" -f $interval1, $interval2, $($interval1 + $interval2)

# Example 4 - The "g" Format Specifier
# This specifier returns the string representation of a TimeSpan value in a compact form
# by including only the elements that are necessary.
"Example 4"

$interval1 = New-Object System.TimeSpan 7, 45, 16
$interval2 = New-Object System.TimeSpan 18, 12, 38

"{0:g} - {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 - $interval2)

# do it in French
$CI = New-object System.Globalization.CultureInfo 'fr-FR'
[System.String]::Format($CI, $("{0:g} + {1:g} = {2:g}") ,
$interval1,$interval2,$($interval1 + $interval2) )
# Another interval
$interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:g} + {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 + $interval2)

# Example 5
# Using the "G' Specifier
#
"Example 5"
$interval1 = new-object System.TimeSpan 7, 45, 16
$interval2 = new-object System.TimeSpan 18, 12, 38
"{0:G} - {1:G} = {2:G}" -f $interval1, $interval2, $($interval1 -$interval2)
[System.String]::Format($(New-Object CultureInfo("fr-FR")),
"{0:G} + {1:G} = {2:G}", $interval1,
$interval2, $($interval1 + $interval2))

$interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:G} + {1:G} = {2:G}" -f $interval1,$interval2, $($interval1 + $interval2)