jan-h
10/13/2015 - 1:50 PM

Hello World in Powershell, with verbose logging and error handling.

Hello World in Powershell, with verbose logging and error handling.

# Usage examples
#   .\hello-world.ps1 "Gert"
#   .\hello-world.ps1 -Name "Gert"
#   .\hello-world.ps1 "Gert" -Greetings "Hey"
#   .\hello-world.ps1 "Gert" -Greetings "Hey" -Verbose
#   .\hello-world.ps1 -Name "Jan"

param (
    [parameter(Mandatory=$true)]
    [string] $Name,

    [string] $Greetings = "Hello"
)

$ErrorActionPreference = "Stop"

Write-Verbose "Using name $Name"

$message = "$Greetings, $Name";

$stats = $message | measure -Character
Write-Verbose ("The message is {0} characters long" -f $stats.Characters)

if($Name -eq "Jan") 
{
    $errorMessage = ("For demo purposes you cannot greet {0}" -f $Name)
    Write-Error $errorMessage
}

Write-Verbose "greeting the user..."
Write-Output $message