Mule PowerShell 连接器 - 无法检索变量“$<variable-name>”,因为它尚未设置

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

无法弄清楚为什么我的 PowerShell 脚本中的变量一直说该变量为空(错误:无法检索变量“$”,因为它尚未设置。)

Mule Flow = HTTP --> 设置负载 --> PowerShell --> 记录器 --> 结束

~ MULE XML 代码片段 - 使用 PowerShell 连接器 3.x 版本 ~

<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 代码~

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

~ 控制台输出~

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

为了解决这个问题,我删除了 Set-StrictMode -Version Latest

根据我的研究,我找到了这篇文章 -> 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.

当 Set-StrictMode 开启时,它会干扰参数值从 MS PowerShell 连接器 - Mule 3 到 PS 脚本的传递。删除代码行后,参数将被设置值。

您已经声明作为调用 PowerShell 的环境的 Mule 偶然 问题。

症状 - 错误 The variable '<name>' cannot be retrieved because it has not been set. - 实际上意味着正在 访问 从未 设置 (初始化的变量),并且只有在 Set-StrictMode -Version 1 或更高版本生效时才会引发此类错误。

  • -Version 1 仍然允许在 可扩展字符串 (例如 "$noSuchVar")中使用未设置的变量,但 -Version 2 及更高版本则不允许。
    可以公平地假设您没有使用 PowerShell v1(其中 -Version Latest 意味着 -Version 1),因此 any 对执行期间遇到的未设置变量的引用将触发错误。

但是,PowerShell 隐式 管理的 参数 变量(作为 param(...) 块的一部分)是 Set-StrictMode检查,如下例所示:

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

注意:& { ... } 使用调用运算符 & 来执行 脚本块 { ... },但是这样的脚本块的行为就像脚本一样。

如您所见,即使没有参数传递给参数 -paramVar1 - 即没有值绑定到基础 $paramVar1 参数变量 - 访问 $paramVar1 确实 not 导致错误,评估为 $null,在字符串插值的上下文中变为 空字符串 .

将此与引用真正的 unset 变量进行对比:

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

因为 $someVar 从未设置过(也不是参数变量),它触发了错误。

因此,我们可以观察到在撰写本文时问题中打印的代码如下,转载于此:

# 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
  • 首先,Set-StrictMode 调用不能放在 上面 param(...) 块,因为后者必须是 脚本中的第一个语句。

  • 其次,假设错误消息抱怨一个名为 $flowVarManager 的变量,并且代码没有对该变量进行任何引用,引用的代码 单独 不能是问题的根源。