Ken Maglio
Automation Solution Engineer My License Plate says it all "I-AU2M8"
Functions are your Friends
get-command -CommandType function
cd function:
dir function: or ls function:
Find variables that change or prompt users for input; move first
Find all hard-coded variables; move second
Work with variables follows
Output final result
Example
$a = 4
$b = 5
$c = $a + $b
Write-Host $c
Example + User Input
[int]$a = Read-Host -Prompt "A"
[int]$b = Read-Host -Prompt "B"
$message = "Your Result:"
$c = $a + $b
Write-Host $message $c
Convert to 'Basic' Function
function Add([int]$a, [int]$b) {
$message = "Your Result:"
$c = $a + $b
Write-Host $message $c
}
[int]$a = Read-Host -Prompt "A"
[int]$b = Read-Host -Prompt "B"
$message = "Your Result:"
$c = $a + $b
Write-Host $message $c
But nothing happens!!!
function Add([int]$a, [int]$b) {
$message = "Your Result:"
$c = $a + $b
Write-Host $message $c
}
[int]$a = Read-Host -prompt "A"
[int]$b = Read-Host -prompt "B"
Add $a $b
By Ken Maglio
Functions are your Friends
Automation Solution Engineer My License Plate says it all "I-AU2M8"