PoshCode Archive  Artifact [5b3dd24f31]

Artifact 5b3dd24f31d57f11c7e8e6fe27ea62bd5be22bce14e519787eff5f58cb7e7df8:

  • File Invoke-Member.ps1 — part of check-in [3dfba10b0a] at 2018-06-10 13:06:36 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 2086)

# encoding: ascii
# api: powershell
# title: Invoke-Member.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2183
# x-archived: 2016-03-18T21:55:29
# x-published: 2011-09-09T21:41:00
#
#
##############################################################################
##
## Invoke-Member
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Enables easy access to methods and properties of pipeline objects.

.EXAMPLE

PS >"Hello","World" | .\Invoke-Member Length
5
5

.EXAMPLE

PS >"Hello","World" | .\Invoke-Member -m ToUpper
HELLO
WORLD

.EXAMPLE

PS >"Hello","World" | .\Invoke-Member Replace l w
Hewwo
Worwd

#>

[CmdletBinding(DefaultParameterSetName= "Member")]
param(

    ## A switch parameter to identify the requested member as a method.
    ## Only required for methods that take no arguments.
    [Parameter(ParameterSetName = "Method")]
    [Alias("M","Me")]
    [switch] $Method,

    ## The name of the member to retrieve
    [Parameter(ParameterSetName = "Method", Position = 0)]
    [Parameter(ParameterSetName = "Member", Position = 0)]
    [string] $Member,

    ## Arguments for the method, if any
    [Parameter(
        ParameterSetName = "Method", Position = 1,
        Mandatory = $false, ValueFromRemainingArguments = $true)]
    [object[]] $ArgumentList = @(),

    ## The object from which to retrieve the member
    [Parameter(ValueFromPipeline = $true)]
    $InputObject
    )

begin
{
    Set-StrictMode -Version Latest
}

process
{
    ## If the user specified a method, invoke it
    ## with any required arguments.
    if($psCmdlet.ParameterSetName -eq "Method")
    {
        $inputObject.$member.Invoke(@($argumentList))
    }
    ## Otherwise, retrieve the property
    else
    {
        $inputObject.$member
    }
}