Sunday, 23 September 2012

EchoArgs.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script contains a function, EchoArgs, that returns a list of 
  4.     the arguments passed and their type. It is used to demonstrate 
  5.     the use of the --% operator when calling a function or cmdlet, a new 
  6.     feature in PowerShell v3. 
  7. .DESCRIPTION 
  8.     The EchoArgs function takes the arguments passed, via $args, and
  9.     displays each argument and its type. Then, this function is called
  10.     first with a normal calling sequence and then using --%. 
  11. .NOTES 
  12.     File Name  : EchoArgs.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18.     MSDN sample posted to: 
  19.          http://msdn.microsoft.com/en-us/library. 
  20. .EXAMPLE 
  21.     Psh[Cookham8:C:\foo]>E:\PowerShellScriptLib\SERVER2012FEATURES\EchoArgs.ps1 
  22.     Calling Echoargs with 'fasdf $(LS) 2334 {asdf}' 
  23.     Argument [0]: [fasdf] Type:System.String 
  24.     Argument [1]: [System.Object[]] Type:System.Object[] 
  25.     Argument [2]: [2334] Type:System.Int32 
  26.     Argument [3]: [asdf] Type:System.Management.Automation.ScriptBlock 
  27.  
  28.     Calling Echoargs with '--%  asdf; {asfd}-a asdf' 
  29.     Argument [0]: [--%] Type:System.String 
  30.     Argument [1]: [asdf; $(ls) {asfd} - a asdf] Type:System.String        
  31. #> 
  32.  
  33. # EchoArgs function 
  34. Function EchoArgs { 
  35. #Loop through and display each argument passed 
  36.   For ($i = 0; $i -ilt $Args.count; $i ++) { 
  37.   "Argument [{0}]: [{1}] Type:{2}" -f $I, $args[$i],$($args[$i].GetType().FullName) 
  38.  
  39. # Test it 
  40. "Calling Echoargs with 'fasdf `$(LS) 2334 {asdf}'" 
  41. Echoargs  fasdf $(ls) 2334 {asdf} 
  42. "";"Calling Echoargs with '--%  asdf; {asfd}-a asdf'" 
  43. Echoargs  --%  asdf; $(ls) {asfd} - a asdf 

No comments: