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 专为包含对象集合的属性而设计。

添加删除 参数添加单个项并将其从集合中删除。 Replace 参数替换整个集合。

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

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

此 cmdlet 在 PowerShell 7 中重新引入。

示例

示例 1:向属性值添加项

在此示例中,我们将创建一个类,该类表示卡片作为列表 集合对象的 存储卡组。 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 中的代码,我们将创建 卡片 类的实例,以表示一组卡片和两名玩家持有的卡片。 我们使用 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 允许在单个命令中使用 添加删除 参数。 在此示例中,Player 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

指定要添加到集合中的属性值。 按集合中应显示的顺序输入值。

类型:Object[]
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-InputObject

指定要更新的对象。 还可以通过管道将对象更新为 Update-List

类型:PSObject
Position:Named
默认值:None
必需:False
接受管道输入:True
接受通配符:False

-Property

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

类型:String
Position:0
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Remove

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

类型:Object[]
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Replace

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

类型:Object[]
Position:Named
默认值:None
必需:True
接受管道输入:False
接受通配符:False

输入

PSObject

可以通过管道将对象更新到此 cmdlet。

输出

Hashtable

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

Object

指定 属性 参数时,此 cmdlet 将返回更新的对象。