0

I have a hashtable used in a job, each hash.key is a job name:

$myhashtable = @{ 
    'somelocation'    = 'somevalue'
    'otherlocation'   = 'othervalue'
}

I run the hashtable against a function, the function is a Start-Job that runs for each object in the hashtable.

foreach ( $location in $myhashtable.GetEnumerator() )
{
    SomeFunction
}

I then get the names of stopped jobs:

$joblist = Get-Job | Where-Object { $_.State -eq "Stopped" }
$joblist = $joblist.name

Trying to do the same as above against the function but only for the jobs that have stopped:

foreach( $location in $myhashtable.GetEnumerator() )
{
    if $location.key matches an object in $joblist}( 
    do SomeFunction for those $location.keys)
}

I've tried various foreach if and where but can't hit on it.

Thanks!

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • Have you tried using an if statement at the top that just sees if item is in job list, and if not, just `continue;`? – Josh Heaps Jul 01 '22 at 19:08
  • 4
    So what do you want to match of `$Array`, the key or the value? I. e. is the job name the array key or the array value? (Nitpicking: it isn't actually an array, but a `Hashtable`) – zett42 Jul 01 '22 at 19:21
  • 2
    _"I've tried various foreach if and where"_, but you don't show any of these attempts. – Theo Jul 01 '22 at 19:33
  • 2
    Your question, as it stand, is not clear. If you want to get your array that was created from a job, you need to use `receive-job` first. If you want to compare arrays and / or object, you can use `Compare-Object`. – Sage Pourpre Jul 01 '22 at 19:37
  • 3
    On a separate note, duplicate keys are not allowed in hash literals, i.e., your `$Array`. – Abraham Zinala Jul 01 '22 at 20:05

2 Answers2

1

as @zett42 noted, your question uses which you name $Array. Renaming this in my reply to avoid confusion, but I think the code below will do what you are trying to do.

PS> $myHashtable = @{
>> 'key1'   = 'value1'
>> 'key2'   = 'value2'
>> 'key3'   = 'value3'
>> }
PS> $joblist= 'key1', 'key2'
PS> $myHashtable.keys | ForEach-Object { if( $_ -in $joblist ){ $myHashtable[$_] } }
value1
value3
Steven
  • 6,817
  • 1
  • 14
  • 14
ben
  • 51
  • 3
  • 3
    If you're interested in switching it up a bit, you can use the `.Where({})` operator instead: `$myHashtable.GetEnumerator().Where{$_.key -in $joblist}.Value`, or `.Key` for the key name. – Abraham Zinala Jul 01 '22 at 20:21
  • 1
    @Retrotube I'm having a bit of trouble parsing just what output you are looking for. Could you please update your question to include what the intended output would look like? – ben Jul 01 '22 at 20:49
  • 1
    @ben May I suggest you add a pseudo function call to your final `If ` block. Reading the question I think that might be the final piece. – Steven Jul 02 '22 at 14:42
  • @steven I'm not sure quite what that means or what problem it would solve. If you'd care to put the code in a comment or point me at an example elsewhere, I'd be glad to learn more. – ben Jul 17 '22 at 19:43
  • @ben Sorry you read too much into that. From the original question I think the op wants to call a function on those values. When I say put a pseudo function call, it's just a place holder to represent that. – Steven Jul 18 '22 at 18:11
1

Here is a possible solution:

$myhashtable = @{ 
'somelocation'    = 'somevalue'
'otherlocation'   = 'othervalue'
}

# For each job that has stopped and whose name matches a key in $myhashtable
Get-Job | Where-Object { $_.State -eq Stopped -and $myhashtable.Contains( $_.Name ) } | 
    ForEach-Object {
        SomeFunction 
    }

When you have a hashtable, you normally shouldn't loop over it, to search for a key somewhere else, which is very inefficient. Instead do it the other way around, loop over the array of objects you want to match and do a lookup in the hashtable, e.g. by calling the .Contains method or using the index operator []. The .Contains method returns $true if the given argument matches one of the keys of the hashtable. This is a very efficient operation, because a hashtable is optimized for fast lookup of keys.

zett42
  • 25,437
  • 3
  • 35
  • 72