Update-List

將專案加入至 ,並從包含物件集合的屬性值中移除專案。

Syntax

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

Description

Cmdlet Update-List 會新增、移除或取代 物件屬性值中的專案,並傳回更新的物件。 此 Cmdlet 是針對包含 物件集合的屬性所設計。

[新增] 和 [移除] 參數會將個別專案新增至 集合,並將其從集合中移除。 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() }
}

注意

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 可讓您在單一命令中使用 [新增 ] 和 [移除 ] 參數。 在此範例中,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

指定要加入至集合的屬性值。 輸入值,順序應該出現在集合中。

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 會傳回更新的物件。