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

如何向Import-Excel导入的PowerShell Object[]数组添加元素?

问题根源与解决方法

你遇到的问题核心在于两点:

  1. Import-Excel导入的数组元素是PSCustomObject类型的自定义对象,但你创建的$New是哈希表数组,类型不匹配,直接添加会导致元素格式异常;
  2. PowerShell中的Object[]是固定大小数组,且函数默认采用传值传递,函数内部对数组的修改不会同步到外部原变量。

1. 创建匹配类型的新元素

不要用哈希表数组,直接生成和原有元素一致的PSCustomObject:

$New = [PSCustomObject]@{
    Ext        = $DocExt
    DotExt     = $DotExt
    Application = "update"
    Active     = 1
    Inactive   = 0
    Skipped    = 0
}

2. 数组修改与传递的三种可行方案

方案一:让函数返回修改后的数组(适合固定大小数组)

因为Object[]的+=操作实际是创建新数组,函数内部修改后返回,外部重新赋值原变量:

function Set-Extensions {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [array] $FileExtsList,
        [string]$DocExt,
        [string]$DotExt
    )

    # 检测扩展名是否存在
    $exists = $FileExtsList | Where-Object { $_.Ext -eq $DocExt }
    if (-not $exists) {
        $New = [PSCustomObject]@{
            Ext        = $DocExt
            DotExt     = $DotExt
            Application = "update"
            Active     = 1
            Inactive   = 0
            Skipped    = 0
        }
        $FileExtsList += $New
    }

    return $FileExtsList
}

# 调用函数并更新原数组
$Win_FileExtsList = Set-Extensions -FileExtsList $Win_FileExtsList -DocExt "XXX" -DotExt ".XXX"

方案二:使用ArrayList(推荐,高效动态修改)

将原数组转换为支持动态添加的System.Collections.ArrayList,函数内部可直接修改:

# 把导入的Object[]转为ArrayList
$Win_FileExtsList = [System.Collections.ArrayList]::new($Win_FileExtsList)

function Set-Extensions {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [System.Collections.ArrayList] $FileExtsList,
        [string]$DocExt,
        [string]$DotExt
    )

    $exists = $FileExtsList | Where-Object { $_.Ext -eq $DocExt }
    if (-not $exists) {
        $New = [PSCustomObject]@{
            Ext        = $DocExt
            DotExt     = $DotExt
            Application = "update"
            Active     = 1
            Inactive   = 0
            Skipped    = 0
        }
        # 忽略Add方法返回的索引值
        $null = $FileExtsList.Add($New)
    }
}

# 调用函数后原ArrayList会直接被修改
Set-Extensions -FileExtsList $Win_FileExtsList -DocExt "XXX" -DotExt ".XXX"

方案三:使用引用参数[ref](针对固定大小数组)

通过引用参数让函数直接修改外部变量:

function Set-Extensions {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [ref] $FileExtsList,
        [string]$DocExt,
        [string]$DotExt
    )

    $currentList = $FileExtsList.Value
    $exists = $currentList | Where-Object { $_.Ext -eq $DocExt }
    if (-not $exists) {
        $New = [PSCustomObject]@{
            Ext        = $DocExt
            DotExt     = $DotExt
            Application = "update"
            Active     = 1
            Inactive   = 0
            Skipped    = 0
        }
        $currentList += $New
        $FileExtsList.Value = $currentList
    }
}

# 调用时传入引用
Set-Extensions -FileExtsList ([ref]$Win_FileExtsList) -DocExt "XXX" -DotExt ".XXX"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 21:42:38