PowerShell错误方法调用失败

时间:2016-10-19 15:24:17

标签: powershell cruisecontrol.net

我有一个奇怪的问题,我现有的代码可以在Windows 7x86 PowerShell 2.0上运行,但在使用PowerShell 5.0的Windows 10x86上无效。

错误说明:

  

方法调用失败,因为   [System.Management.Automation.PSRemotingJob]不包含名为“op_Addition”的方法。   在   C:\建立\ AmazingCharts \工作\建筑\安装\ WiseInstaller \ Obfusated      项目\ ObfuscateFiles.ps1:211   char:13 + $ Jarray + =   Start-Job -name ObfuscateFiles $ ScriptBlock - ...
  +
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
      + CategoryInfo:InvalidOperation:   (op_Addition:String)[],Runti
  meException +   FullyQualifiedErrorId:MethodNotFound

这说明重载的运算符+ =似乎没有被定义,但我发现很难相信在PowerShell 5.0中对于数组不推荐使用+ =重载的加法运算符。

此外,代码似乎仍然执行并产生正确的输出。 在我的例子中,我使用一个作业数组来执行Red Gate Smart Assembly来混淆构建文件。以下是代码段

$Jarray = @() #initialize jobs array 
foreach ($file in $farray_) {
   $params = $file,$cwd,$bldLog #parameters that are passed to script block
   $nCount = (get-job -state "Running").Count #determines num of running jobs
   if (-not $nCount) {$ncount=0} #initialize variable if null
   if ($nCount -le $MAX_INSTANCES) {
      #The line below is the one that generates the error
      $Jarray += Start-Job -name ObfuscateFiles $ScriptBlock -ArgumentList $params 
      Start-Sleep -s 1  #gives the system time for process to start
   } else {
      $nCount = (get-job -state "Running").Count 
      if (-not $nCount) {$nCount=0}
      while ($nCount -ge $MAX_INSTANCES) {
         Start-Sleep -s 1  #gives time to recognize the job has started 
         $nCount = (get-job -state "Running").Count
         if (-not $nCount) {$nCount=0}
      }
      $Jarray += Start-Job -name ObfuscateFiles $ScriptBlock  -ArgumentList $params
   }
} 

执行此操作时,它会及时正确地生成所有模糊文件,因此我知道它是并行构建的MAX_INSTANCES值。我甚至有代码(上面没有包含)来验证预期的混淆文件的数量是否正确。 '方法调用失败'错误报告回CruiseControl.NET,我的构建失败。 另一个令人沮丧的是,在PowerGUI中执行相同的.ps1文件成功完成,没有任何错误。

我已确保我的数组已初始化。我已经尝试过建议指定数组是[PSObject]类型,但我得到相同的结果。

有没有人有任何想法我可以检查这个?

谢谢。

编辑:

  • 我在PowerShell IDE中对此进行了测试 - 从命令提示符运行测试时,我收到错误 - 运行调试器测试时,我没有收到错误
  • 我检查了$ Jarray类型,它是一个System.Object [],这是我对数组的期望。
  • 我在调试期间检查了$ Jarray值并且正在创建作业,但错误仍然存​​在,就像它不理解+ =重载的加法运算符一样。我唯一的猜测是,这是一个掩盖其他东西的虚假错误。
  • 我在$ Jarray的调用周围添加了一个try / catch块。我曾尝试过ErrorAction Ignore,但由于调用不知道如何处理错误,因此无效。 try / catch块成功掩盖了错误;幸运的是,我有另一种方法来确定我的通话是否成功。毋庸置疑,这并不能“解决”这个问题,它只会掩盖这个问题,因为现在它并不重要。

我将继续使用我测试的内容更新此区域。

1 个答案:

答案 0 :(得分:0)

只是一个想法,但可能想要确保您的新机器有一个宽松的远程策略和启用脚本:

Set-ExecutionPolicy Unrestricted& 使-PSRemoting