代码之家  ›  专栏  ›  技术社区  ›  mahmood

在powershell中向arry添加元素

  •  0
  • mahmood  · 技术社区  · 4 年前

    我只想向数组中添加一些数字,然后通过powershell对它们进行排序,但是,下面的代码似乎是错误的

    $myArray = New-Object System.Collections.ArrayList
    Foreach ($Name in $VMName) {
        $Tokens = $Name.Split(".")
        $myArray.Add($Tokens[$Tokens.Count-1])
    }
    Write-Host($myArray | Sort-Object)
    

    错误是

    +     $myArray.Add($Tokens[$Tokens.Count-1])
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : NotSupportedException
    

    我怎样才能解决这个问题?

    变量 $Name 101.u18.uab.14 103.win10.template 102.win7.pink.18 等等每个 $Name . 符号,我想对它们进行标记,并获取每个符号的最后一个元素。在这个例子中,我想看到一个排序的 14 18 template .

    更新:

    提供的方法似乎不正确。

    $myArray = New-Object System.Collections.ArrayList
    Foreach ($Name in $VMName) {
        $Tokens = $Name.Split(".")
        [Void]$myArray.Add($Tokens[-1])
    }
    

    显示此错误

    Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
    At C:\Users\user\Desktop\get_ip_list.ps1:20 char:5
    +     [Void]$myArray.Add($Tokens[-1])
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : NotSupportedException
    

    2-圣地亚哥的这种方法

    $myArray = [Collections.Generic.List[string]]::new()
    Foreach ($Name in $VMName) {
        [Void]$myArray.Add($Name.Split(".")[-1])
    }
    

    显示以下错误

    Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
    At C:\Users\user\Desktop\get_ip_list.ps1:19 char:5
    +     [Void]$myArray.Add($Name.Split(".")[-1])
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : NotSupportedException
    

    如果我在上述代码中没有理解你的观点,请告诉我。

    1 回复  |  直到 4 年前
        1
  •  3
  •   Steven    4 年前

    我认为你错过了错误的第一行。但是,看起来您只是在尝试添加 $Tokens 大堆在这种情况下,您不需要像这样引用索引,下面应该可以:

    $myArray = New-Object System.Collections.ArrayList
    Foreach ($Name in $VMName) {
        $Tokens = $Name.Split(".")
        [Void]$myArray.Add($Tokens[-1])
    }
    

    注意添加了 [Void] 这将阻止 .Add()

    另请注意,您可以使用强制转换创建数组列表对象,如:

    $myArray = [Collections.ArrayList]@()
    

    更新以解决持续的错误:

    例子:

    [String[]]$myArray = @()
    
    # Posibly a whole bunch of other things happening maybe in the console or IDE.
    
    $myArray = [Collections.ArrayList]@()
    $myArray.Add('something')
    

    Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
    At line:1 char:1
    + $myArray.Add('something')...
    

    $myArray 不会变为 [Collections.ArrayList] . 直到您尝试使用 .Add() [String[]] [Object[]] .

    注意:如果你要跑步 $myArray.IsFixedSize 它将返回“True”。

    我对发生了什么事的猜测;在开发代码时,或者在更大的脚本中, $myArray 类型受到约束,并卡在范围内。这肯定会发生,特别是考虑到像PowerShell的ISE这样的IDE中的作用域重叠,我认为它也会发生在VSCode中。如果这是较大脚本的一部分,请查找 $myArray 查看它是否确实受类型约束,并根据需要进行更正。否则,只需重新启动会话就可以了。

        2
  •  2
  •   Santiago Squarzon    4 年前

    老实说,我不知道你怎么会犯这样的错误,除非 array 事实上,我们的循环是不同的。 Steven's answer 如果工作正常,我将在下面列出这段代码,以表明我们得到的结果是您期望的结果:

    $col = [Collections.Generic.List[String]]::new()
    
    $vmName = @(
        '101.u18.uab.14'
        '103.win10.template'
        '102.win7.pink.18'
    )
    
    ForEach($name in $vmName)
    {
        $col.Add($name.Split('.')[-1])
    }
    

    enter image description here

        3
  •  1
  •   Esperento57    4 年前

    如果您想绝对使用阵列,只需执行以下操作:

    $Array=@()
    
    $VMName | %{
     $Value=($_.Split('.'))[-1]
     $Array+= $Value
    }
    
    $Array| sort
    

    否则,您只需执行以下操作:

    $VMName | %{($_.Split('.'))[-1]} | sort
    
    推荐文章