PoshCode Logo PowerShell Code Repository

Invoke-NamedParameter by JasonMArcher 10 months ago (modification of post by JasonMArcher view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2608"></script>download | new post

A function that simplifies calling methods with named parameters to make it easier to deal with long signatures and optional parameters. This is particularly helpful for COM objects.

  1. Function Invoke-NamedParameter {
  2. <#
  3. .SYNOPSIS
  4.     Invokes a method using named parameters.
  5. .DESCRIPTION
  6.     A function that simplifies calling methods with named parameters to make it easier to deal with long signatures and optional parameters.  This is particularly helpful for COM objects.
  7. .PARAMETER Object
  8.     The object that has the method.
  9. .PARAMETER Method
  10.     The name of the method.
  11. .PARAMETER Parameter
  12.     A hashtable with the names and values of parameters to use in the method call.
  13. .PARAMETER Argument
  14.     An array of arguments without names to use in the method call.
  15. .NOTES
  16.     Name: Invoke-NamedParameter
  17.     Author: Jason Archer
  18.     DateCreated: 2011-04-06
  19.  
  20.     1.0 - 2011-04-06 - Jason Archer
  21.         Initial release.
  22. .EXAMPLE
  23.     $shell = New-Object -ComObject Shell.Application
  24.     Invoke-NamedParameter $Shell "Explore" @{"vDir"="$pwd"}
  25.  
  26.     Description
  27.     -----------    
  28.     Invokes a method named "Explore" with the named parameter "vDir."
  29. #>
  30.     [CmdletBinding(DefaultParameterSetName = "Named")]
  31.     param(
  32.         [Parameter(ParameterSetName = "Named", Position = 0, Mandatory = $true)]
  33.         [Parameter(ParameterSetName = "Positional", Position = 0, Mandatory = $true)]
  34.         [ValidateNotNull()]
  35.         [System.Object]$Object
  36.         ,
  37.         [Parameter(ParameterSetName = "Named", Position = 1, Mandatory = $true)]
  38.         [Parameter(ParameterSetName = "Positional", Position = 1, Mandatory = $true)]
  39.         [ValidateNotNullOrEmpty()]
  40.         [String]$Method
  41.         ,
  42.         [Parameter(ParameterSetName = "Named", Position = 2, Mandatory = $true)]
  43.         [ValidateNotNull()]
  44.         [Hashtable]$Parameter
  45.         ,
  46.         [Parameter(ParameterSetName = "Positional")]
  47.         [Object[]]$Argument
  48.     )
  49.  
  50.     end {  ## Just being explicit that this does not support pipelines
  51.         if ($PSCmdlet.ParameterSetName -eq "Named") {
  52.             ## Invoke method with parameter names
  53.             ## Note: It is ok to use a hashtable here because the keys (parameter names) and values (args)
  54.             ## will be output in the same order.  We don't need to worry about the order so long as
  55.             ## all parameters have names
  56.             $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
  57.                 $null,  ## Binder
  58.                 $Object,  ## Target
  59.                 ([Object[]]($Parameter.Values)),  ## Args
  60.                 $null,  ## Modifiers
  61.                 $null,  ## Culture
  62.                 ([String[]]($Parameter.Keys))  ## NamedParameters
  63.             )
  64.         } else {
  65.             ## Invoke method without parameter names
  66.             $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
  67.                 $null,  ## Binder
  68.                 $Object,  ## Target
  69.                 $Argument,  ## Args
  70.                 $null,  ## Modifiers
  71.                 $null,  ## Culture
  72.                 $null  ## NamedParameters
  73.             )
  74.         }
  75.     }
  76. }
  77.  
  78. <#
  79. Examples
  80.  
  81. Calling a method with named parameters.
  82.  
  83. $shell = New-Object -ComObject Shell.Application
  84. Invoke-NamedParameters $Shell "Explore" @{"vDir"="$pwd"}
  85.  
  86. The syntax for more than one would be @{"First"="foo";"Second"="bar"}
  87.  
  88. Calling a method that takes no parameters (you can also use -Argument with $null).
  89.  
  90. $shell = New-Object -ComObject Shell.Application
  91. Invoke-NamedParameters $Shell "MinimizeAll" @{}
  92. #>

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me