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 에서 속성을 지정하지 않으면 개체를 업데이트하는 대신 업데이트를 설명하는 해시 테이블을 반환합니다. 나중에 이 변경 집합을 사용하여 목록 개체를 업데이트할 수 있습니다.

이 cmdlet은 업데이트되는 속성이 사용하는 IList 인터페이스 Update-List지원하는 경우에만 작동합니다. 또한 업데이트를 수락하는 모든 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의 코드를 계속 진행하면서 카드 클래스의 인스턴스를 만들어 카드 데크와 두 플레이어가 보유한 카드 나타냅니다. cmdlet을 Update-List 사용하여 플레이어의 손에 카드 추가하고 데크에서 카드 제거합니다.

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

출력은 카드 플레이어에게 처리되기 전의 데크 상태를 보여 줍니다. 각 플레이어가 데크에서 5개의 카드 받은 것을 볼 수 있습니다. 최종 출력은 플레이어에게 카드 처리한 후 데크의 상태를 보여 줍니다. 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♥

예제 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은 업데이트된 개체를 반환합니다.