您好,我正在创建一个脚本,用于创建接口的历史记录,但是我遇到了嵌套循环的问题。
for($r = 0; $r -lt $iface.Count; $r++){
for($m = 0; $m -lt $iface.Count; $m++){
if($ifaceold[$r] -match $iface[$m]){
Write-Host "name true" " " $ifaceold[$r] " " $iface[$m]
if($ifaceold[$r] -match $ifaceip[$m]){
Write-Host "ip true" " " $ifaceold[$r] " " $iface[$m] " " $ifaceip[$m]
}else{
Write-Host "ip false" " " $ifaceold[$r] " " $iface[$m] " " $ifaceip[$m]
}
}else{
Write-Host "name false" " " $ifaceold[$r] " " $iface[$m]
}
}
}
Result:
name true Ethernet 6 10.10.2.5 Ethernet 6
ip false Ethernet 6 10.10.2.5 Ethernet 6 10.10.2.1
name false Ethernet 6 10.10.2.5 Ethernet 5
name false Ethernet 6 10.10.2.5 Ethernet 4
name false Ethernet 5 10.10.1.1 Ethernet 6
name true Ethernet 5 10.10.1.1 Ethernet 5
ip true Ethernet 5 10.10.1.1 Ethernet 5 10.10.1.1
name false Ethernet 5 10.10.1.1 Ethernet 4
name false Ethernet 4 192.168.77.53 Ethernet 6
name false Ethernet 4 192.168.77.53 Ethernet 5
name true Ethernet 4 192.168.77.53 Ethernet 4
ip true Ethernet 4 192.168.77.53 Ethernet 4 192.168.1.53由于某些原因,第二个循环不能按预期工作。例如,$iface将等于以太网6,并且它将匹配以以太网6、以太网4、以太网5作为值的$ifaces阵列。如果$ifaceold与$iface匹配,那么它将转到下一个If序列,这对我来说是错误的。现在,我希望将以太网6 ip与$ifaceip值进行匹配,如果它们匹配,则输出为true。但正如您所看到的,它与名称匹配3次,但每个接口的ip只匹配一次。有谁能告诉我为什么我的第二个循环不能工作吗?
发布于 2019-11-15 16:38:15
好了,我想好了我必须要做什么。在匹配接口名称并开始比较ip地址之后,第二个循环必须在if用例中
for($r = 0; $r -lt $iface.Count; $r++){
for($m = 0; $m -lt $iface.Count; $m++){
if($ifaceold[$r] -match $iface[$m]){
Write-Host "name true" " " $ifaceold[$r] " " $iface[$m]
for($q = 0; $q -lt $iface.Count; $q++){
if($ifaceold[$r] -match $ifaceip[$q]){
Write-Host "ip true" " " $ifaceold[$r] " = " $iface[$q] " " $ifaceip[$q]
}else{
Write-Host "ip false" " " $ifaceold[$r] " = " $iface[$q] " " $ifaceip[$q]
}
}
}else{
Write-Host "name false" " " $ifaceold[$r] " = " $iface[$m]
}
}
}
Result:
name true Ethernet 6 10.10.2.5
ip false Ethernet 6 10.10.2.5 Ethernet 6 10.10.2.1
ip false Ethernet 6 10.10.2.5 Ethernet 5 10.10.1.1
ip false Ethernet 6 10.10.2.5 Ethernet 4 192.168.1.53
name true Ethernet 5 10.10.1.1
ip false Ethernet 5 10.10.1.1 Ethernet 6 10.10.2.1
ip true Ethernet 5 10.10.1.1 Ethernet 5 10.10.1.1
ip false Ethernet 5 10.10.1.1 Ethernet 4 192.168.1.53
name true Ethernet 4 192.168.77.53
ip false Ethernet 4 192.168.77.53 Ethernet 6 10.10.2.1
ip false Ethernet 4 192.168.77.53 Ethernet 5 10.10.1.1
ip true Ethernet 4 192.168.77.53 Ethernet 4 192.168.1.53https://stackoverflow.com/questions/58858661
复制相似问题