• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Powershell function
 

Powershell function

on

  • 154 views

 

Statistics

Views

Total Views
154
Views on SlideShare
154
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Powershell function Powershell function Presentation Transcript

    • Jason
    • function <name> (<parameter list>) { <function body> }
    • function Say-Hello() { Write-Host "Hello World from a function in PowerShell." } > Say-Hello
    • function 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
    •  $helloFunction | gm –type Property
    • $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.
    • $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 Write-Sum([int]$first =$(throw “first value required”) , [int]$second = “5”) { $sum = $first + $second Write-Host "Sum:$sum" } Write-Sum 6 9
    • $name = “Jason" function AssignValueToParam($name) { $name = “Hung" Write-Host "inside function : $name" } AssignValueToParam $name Write-Host "Outside function : $name"
    • function 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"
    • function Write-Variable([ref]$name) { $name.GetType().FullName #System.Management.Automation.PSReference $name.Value = "Hung" } $name = "Jason" Write-Variable ([ref]$name) $name
    • function Generate-NumberTo($max) { for($i=0; $i -lt $max; $i++) { $i } } Generate-NumberTo 4
    • function Find-Object($target, $haystack) { foreach ($item in $haystack) { if($item -eq $target) { return $item } } } Find-Object 5 (2..19)
    • function 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
    • function 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
    • function Get-FileSize { begin { $total = 0 } process { Write-Host "processing: $($_.name)" $total += $_.Length } end { return $total } } dir *.txt | Get-FileSize
    • filter Even-Filter { if ($_ %2 -eq 0) { $_ } } function Even-Function { process { if ($_ % 2 -eq 0) { $_ } } } Get-Command Even-Function -CommandType Function Get-Command Even-Filter -CommandType Function CommandType ----------Function Filter Name ---Even-Function Even-Filter Definition ---------process {... ...