Update-List

在包含对象集合的属性值中添加和删除项。

语法

Update-List
      [-Add <Object[]>]
      [-Remove <Object[]>]
      [-InputObject <PSObject>]
      [[-Property] <String>]
      [<CommonParameters>]
Update-List
      -Replace <Object[]>
      [-InputObject <PSObject>]
      [[-Property] <String>]
      [<CommonParameters>]

说明

Update-List cmdlet 添加、移除或替换对象的属性值中的项,并返回更新后的对象。 此 cmdlet 旨在用于包含对象集合的属性。

Add 和 Remove 参数分别在集合中添加和移除各项。 Replace 参数替换整个集合。

如果未在命令中指定属性, Update-List 则返回描述更新而不是更新对象的哈希表。 稍后,可以使用此更改集更新列表对象。

仅当要更新的属性支持 使用的 IList 接口 Update-List 时,此 cmdlet 才有效。 此外,接受更新的任何 Set cmdlet 都必须支持 IList 接口。

此 cmdlet 在 PowerShell 7 中重新引入。

示例

示例 1:向属性值添加项

在此示例中,我们创建一个表示一副扑克牌的类,其中,扑克牌存储为一个 List 集合对象。 NewDeck() 方法使用 Update-List 向扑克牌集合添加完整的一副扑克牌值。

class Cards {

    [System.Collections.Generic.List[string]]$cards
    [string]$name

    Cards([string]$_name) {
        $this.name = $_name
        $this.cards = [System.Collections.Generic.List[string]]::new()
    }

    NewDeck() {
        $_suits = "`u{2663}","`u{2666}","`u{2665}","`u{2660}"
        $_values = 'A',2,3,4,5,6,7,8,9,10,'J','Q','K'
        $_deck = foreach ($s in $_suits){ foreach ($v in $_values){ "$v$s"} }
        $this | Update-List -Property cards -Add $_deck | Out-Null
    }

    Show() {
        Write-Host
        Write-Host $this.name ": " $this.cards[0..12]
        if ($this.cards.count -gt 13) {
            Write-Host (' ' * ($this.name.length+3)) $this.cards[13..25]
        }
        if ($this.cards.count -gt 26) {
            Write-Host (' ' * ($this.name.length+3)) $this.cards[26..38]
        }
        if ($this.cards.count -gt 39) {
            Write-Host (' ' * ($this.name.length+3)) $this.cards[39..51]
        }
    }

    Shuffle() { $this.cards = Get-Random -InputObject $this.cards -Count 52 }

    Sort() { $this.cards.Sort() }
}

注意

Update-List cmdlet 将更新的对象输出到管道。 我们通过管道将输出传递给 Out-Null 来抑制不需要的显示。

示例 2:添加和移除集合属性的项

接示例 1 中的代码继续进行,我们将创建 Cards 类的实例,来表示一副牌和两名玩家持有的牌。 我们使用 Update-List cmdlet 将牌添加到玩家手中,并从一副牌中移除这些牌。

$player1 = [Cards]::new('Player 1')
$player2 = [Cards]::new('Player 2')

$deck = [Cards]::new('Deck')
$deck.NewDeck()
$deck.Shuffle()
$deck.Show()

# Deal two hands
$player1 | Update-List -Property cards -Add $deck.cards[0,2,4,6,8] | Out-Null
$player2 | Update-List -Property cards -Add $deck.cards[1,3,5,7,9] | Out-Null
$deck | Update-List -Property cards -Remove $player1.cards | Out-Null
$deck | Update-List -Property cards -Remove $player2.cards | Out-Null

$player1.Show()
$player2.Show()
$deck.Show()

Deck :  4♦ 7♥ J♦ 5♣ A♣ 8♦ J♣ Q♥ 6♦ 3♦ 9♦ 6♣ 2♣
        K♥ 4♠ 10♥ 8♠ 10♦ 9♠ 6♠ K♦ 7♣ 3♣ Q♣ A♥ Q♠
        3♥ 5♥ 2♦ 5♠ J♥ J♠ 10♣ 4♥ Q♦ 10♠ 4♣ 2♠ 2♥
        6♥ 7♦ A♠ 5♦ 8♣ 9♥ K♠ 7♠ 3♠ 9♣ A♦ K♣ 8♥

Player 1 :  4♦ J♦ A♣ J♣ 6♦

Player 2 :  7♥ 5♣ 8♦ Q♥ 3♦

Deck :  9♦ 6♣ 2♣ K♥ 4♠ 10♥ 8♠ 10♦ 9♠ 6♠ K♦ 7♣ 3♣
        Q♣ A♥ Q♠ 3♥ 5♥ 2♦ 5♠ J♥ J♠ 10♣ 4♥ Q♦ 10♠
        4♣ 2♠ 2♥ 6♥ 7♦ A♠ 5♦ 8♣ 9♥ K♠ 7♠ 3♠ 9♣
        A♦ K♣ 8♥

输出显示扑克牌被发给玩家之前这副牌的状态。 可以看到每个玩家都收到了这副牌中的五张牌。 最终输出显示向玩家发牌后这副牌的状态。 Update-List 用于选择这副牌中的牌,并将其添加到玩家的集合中。 然后,使用 Update-List 从这副牌中移除了玩家的牌。

示例 3:在单个命令中添加和移除项

Update-List 支持在单个命令中使用 Add 和 Remove 参数。 在此示例中,玩家 1 希望放弃 4♦6♦ 并获取两张新牌。

# Player 1 wants two new cards - remove 2 cards & add 2 cards
$player1 | Update-List -Property cards -Remove $player1.cards[0,4] -Add $deck.cards[0..1] | Out-Null
$player1.Show()

# remove dealt cards from deck
$deck | Update-List -Property cards -Remove $deck.cards[0..1] | Out-Null
$deck.Show()

Player 1 :  J♦ A♣ J♣ 9♦ 6♣

Deck :  2♣ K♥ 4♠ 10♥ 8♠ 10♦ 9♠ 6♠ K♦ 7♣ 3♣ Q♣ A♥
        Q♠ 3♥ 5♥ 2♦ 5♠ J♥ J♠ 10♣ 4♥ Q♦ 10♠ 4♣ 2♠
        2♥ 6♥ 7♦ A♠ 5♦ 8♣ 9♥ K♠ 7♠ 3♠ 9♣ A♦ K♣
        8♥

示例 4:将更改集应用于列表对象

如果未指定属性, Update-List 则返回描述更新而不是更新对象的哈希表。 可以将哈希表 强制转换为 System.PSListModifier 对象,并使用 ApplyTo() 方法将更改集应用于列表。

$list = [System.Collections.ArrayList] (1, 43, 2)
$changeInstructions = Update-List -Remove 43 -Add 42
$changeInstructions

Name                           Value
----                           -----
Add                            {42}
Remove                         {43}

([PSListModifier]($changeInstructions)).ApplyTo($list)
$list

1
2
42

参数

-Add

指定要添加到集合中的属性值。 按照值应该在集合中出现的顺序输入值。

Type:Object[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InputObject

指定要更新的对象。 也可以通过管道将要更新的对象传递给 Update-List

Type:PSObject
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Property

指定包含要更新的集合的属性。 如果省略此参数,则 Update-List 将返回一个表示更改的对象,而不是更改该对象。

Type:String
Position:0
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Remove

指定要从集合中删除的属性值。

Type:Object[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Replace

指定新集合。 此参数会将原始集合中的所有项替换为由此参数指定的项。

Type:Object[]
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

输入

PSObject

可以通过管道将要更新的对象传递给此 cmdlet。

输出

Hashtable

默认情况下,此 cmdlet 返回描述更新的哈希表。

Object

指定了 Property 参数时,此 cmdlet 返回更新后的对象。