< Back

Invoke-Method

Mon Jan 13, 2020 8:25 pm

NAME Invoke-Method



SYNOPSIS

Invokes the method given.





SYNTAX

Invoke-Method -Method <MethodInfo> [[-Object] <Object>] [[-ArgumentList] <Object[]>] [<CommonParameters>]





DESCRIPTION

Invokes the method given to the Method parameter. The object to invoke the method on is supplied to the Object

parameter, and any arguments to the method's parameters are given to the ArgumentList parameter.



The arguments in the ArgumentList must be of the type and in the correct order required by the method. If no

arguments are required, the default is Null.



In the case of static methods, a value of Null is provided to the Object parameter by default.





PARAMETERS

-Method <MethodInfo>

The method to invoke.



Required? true

Position? named

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



-Object <Object>

The object to invoke the method on. Default value is Null, provided to static methods.



Required? false

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ArgumentList <Object[]>

The list of arguments to provide to the method's parameters. Default value is Null, when no arguments are

required.



Required? false

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

System.Reflection.MethodInfo





You can pipe MethodInfo objects to Invoke-Method.





OUTPUTS

System.Object





The output is determined by the return value of the invoked method.





NOTES





This command is intended for use with private methods, since public ones can be called using more standard

means, i.e. $obj.Method() or [Object]::Method().



It will work on public methods, such as the first example.



Author: Chris Carter

Version: 1.0



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>[String] | Get-Method -Name Copy -BF 'Public','Static' | Invoke-Method -ArgumentList "Test String"



This command will invoke the public static Copy method on the string "Test String". Since it is a static method,

no object is provided to the Object parameter.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>[String] | Get-Method -Name CreateTrimmedString -NonPublic -Instance | Invoke-Method -Object "Test String"

-ArgumentList 0,5



This command will invoke the private CreateTrimmedString method on the string "Test String" supplying the

arguments 0 and 5 to the start and end parameters.











RELATED LINKS