TUTOS.EU

Créer et utiliser une hash table en powershell

Comment manipuler une hash table (un dictionnaire) en powershell

#hash table
$objHashTable = @{} #Initialisation d'une hash table
#$objHashTable.Clear()

$objHashTable.Add("A", "01")
$objHashTable.Add("B", "02")

if ($objHashTable.ContainsKey("C") -ne $true) {$objHashTable.Add("C", "01")}
else{write-host "Pouet"}

Write-host "Nombre d'éléments : $($objHashTable.count)"

Write-host ""
Write-host "Valeurs non triées"
#Enumeration des valeurs en ayant trié sur les noms de clé
foreach ($UneLigne in $objHashTable.GetEnumerator()){
	Write-host "($($UneLigne.Key) : $($UneLigne.Value))"
}

#Enumeration des valeurs en ayant trié sur les noms de clé
Write-host ""
Write-host "Contenu trié par clés"
foreach ($UneLigne in $objHashTable.GetEnumerator() | Sort-Object Name ){
	Write-host "($($UneLigne.Key) : $($UneLigne.Value))"
}

Write-host ""
Write-host "Contenu trié par valeurs"
#Enumeration des valeurs en ayant trié sur les noms de clé
foreach ($UneLigne in $objHashTable.GetEnumerator() | Sort-Object Value ){
	Write-host "($($UneLigne.Key) : $($UneLigne.Value))"
}

#Purge de la Hash Table
$objHashTable.Clear()
Lien vers le fichier : cliquez ici Copier le code

Pages Web

Site WebDescription
Technet.microsoft.comWorking with Hash Tables
Blogs.msdn.microsoft.comThe Joy of using Hashtables with Windows PowerShell
Technet.microsoft.comConverting the Dictionary Object to Windows PowerShell Commands

Article(s) précédent(s)

2