Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mule PowerShell Connector - The variable '$<variable-name>' cannot be retrieved because it has not been set

Can't figure out why my variable in my PowerShell script keeps saying the variable is null (Error: The variable '$' cannot be retrieved because it has not been set.)

Mule Flow = HTTP --> Set Payload --> PowerShell --> Logger --> End

~ MULE XML Code Snippet - Using PowerShell Connector 3.x version ~

<powershell:execute config-ref="PowerShell" doc:name="PowerShell" scriptFile="classpath:powershell-script.ps1">
   <powershell:parameters>
     <powershell:parameter key="variable1">#[groovy: payload.variable1 ?: '']</powershell:parameter>
     <powershell:parameter key="variable2">#[groovy: payload.variable2 ?: '']</powershell:parameter>
     <powershell:parameter key="variable3">#[groovy: payload.variable3 ?: '']</powershell:parameter>
   <powershell:parameters>
<powershell:execute>

~ PowerShell Code ~

Set-StrictMode -Version Latest 
Param($variable1, $variable2, $variable3)

#Create NEW AD accounts
Function Start-Commands
{
  Write-Output "Start-Commands"
  Write-Output "Parameters-: $variable1 - $variable2 - $variable3"
}

Start-Commands

~ Console Output ~

Root Exception stack trace: org.mule.modules.powershell.PowershellException: The message could not be sent. An exception has been thrown: [{"Exception":"System.Management.Automation.RuntimeException: The variable '$flowVarManager' cannot be retrieved because it has not been set.\r\n

like image 437
JustAnotherUser32 Avatar asked Oct 31 '25 09:10

JustAnotherUser32


1 Answers

You've since stated that Mule as the environment from which PowerShell is invoked is incidental to the problem.

The symptom - error The variable '<name>' cannot be retrieved because it has not been set. - indeed implies that a variable is being accessed that has never been set (initialized), and this kind of error is only raised if Set-StrictMode -Version 1 or higher is in effect.

  • -Version 1 still allows unset variables inside expandable strings (e.g., "$noSuchVar"), but -Version 2 and above does not.
    It's fair to assume that you're not on PowerShell v1 (where -Version Latest would imply -Version 1), so any reference to an unset variable encountered during execution would trigger the error.

However, the parameter variables that PowerShell implicitly manages (as part of the param(...) block) are not subject to Set-StrictMode checking, as the following example demonstrates:

PS> Set-StrictMode -Version Latest; & { param($paramVar1) "[$paramVar1]" }
[]

Note: & { ... } uses &, the call operator, to execute a script block { ... }, but such a script block behaves just as a script would.

As you can see, even though no argument was passed to parameter -paramVar1 - i.e., no value was bound to the underlying $paramVar1 parameter variable - accessing $paramVar1 did not cause an error, evaluated to $null, which in the context of string interpolation becomes the empty string.

Contrast this with referencing a truly unset variable:

PS> Set-StrictMode -Version Latest; & { param($paramVar1) "[$somveVar]" }
The variable '$somveVar' cannot be retrieved because it has not been set.

Because $someVar wasn't ever set (and isn't a parameter variable), it triggered the error.

Therefore, we can observe the following about the code printed in the question as of this writing, reproduced here:

# NOTE: This code is syntactically invalid due to the placement
#       of the Set-StrictMode call.

Set-StrictMode -Version Latest 
Param($variable1, $variable2, $variable3)

#Create NEW AD accounts
Function Start-Commands
{
  Write-Output "Start-Commands"
  Write-Output "Parameters-: $variable1 - $variable2 - $variable3"
}

Start-Commands
  • Firstly, the Set-StrictMode call cannot be placed above the param(...) block, as the latter must be the first statement in a script.

  • Secondly, given that the error message complains about a variable named $flowVarManager and given that the code makes no reference whatsoever to that variable, the quoted code alone cannot be the source of the problem.

like image 84
mklement0 Avatar answered Nov 02 '25 03:11

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!