Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Powershell function

239 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Powershell function

  1. 1. Jason
  2. 2. function <name> (<parameter list>) { <function body> }
  3. 3. function Say-Hello() { Write-Host "Hello World from a function in PowerShell." } > Say-Hello
  4. 4. 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
  5. 5.  $helloFunction | gm –type Property
  6. 6. $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.
  7. 7. $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.
  8. 8. function Write-Sum([int]$first =$(throw “first value required”) , [int]$second = “5”) { $sum = $first + $second Write-Host "Sum:$sum" } Write-Sum 6 9
  9. 9. $name = “Jason" function AssignValueToParam($name) { $name = “Hung" Write-Host "inside function : $name" } AssignValueToParam $name Write-Host "Outside function : $name"
  10. 10. 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"
  11. 11. function Write-Variable([ref]$name) { $name.GetType().FullName #System.Management.Automation.PSReference $name.Value = "Hung" } $name = "Jason" Write-Variable ([ref]$name) $name
  12. 12. function Generate-NumberTo($max) { for($i=0; $i -lt $max; $i++) { $i } } Generate-NumberTo 4
  13. 13. function Find-Object($target, $haystack) { foreach ($item in $haystack) { if($item -eq $target) { return $item } } } Find-Object 5 (2..19)
  14. 14. function OuterFunction() { function InnerFunction() { Write-Host "Printed by InnerFunction!" } InnerFunction } OuterFunction
  15. 15. 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
  16. 16. 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
  17. 17. function Get-FileSize { begin { $total = 0 } process { Write-Host "processing: $($_.name)" $total += $_.Length } end { return $total } } dir *.txt | Get-FileSize
  18. 18. 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 {... ...

×