| « Windows PowerShell 2.0 RTM for WinXP/W2k3/W2k8/Vista | Clean-ProfileSharev2.ps1 » |
Template script to use arguments from the command line [updated 24-12-2009]
Scripts, Admin Automation, Admin analyse Add commentsI had a lot of problems with making a failsafe script that forces the use of 'named' parameters from the commandline and give the posibility to show a help when not used correct. This is my solution and it is possible to use it as a template. See the test below or download the script itself from the attachment to have the proper indents.
Update: The template is updated because the error handling didn't work properly with the arraylist. Also some declarations are moved.
Follow up:
param( [string]$Arg1 = '', [string]$Arg2 = "value2", [string]$Arg3 = '') ## When <argument> = '' then it is a mandatory argument
## Create the default Trap and Error Action Preference
$ErrorActionPreference = "SilentlyContinue"
trap {
Write-Host "Could not continue, an exception was thrown:" -ForeGroundColor "Red"
Write-Host $_.Exception.Message -ForeGroundColor "Yellow"
Add-Content -Path ./$DateRevMin-$LogFile -Value "$(Get-Date -Format `"yyyy-MM-dd HH:mm:ss`"); !!! ERROR EXCEPTION THROWN !!!"
Add-Content -Path ./$DateRevMin-$LogFile -Value "$(Get-Date -Format `"yyyy-MM-dd HH:mm:ss`"); $($_.Exception.Message)"
continue
}
# Retrieves the filename of this script.
$ScriptName = Split-Path $MyInvocation.MyCommand.Definition -leaf
$LogFile = $($ScriptName).replace("ps1","csv")
$DateRevMin = get-date -uformat "%Y-%m-%d"
Function Show-Help
{
Write-Host ""
Write-Host "Script created by Mike Bijl (Login Consultants)" -ForeGroundColor "DarkGreen"
Write-Host "Script version 2.0" -ForeGroundColor "DarkGreen"
Write-Host ""
Write-Host "DEPENDANCY:"
Write-Host " - " -ForeGroundColor "Green"
Write-Host ""
Write-Host "DESCRIPTION:"
Write-Host ""
Write-Host "EXAMPLE:"
Write-Host ".\template.ps1 -Arg1 test1 -Arg2 Value2 -Arg3 Value3"
Write-Host ""
Write-Host "SYNTAX:"
Write-Host ""
Exit
}
## Arrays of all valid arguments (and the possible values)
#$arrArg1 = "" When the array of valid values is not defined and there is no default valie (see line1) it is a mandatory undefined value
If (!$Arg1) { Show-Help } # Check is there is an argument parsed on the commandline.
$arrArg2 = "Value2", "None", "Normal" # This is a optional value but with predefined possibilities.
If (!($arrArg2 -contains $Arg2)){ Show-Help }
$arrArg3 = "Value3", "None", "Full" # This is an example of a mandatory value with predefined options Value3/None/Full
If (!($arrArg3 –contains $Arg3)){ Show-Help }
## This function is the actual script
Function Script
{
## Creates the ArrayList to put the log in. This way there are less write actions in the logfile
$LogBuffer = New-Object System.Collections.ArrayList
Write-Host "-Arg1 = $Arg1"
Write-Host "-Arg2 = $Arg2"
Write-Host "-Arg3 = $Arg3"
#Write log to file if $LogBuffer is not empty
If ($LogBuffer)
{
## Creates a logfile if it does not exist and creates the firstline with the header.
If ( -not (Test-Path -Path ./$DateRevMin-$LogFile)){ New-Item -path . -Name ./$DateRevMin-$LogFile -Type "file" -Value "Header1;Header2;Header3`n" | Out-Null}
Add-Content -Path ./$DateRevMin-$LogFile -Value $LogBuffer
$LogBuffer.Clear() ## If the LogBuffer is not empty then clear the LogBuffer
}
}
Script
Attachments:
2009-12-24 template.ps1 (2.7 KB)
No feedback yet
Comments are closed for this post.