我有一个奇怪的问题,我现有的代码可以在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]类型,但我得到相同的结果。
有没有人有任何想法我可以检查这个?
谢谢。
编辑:
我将继续使用我测试的内容更新此区域。
答案 0 :(得分:0)
只是一个想法,但可能想要确保您的新机器有一个宽松的远程策略和启用脚本:
Set-ExecutionPolicy Unrestricted& 使-PSRemoting