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 返回描述更新而不是更新对象的哈希表。 稍后,可以使用此更改集更新列表对象。

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

示例

示例 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 = [char]0x2663,[char]0x2666,[char]0x2665,[char]0x2660
        $_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[]

默认值: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

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

输出

Hashtable

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

Object

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