Posted On April 26, 2019

PowerShell: Error While Invoking Functions Containing 2 Parameters

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Error While Invoking Functions Containing 2 Parameters

Issue:

This function will raise an error when being invoked:

    function localFunc ($x, $y){
begin{}
process{
ping $y
ping $y
}
end{}
}

Invoke-Command -Session $elevate -ScriptBlock {
param( $x, $y, $importedFunc)

# Import the function from the variable inside parameters
[ScriptBlock]::Create($importedFunc).Invoke($x,$y)

} -ArgumentList $xValue, $yValue, ${function:localFunc}

Error:

PS U:\> C:\Users\kimconnect\Desktop\Notes\check-tcpconnection.ps1
Exception calling "Invoke" with "2" argument(s): "The script block cannot be invoked because it contains more than one clause. The Invoke() method can only be used on script blocks that contain a single clause."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : PSInvalidOperationException
+ PSComputerName : SERVER001


Explanation:

A PowerShell function that is being passed via the invoke method has a limitation of only accepting 1 “clause” or 1 block. A block is a parameter of the function object. Yes, a PowerShell function is an object – similar to JavaScript and Python. The easiest way to demonstrate this is to inspect the ForEach-Object.

Get-Help ForEach-Object -Parameter *

-Begin <ScriptBlock>
Specifies a script block that runs before processing any input objects.

Required? false
Position? named
Default value None
Accept pipeline input? false
Accept wildcard characters? false


-End <ScriptBlock>
Specifies a script block that runs after processing all input objects.

Required? false
Position? named
Default value None
Accept pipeline input? false
Accept wildcard characters? false

<# ... #>

-Process <ScriptBlock[]>
Specifies the operation that is performed on each input object. Enter a script
block that describes the operation.

Required? true
Position? 1
Default value None
Accept pipeline input? false
Accept wildcard characters? false

Resolution:

Remove extra parameters inside the original function. For Example,

    function localFunc ($x, $y){
#begin{} #this is not a required block, comment it out
process{
ping $y
ping $y
}
#end{} #remove this extra clause as well
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Resolve Error: (59) An unexpected network error occurred

Prerequisites:   Ensure the the target machine has PowerShell Remoting Enabled (as it's Disabled by…

CSS: Variables & Simple Animation

CSS can mimic other interpreted programming languages by enabling features such as variables. The limitation…

PowerShell: Get All Hyper-V Host Spectre Patch Versions

# getAllVmSpectrePatchVersions.ps1 function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…