2
votes

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

2

2 Answers

2
votes

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.

-1
votes

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

Based off my research I found this article -> https://docs.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.