Update-List
在包含对象集合的属性值中添加和删除项。
语法
Update-List
[-Add <Object[]>]
[-Remove <Object[]>]
[-InputObject <PSObject>]
[[-Property] <String>]
[<CommonParameters>]
Update-List
-Replace <Object[]>
[-InputObject <PSObject>]
[[-Property] <String>]
[<CommonParameters>]
说明
cmdlet Update-List
添加、删除或替换 对象的属性值中的项,并返回更新的对象。 此 cmdlet 旨在用于包含对象集合的属性。
“添加”和“删除”参数将各个项添加到集合中,并从集合中删除它们。 Replace 参数替换整个集合。
如果未在命令中指定属性, Update-List
则 返回描述更新的对象,而不是更新对象。 可以将更新对象提交到更改对象的 cmdlet,例如 Set
cmdlet。
仅当正在更新的属性支持使用的 IList 接口 Update-List
时,此 cmdlet 才有效。 此外,接受更新的任何 Set
cmdlet 都必须支持 IList 接口。
随 PowerShell 一起安装的核心 cmdlet 不支持此接口。 若要确定 cmdlet 是否支持 Update-List
,请参阅 cmdlet 帮助主题。
此 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() }
}
注意
cmdlet Update-List
将更新的对象输出到管道。 通过管道将输出传递给 Out-Null
以禁止显示不需要的显示。
示例 2:添加和删除集合属性的项
继续执行示例 1 中的代码,我们将创建 Card 类的实例,以表示一副纸牌和两名玩家持有的卡片。 我们使用 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
允许在单个命令中使用 “添加” 和 “删除 ”参数。 在此示例中,玩家 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
指定要添加到集合中的属性值。 按照值应该在集合中出现的顺序输入值。
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 |
输入
可以通过管道将要更新的对象传递给 Update-List
。
输出
Objects or System.Management.Automation.PSListModifier
Update-List
返回更新的对象,或返回表示更新操作的对象。