본문 바로가기
파워쉘(Powershell)/문법

파워쉘 해시 테이블(Powershell HashTable)

by 예배파 2024. 3. 8.

 

파워쉘 해시테이블(PowershellHashTable)

 

해시 테이블은 배열과 같이 데이터를 저장하는데 사용됩니다

해시 테이블의 다른점은 key와 value의 쌍을 사용해서 데이터를 저장합니다

 

예를들어 컴퓨터들의 정보를 저장하고 싶을때

컴퓨터의 이름 : Key

컴퓨터 정보 (제조사, IP 주소, MAC): Value

 

이렇게 저장 후 필요할 때 컴퓨터 이름으로 정보를 조회할 수 있습니다

 

 

해시 테이블 만들기

#빈 해시 테이블 만들기
$ht = @{}

#해시 테이블 만들면서 값 넣기
$ht = @{
    'com1' = 'Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19'
    'com2' = 'Dell computer,IP:192.168.34.56,MAC:70-B5-E8-4D-98-29'
    'com3' = 'HP,IP:192.168.14.56,MAC:40-C5-E8-4D-92-23'
}

 

 

 

해시 테이블 데이터 넣기

$ht = @{}
$ht.Add('com1','Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19')
$ht.Add('com2','Dell computer,IP:192.168.34.56,MAC:70-B5-E8-4D-98-29')

 

 

 

 

해시 테이블 데이터 읽기

$ht = @{}
$ht.Add('com1','Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19')
$ht.Add('com2','Dell computer,IP:192.168.34.56,MAC:70-B5-E8-4D-98-29')

$ht['com1']


실행결과
Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19

 

 

 

해시 테이블 데이터 바꾸기

$ht = @{}
$ht.Add('com1','Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19')
$ht.Add('com2','Dell computer,IP:192.168.34.56,MAC:70-B5-E8-4D-98-29')

$ht['com2'] = 'aaaa'
$ht

결과
Name                           Value                                                                                                             
----                           -----                                                                                                             
com2                           aaaa                                                                                                              
com1                           Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19

 

 

 

해시 테이블 데이터 제거

$ht = @{}
$ht.Add('com1','Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19')
$ht.Add('com2','Dell computer,IP:192.168.34.56,MAC:70-B5-E8-4D-98-29')

$ht.Remove('com2')
$ht


결과
Name                           Value                                                                                                             
----                           -----                                                                                                             
com1                           Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19

 

 

 

해시 테이블 전체 Key

$ht = @{}
$ht.Add('com1','Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19')
$ht.Add('com2','Dell computer,IP:192.168.34.56,MAC:70-B5-E8-4D-98-29')

$ht.Keys


실행결과
com2
com1

 

 

 

해시 테이블 전체 Value 

$ht = @{}
$ht.Add('com1','computer-1,Dell computer,192.168.34.55,70-B5-E8-4D-18-19')
$ht.Add('com2','computer-2,Dell computer,192.168.34.56,70-B5-E8-4D-98-29')

$ht.Values


실행결과
computer-2,Dell computer,192.168.34.56,70-B5-E8-4D-98-29
computer-1,Dell computer,192.168.34.55,70-B5-E8-4D-18-19

 

 

 

해시 테이블 전체 개수

$ht = @{}
$ht.Add('com1','Dell computer,IP:192.168.34.55,MAC:70-B5-E8-4D-18-19')
$ht.Add('com2','Dell computer,IP:192.168.34.56,MAC:70-B5-E8-4D-98-29')

$ht.Count


실행결과
2

'파워쉘(Powershell) > 문법' 카테고리의 다른 글

파워쉘-PSCustomObject  (0) 2024.04.16
파워쉘 - 날짜, 시간 (Get-Date)  (0) 2024.04.05
ArrayList  (0) 2024.03.07
반복문 (For, ForEach, Do ~ while, While)  (0) 2024.02.09
배열 - Array  (0) 2024.01.21