20
Powershell EXECUTE FUNCTION Jason

Powershell function

Embed Size (px)

Citation preview

Powershell EXECUTE FUNCTION

Jason

Powershell-Functionfunction <name> (<parameter list>)

{

<function body>

}

Example function Say-Hello()

{

Write-Host "Hello World from a function in PowerShell."

}

> Say-Hello

FunctionInfofunction Say-Hello

{

Write-Host "Hello World from a function in PowerShell."

}

$helloFunction = Get-Command Say-Hello -type Function

$helloFunction.GetType().FullName

>System.Management.Automation.FunctionInfo

FunctionInfo $helloFunction | gm –type Property

FunctionInfo - ScriptBlock$helloFunction.ScriptBlock

> Write-Host "Hello World from a function in PowerShell."

$helloFunction.ScriptBlock.GetType().FullName

> System.Management.Automation.ScriptBlock

& $helloFunction.ScriptBlock

>Hello World from a function in PowerShell.

FunctionInfo - Definition$helloFunction. Definition

> Write-Host "Hello World from a function in PowerShell."

$helloFunction. Definition.GetType().FullName

> System.String

Invoke-Expression $helloFunction. Definition

>Hello World from a function in PowerShell.

Function Parameterfunction Write-Sum([int]$first =$(throw “first value required”) , [int]$second = “5”)

{

$sum = $first + $second

Write-Host "Sum:$sum"

}

Write-Sum 6 9

Assign value to param$name = “Jason"

function AssignValueToParam($name)

{

$name = “Hung"

Write-Host "inside function : $name"

}

AssignValueToParam $name

Write-Host "Outside function : $name"

Assign value to param - Get-Variablefunction Write-Variable($name)

{

$v1 = Get-Variable $name -Scope 0 -ValueOnly

$v2 = Get-Variable $name -Scope 1 -ValueOnly

Write-Host "$v1 $v2"

}

$name = "Jason"

Write-Variable "name"

Assign value to param - PSReferencefunction Write-Variable([ref]$name)

{

$name.GetType().FullName

#System.Management.Automation.PSReference

$name.Value = "Hung"

}

$name = "Jason"

Write-Variable ([ref]$name)

$name

Return Valuefunction Generate-NumberTo($max)

{

for($i=0; $i -lt $max; $i++)

{

$i

}

}

Generate-NumberTo 4

Return Valuefunction Find-Object($target, $haystack){ foreach ($item in $haystack) { if($item -eq $target) { return $item } }}

Find-Object 5 (2..19)

Scope Rulefunction OuterFunction(){ function InnerFunction() { Write-Host "Printed by InnerFunction!" } InnerFunction}

OuterFunction

function OuterFunction(){ function Do-Something() { Write-Host "Original Do-Something function" }

function InnerFunction() { function Do-Something() { Write-Host "Overriden Do-Something function" } Do-Something } InnerFunction Do-Something}

OuterFunction

Scope Rulefunction global:Do-Something(){ Write-Host "Global Do-Something"}function InnerScope(){ function local:Do-Something() { Write-Host "Local Do-Something" } local:Do-Something global:Do-Something}

InnerScope

Filterfunction Get-FileSize{ begin { $total = 0 } process { Write-Host "processing: $($_.name)" $total += $_.Length } end { return $total }}

dir *.txt | Get-FileSize

Filterfilter Even-Filter{ if ($_ %2 -eq 0) { $_ }}

function Even-Function{ process {

if ($_ % 2 -eq 0) { $_ }

}}

Get-Command Even-Function -CommandType FunctionGet-Command Even-Filter -CommandType Function

CommandType Name Definition ----------- ---- ---------- Function Even-Function process {... Filter Even-Filter ...

Q & A

REFERENCE