4

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

JustAnotherUser32
  • 137
  • 2
  • 5
  • 17

3 Answers3

4

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

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.

JustAnotherUser32
  • 137
  • 2
  • 5
  • 17
  • That's good to know, but (a) the placement of `Set-StrictMode -Version Latest` in the code in your question is syntactically invalid (`param()` must come _first_), and (b) `Set-StrictMode` doesn't apply to _parameter_ variables (verify with `Set-StrictMode -Version Latest; & { param($foo) $foo }`, which produces _no_ error), so while there must be _some_ code involved that is sensitive to unset variables, it still isn't yours _as shown_. So I'm still curious as to whether the problem is related to Mule at all. – mklement0 Jul 19 '18 at 15:28
  • 1
    The problem was not with Mule, it was with the PowerShell script. If Set-StrictMode does not apply to parameter variables, then how come it was not allowing the parameters to become set? Then why did removing the Set-StrictMode resolve the issue? – JustAnotherUser32 Jul 19 '18 at 17:08
  • Please see my answer. – mklement0 Jul 19 '18 at 19:22
  • I can't explain why it worked. I just removed Set-StrictMode -Version Latest from the PowerShell and everything started working. – JustAnotherUser32 Aug 16 '18 at 14:19
  • That may be, but the _explanation_ you're giving is incorrect, as mentioned in my previous comment and as discussed in detail in my answer. – mklement0 Aug 16 '18 at 14:24
  • Yes, I understand your response, however Latest was also preventing the params from showing up as well. The fix was removing that line of code. – JustAnotherUser32 Aug 16 '18 at 15:48
  • I understand that your fix was _effective_, but saying that `Set-StrictMode` being on interferes with parameter passing is incorrect and misleading. With the sample code you've given, we still don't know what the true cause of the problem is. Using my answer as guidance, I encourage you to dig deeper to find that cause, so we can present it to future readers, including a verdict on whether the problem is specific to MuleConnector or not. – mklement0 Aug 16 '18 at 15:52
  • It will be challenging to dig deeper as the PowerShell connector was something created by MuleSoft. – JustAnotherUser32 Aug 16 '18 at 16:22
  • Understood, but you can start by investigating where the `$flowVarManager` variable is being referenced. – mklement0 Aug 16 '18 at 16:23
0

I get it at connect-exchangeonline, my solution was:

$VerbosePreference = $false

Cannot update the manifest file because the manifest is not valid. Verify that the manifest file is valid, and then try again.'The variable $VerbosePreference cannot be retrieved because it has not been set.'

Tyler2P
  • 2,324
  • 26
  • 22
  • 31