1

I have a simple powershell script that looks at the machine ip address and then assigns the floor based on the subnet to another variable. I currently have a array with the ip address and an if statement in a for each loop to match the floor and assign a variable. I would like to do this as a hash table or a multidimensional array but ran into problems with the array. Tried a few different iterations but I can't get this to work. Below is the closest it got.

$FloorIpTest = @{"10.7.20" = "20"; "10.7.21" = "21"; "10.7.22" = "10.7.22"}
[string]$testip = "10.7.20"
Foreach ($value in $FlooripTest.ContainsKey($testip)){write-host $Value.Values}

when I run $FlooripTest.ContainsKey($testip) I receive true

but I get no output from the above. Any advice would be appreciated.

1
  • 1
    What are you expecting $value to contain there? Because you know that the result of $FlooripTest.ContainsKey($testip) is True. (Hint: See what echo $true.values outputs.) Commented Mar 23, 2015 at 16:53

1 Answer 1

0

First find your callable methods and properties using get-member

$FloorIpTest = @{"10.7.20" = "20"; "10.7.21" = "21"; "10.7.22" = "10.7.22"}
$FloorIpTest | get-member


   TypeName: System.Collections.Hashtable

Name              MemberType            Definition
----              ----------            ----------
Add               Method                System.Void Add(System.Object key, System.Object value)
Clear             Method                System.Void Clear()
Clone             Method                System.Object Clone()
Contains          Method                bool Contains(System.Object key)
ContainsKey       Method                bool ContainsKey(System.Object key)
ContainsValue     Method                bool ContainsValue(System.Object value)
CopyTo            Method                System.Void CopyTo(array array, int arrayIndex)
Equals            Method                bool Equals(System.Object obj)
GetEnumerator     Method                System.Collections.IDictionaryEnumerator GetEnumerator()
GetHashCode       Method                int GetHashCode()
GetObjectData     Method                System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo inf...
GetType           Method                type GetType()
OnDeserialization Method                System.Void OnDeserialization(System.Object sender)
Remove            Method                System.Void Remove(System.Object key)
ToString          Method                string ToString()
Item              ParameterizedProperty System.Object Item(System.Object key) {get;set;}
Count             Property              System.Int32 Count {get;}
IsFixedSize       Property              System.Boolean IsFixedSize {get;}
IsReadOnly        Property              System.Boolean IsReadOnly {get;}
IsSynchronized    Property              System.Boolean IsSynchronized {get;}
Keys              Property              System.Collections.ICollection Keys {get;}
SyncRoot          Property              System.Object SyncRoot {get;}
Values            Property              System.Collections.ICollection Values {get;}

You already have a hashtable. GetHashCode method is there if you want to use it. But as I understand you want to iterate all of your members only.

$FloorIpTest = @{"10.7.20" = "20"; "10.7.21" = "21"; "10.7.22" = "10.7.22"}
[string]$testip = "10.7.20"
Foreach ($key in $FlooripTest.Keys)
{
    write-host "key: $key"
    write-host "value: $FlooripTest[$key]"
    write-host "hashcode: $FlooripTest[$key].GetHashCode()""
}
Sign up to request clarification or add additional context in comments.

2 Comments

Think I found the answer. $FloorIp = @{"10.7.20" = "20"; "10.7.21" = "21"; "10.7.22" = "10.7.22"} [string]$testip = "10.7.20" $Floors = $FloorIp["$testip"] | out-string The ipaddress of the machine is returned as a variable from a earlier section of the script that comes off a wmi query, some machines have more that one ip address, so I only need to test it in a for loop
Ran into a problem with multiple ip addresses I am running the command $ipaddrWMI = (gwmi -Class Win32_NetworkAdapterConfiguration) | where {$_.IPAddress -ne $Null} | Select-Object -ExpandProperty IPAddress | findstr [.] | out-string | foreach{ $_.trim()}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.