templates

This is a template for a powershell script:

#
#
# template for powershell
#
# copied from blog.alloon.net
#

#
# Parameters
#

Param(
[int]$p1,
[string]$p2
)

#Shell-Version
$ShellVersion = $psversiontable.psversion

# Date
$CurrentDateTime = get-date -Format "MMM dd yyyy HH:mm:ss:fff"

# Skriptname
$BaseName = (split-path $MyInvocation.MyCommand -leaf).split(".")[0]

# Logfilename
$LogFileName = "$BaseName.log"

# function
function Add-Together([string]$text,[int]$number1,[int]$number2)
{
Write-Host $text ($sum = $number1 + $number2)
}

# For-Loop
for ($i=1; $i -le 5; $i++)
{
Write-Host $i
}

# While-Loop
$i = 1

while ($i -le 5) {
Write-Host $i
$i++
}

# If with string comparision
if ($OS.Version -eq "5.1.2600") {

}else{

}

# Switch
$a = 2

switch ($a)
{
1 {"The color is red."}
2 {"The color is blue."}
3 {"The color is green."}
default {"The color could not be determined."}
}

# Write to File
$stream = [System.IO.StreamWriter] "c:\temp\$LogFileName"
$stream.WriteLine($CurrentDateTime)
$stream.WriteLine($ShellVersion)
$stream.WriteLine($BaseName)
$stream.close()
$stream.dispose()

# read from File
$stream = new-object System.IO.StreamReader("c:\temp\$LogFileName")

while( ($line = $stream.ReadLine()) -ne $null){
Write-Host $line
}
$stream.close()
$stream.dispose()

# Write to Console
Write-Host "Test"

# Read from Console
Read-Host Input$

# start external program
cmd /c dir

# Exit
exit