Ci-dessous, les différences entre deux révisions de la page.
| dev:powershell:table_de_hachage [2023/06/21 16:28] – créée yoann | dev:powershell:table_de_hachage [2023/08/26 12:25] (Version actuelle) – yoann | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | {{tag> | + | {{tag> |
| Ligne 26: | Ligne 26: | ||
| # Modifier un élément | # Modifier un élément | ||
| $computers.' | $computers.' | ||
| + | |||
| + | # Rechercher une clé | ||
| + | $computers.ContainsKey(' | ||
| </ | </ | ||
| + | |||
| + | |||
| + | ===== Itérations ===== | ||
| + | |||
| + | <code powershell> | ||
| + | foreach($key in $ageList.keys) | ||
| + | { | ||
| + | $message = '{0} is {1} years old' -f $key, $ageList[$key] | ||
| + | Write-Output $message | ||
| + | } | ||
| + | |||
| + | |||
| + | $ageList.keys | ForEach-Object{ | ||
| + | $message = '{0} is {1} years old!' -f $_, $ageList[$_] | ||
| + | Write-Output $message | ||
| + | } | ||
| + | |||
| + | $ageList.GetEnumerator() | ForEach-Object{ | ||
| + | $message = '{0} is {1} years old!' -f $_.key, $_.value | ||
| + | Write-Output $message | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Passage des arguments ===== | ||
| + | |||
| + | |||
| + | On peut utiliser les tables de hachages pour définir les paramètres d'une applet. Plutot que de fournir l' | ||
| + | |||
| + | <code bash> | ||
| + | $DHCPScope = @{ | ||
| + | Name = ' | ||
| + | StartRange | ||
| + | EndRange | ||
| + | SubnetMask | ||
| + | Description | ||
| + | LeaseDuration = (New-TimeSpan -Days 8) | ||
| + | Type = " | ||
| + | } | ||
| + | Add-DhcpServerV4Scope @DHCPScope | ||
| + | </ | ||
| + | |||
| + | ===== Créer un objet ===== | ||
| + | |||
| + | Pour certains traitements, | ||
| + | |||
| + | <code powershell> | ||
| + | |||
| + | $aPerson = [PSCustomObject]@{ | ||
| + | | ||
| + | age = " | ||
| + | | ||
| + | } | ||
| + | |||
| + | # Affiche le type de $aPerson | ||
| + | $aPerson.GetType() | ||
| + | |||
| + | IsPublic IsSerial Name | ||
| + | -------- -------- ---- | ||
| + | True | ||
| + | |||
| + | |||
| + | $aPerson.firstname | ||
| + | John | ||
| + | </ | ||
| + | |||
| + | On peut convertir à tout moment une table de hachage en objet via le transtypage : | ||
| + | <code powershell> | ||
| + | $aPerson = @{ | ||
| + | name = " | ||
| + | age = 33 | ||
| + | } | ||
| + | |||
| + | # Renvoie Hashtable | ||
| + | $aPerson.GetType() | ||
| + | |||
| + | # Transtypage | ||
| + | $aPerson = [PSCustomObject]$aPerson | ||
| + | |||
| + | # Renvoie PSCustomObject | ||
| + | $aPerson.GetType() | ||
| + | </ | ||
| + | |||
| + | ===== Références ===== | ||
| + | |||
| + | * [[https:// | ||