Use Hashtable with switch case in PowerShell -


i need make decisions in powershell based on values in hashtable.

basically need assign users' upn in active directory according attribute, company one, , thought create hashtable containing keys , values this

 key         value company1    @company1.com company2    @company2.com 

the issue facing don't know how tell powershell use value rather based on company attribute, don't know how cycle/check company attribute against key in hashtable.

i've tried use switches

$a -match $hashtable 

or

$a -contains $hashtable 

with little success.

i think hashtable need here, of course open suggestion using external files match number of companies need match rather high.

while mathias presented proper solution i'd add more explanation.

hashtables , switch statements 2 different solutions same problem: transform input value corresponding output.

 → "foo" b → "bar" c → "baz" ... 

hashtables simpler (and faster) approach, result input value in pre-defined table. advantage can implement associations in 1 place (e.g. declaration section of code define (static) data) , use them in simple manner anywhere else in code:

$domains = @{   'company1' = '@company1.com'   'company2' = '@company2.com' }  $name    = 'foo' $company = 'company1'  $addr = $name + $domains[$company] 

switch statements more complicated in handling, lot more versatile. instance, allow different kinds of comparisons (wildcard, regular expression) in addition simple lookups. allow providing default values/actions if value not listed. hashtables need handle cases additional if/else statement mathias showed, unless you're fine empty value hashtables return when lookup doesn't find match.

$name    = 'foo' $company = 'company1'  $domain = switch ($company) {   'company1' { '@company1.com' }   'company2' { '@company2.com' }   default    { throw 'unknown company' } }  $addr = $name + $domain 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -