我有一个简单的PowerShell脚本,它查看机器的IP地址,然后根据子网将楼层分配给另一个变量。我目前有一个带有ip地址的数组和一个for语句,用于每个循环以匹配发言权并分配一个变量。我想将此作为哈希表或多维数组执行,但遇到了数组问题。尝试了几个不同的迭代,但我不能让它工作。它是最接近的。
$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}
当我跑$FlooripTest.ContainsKey($testip)
时,我收到了真实的
但我从上面得不到输出。任何建议将不胜感激。
答案 0 :(得分:-1)
你在找这样的东西吗?
$Floors = @{"10.7.20" = "20"; "10.7.21" = "21"; "10.7.22" = "22"}
$testip = "10.7.20.123"
$Floor = $Floors[$testip.Substring(0, $testip.LastIndexOf('.'))]
$Floor
答案 1 :(得分:-1)
首先使用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;}
您已有哈希表。如果你想使用GetHashCode方法。但据我了解,您只想迭代所有成员。
$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()""
}