Advertisement
1RedOne

Comparison of Advanced v. Basic Function in PowerShell

May 6th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #writing out the declaration of $names to the screen
  2. "`$names = `'hamster`'`, `'dance`' "
  3. $names = 'hamster', 'dance'
  4.  
  5. function print-Variable {
  6. Param(
  7.               [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
  8.               [string[]] $Name
  9.           )
  10. "Output from Basic Function`n`t--hello $Name"
  11. "Note that though `$names contains two objects, the main body of the script process only executes one time"
  12. }
  13.  
  14.  
  15. $names | print-Variable
  16.  
  17.  
  18. function AdvancedPrint-Variable {
  19. Param(
  20.               [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
  21.               [string[]] $Name
  22.           )
  23. begin  {#Stuff we want to happen once before our process executes goes here
  24.         Write-host -ForegroundColor Yellow "Beginning advanced function"
  25.        }
  26. process{#Stuff we want to happen for every object in the pipeline goes here
  27.         "Output from Advanced Function`n`t--hello $Name"}
  28. end    {#cleanup operations and stuff we want to happen at the end of our process goes here
  29.         Write-host -ForegroundColor Yellow "Ending advanced function"
  30.         "Now, note that because we have a process block, PowerShell will enumerate our objects for us, and run the main process block once for each object.  Also note that the Write-Host commands in the begin and end blocks are only displayed once, even though the process block executes twice (once for each object)"}
  31.        }
  32.  
  33. $names| AdvancedPrint-Variable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement