首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌套循环未按预期的powershell运行

嵌套循环未按预期的powershell运行
EN

Stack Overflow用户
提问于 2019-11-14 22:07:15
回答 1查看 51关注 0票数 0

您好,我正在创建一个脚本,用于创建接口的历史记录,但是我遇到了嵌套循环的问题。

代码语言:javascript
运行
复制
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只匹配一次。有谁能告诉我为什么我的第二个循环不能工作吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-15 16:38:15

好了,我想好了我必须要做什么。在匹配接口名称并开始比较ip地址之后,第二个循环必须在if用例中

代码语言:javascript
运行
复制
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.53
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58858661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档