Search code examples
powershellmuleanypoint-studiomule-esb

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


Solution

  • To fix the issue I removed Set-StrictMode -Version Latest

    Based off my research I found this article -> https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode?view=powershell-6

    The Set-StrictMode cmdlet configures strict mode for the current scope and all child scopes, and turns it on and off. When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.

    When Set-StrictMode is ON it is interfering with the passing of the parameter values from the MS PowerShell Connector - Mule 3 to the PS script. Upon removing the line of code, the parameters were getting set with values.