Hashtable feature missing in my script

Hi

I need help with parallel collecting data from a API to a hashtable.
I have managed to get all data and put it in a hasthable, but Im missing a certain feature of this hashtable.
To be able to use $hashtable[$key] to retrieve data value from key.

I will show you an simplified example of my script here.

function ParallelForEach { param([scriptblock]$Process, [parameter(ValueFromPipeline)]$InputObject, $ThrottleLimit = 8, $data) $runspaces = @() $Input | ForEach { while (($runspaces |?{ !$_.Handle.IsCompleted }).Count -ge $ThrottleLimit) { sleep 1 } $r = [PowerShell]::Create().AddScript("param(`$_,`$data);$Process").AddArgument($_).AddArgument($data) $runspaces += [PSCustomObject]@{ Runspace = $r; Handle = $r.BeginInvoke() } } $runspaces | ForEach { while (!$_.Handle.IsCompleted) { sleep 1 } $_.Runspace.EndInvoke($_.Handle) $_.Runspace.Dispose() } } # End function $keylist = @( 'u_f660730d-eba6-420d-93d1-60b7d9677ef2', 'u_d2963ac1-abbf-49ee-a7a6-d9c3556a8252', 'u_e502d8a9-dc17-4145-9244-b4237be00bbf', 'u_a6a5b2c3-d682-4086-a8e2-63b584346f90', 'u_5c0a00fc-9347-4ef9-b68c-88be1d4423ed', 'u_54f28fc8-23eb-41f1-b39e-9c0bb219a0cd' ) $userCustomData = @{} $userCustomData = $keylist | ParallelForEach { $headers = "secret-token" $CustomData = @{} $key = $_ $url = "https://contoso.com/api/v1/users/$key/cool_data?ns=secret-stuff" try { $result = Invoke-RestMethod -Method GET -Uri $url -ContentType 'application/json' -Headers $header }catch{ $result = "" }finally{ if($result) { $CustomData[$key] = @{}; $CustomData[$key] = $result }} Write-Output $CustomData } # End ParallelForEach 

This will give me a nice list of Keys (Name) and Values (Value) in the hashtable $userCustomData.
I can use $userCustomData.Keys and $userCustomData.Values to output information.
I can use $userCustomData.’u_f660730d-eba6-420d-93d1-60b7d9677ef2′ to get this certain key data.

But this command wont give me any output (like hashtables usually does): $userCustomData[‘u_f660730d-eba6-420d-93d1-60b7d9677ef2’] ??

I really want this to work but cant figure out how? Perhaps it’s not possible from my parallel way of collecting the data?

$userCustomData | Get-member TypeName: System.Collections.Hashtable Name MemberType Definition ---- ---------- ---------- Add Method void Add(System.Object key, System.Object value), void IDictionary.Add(System.Object key, System.Object value) Clear Method void Clear(), void IDictionary.Clear() Clone Method System.Object Clone(), System.Object ICloneable.Clone() Contains Method bool Contains(System.Object key), bool IDictionary.Contains(System.Object key) ContainsKey Method bool ContainsKey(System.Object key) ContainsValue Method bool ContainsValue(System.Object value) CopyTo Method void CopyTo(array array, int arrayIndex), void ICollection.CopyTo(array array, int index) Equals Method bool Equals(System.Object obj) GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator(), System.Collections.IDictionaryEnumerator IDictionary.GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerat... GetHashCode Method int GetHashCode() GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializable.GetObjectData(System.Runtime.Serial... GetType Method type GetType() OnDeserialization Method void OnDeserialization(System.Object sender), void IDeserializationCallback.OnDeserialization(System.Object sender) Remove Method void Remove(System.Object key), void IDictionary.Remove(System.Object key) ToString Method string ToString() Item ParameterizedProperty System.Object Item(System.Object key) {get;set;} Count Property int Count {get;} IsFixedSize Property bool IsFixedSize {get;} IsReadOnly Property bool IsReadOnly {get;} IsSynchronized Property bool IsSynchronized {get;} Keys Property System.Collections.ICollection Keys {get;} SyncRoot Property System.Object SyncRoot {get;} Values Property System.Collections.ICollection Values {get;} 

I really appreciate your help and inputs.

submitted by /u/Carenborn
[link] [comments]

Leave a Reply