Update-List

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

语法

AddRemoveSet (默认值)

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

ReplaceSet

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

说明

Update-List cmdlet 添加、删除或替换对象属性值中的项,并返回更新的对象。 此 cmdlet 专为包含对象集合的属性而设计。

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

如果未在命令中指定属性, Update-List 则返回描述更新的对象,而不是更新对象。 可以将更新对象提交到更改对象的 cmdlet,例如 Set cmdlet。

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

随 PowerShell 一起安装的核心 cmdlet 不支持此接口。 若要确定 cmdlet 是否支持 Update-List,请参阅 cmdlet 帮助主题。

此 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♥

参数

-Add

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

参数属性

类型:

Object[]

默认值:None
支持通配符:False
不显示:False

参数集

AddRemoveSet
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-InputObject

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

参数属性

类型:PSObject
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:True
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Property

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

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:0
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Remove

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

参数属性

类型:

Object[]

默认值:None
支持通配符:False
不显示:False

参数集

AddRemoveSet
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Replace

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

参数属性

类型:

Object[]

默认值:None
支持通配符:False
不显示:False

参数集

ReplaceSet
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters

输入

PSObject

您可以将要更新的对象通过管道连接到 Update-List

输出

Objects or System.Management.Automation.PSListModifier

Update-List 返回更新的对象,或者返回表示更新作的对象。