You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本出现Cannot find an overload for 'contains'错误求助

问题描述

运行PowerShell脚本生成Azure部署JSON文件时遇到两个问题:

  1. 脚本持续要求输入vnetArray,陷入循环
  2. 随后抛出以下错误:
Cannot find an overload for "contains" and the argument count: "2".
At \Deploy-Network.ps1:58 char:6
+ if ( $ARMTemplate.contains('vnet2', [System.StringComparison]::Curren ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
解决方案

1. 修复contains方法调用错误

PowerShell字符串的Contains()方法不支持第二个参数(字符串比较规则),这是错误的直接原因。将第58行代码替换为以下兼容写法之一:

写法1:使用-match实现不区分大小写匹配

if ( $ARMTemplate -match '(?i)vnet2' ) {

写法2:使用IndexOf方法替代

if ( $ARMTemplate.IndexOf('vnet2', [System.StringComparison]::CurrentCultureIgnoreCase) -ge 0 ) {

2. 解决vnetArray输入循环问题

脚本陷入输入循环的核心原因是vnetParam.json文件异常:

  • 确认脚本同目录下存在vnetParam.json文件
  • 检查文件结构,确保包含顶级vNetArray属性,示例结构如下:
{
  "vNetArray": [
    {
      "name": "vnet1",
      "addressPrefixes": ["10.0.0.0/16"],
      "subnets": [
        {
          "name": "subnet1",
          "addressPrefix": "10.0.0.0/24"
        }
      ]
    }
  ]
}
  • 若文件存在但属性名拼写错误(如大小写不一致),修正为vNetArray

3. 修复数组参数处理逻辑

脚本中Convert-ParamHashToARMParamJson函数对数组类型参数未正确处理(仅输出红色提示),导致生成的参数JSON缺失数组值。需修改函数内的array分支:

修改前

'array' {
  Write-Host "ARRAY!!!"  -ForegroundColor Red
}

修改后

'array' {
  $obj.parameters.Add($Key, @{"value" = $Hash.Item($Key) })
}

内容的提问来源于stack exchange,提问作者learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 16:13:24