【问题标题】:Incrementing until number is reached递增直到达到 number
【发布时间】:2018-09-13 16:46:30
【问题描述】:

我有一个数字,让我们使用11 作为示例值。我有名为(在本例中)0266AP1, 0266AP2, 0266AP3, 0266AP4 等的访问点。 0266 是站点/商店编号。

通过调用 Cisco Prime API,我可以看到站点 0266 有 11 个接入点。为了让真正的快速列表传递给我的控制器,我想要做的是递增,直到达到11@count 的值。

Function Get-AllApNames {
    Write-Verbose "Getting all APs for Store $Store"
    $req = "https://cpist/webacs/api/v3/data/AccessPointDetails.json?.group=$Store"
    Write-Verbose "Making request to $storeApReq"
    $idReq = Invoke-RestMethod -uri $storeApReq -method Get -ContentType 'application/json' -headers @{ Authorization = $auth }
    Write-Log "Making Get request to $storeApReq" -Level INFO -logfile $logFile
    $apIdCount =  $apIdListReq.queryResponse."@count"
    $apArray = New-Object System.Collections.ArrayList
}

我已经删除了我的尝试,因为它们都是空的,但我基本上想使用 $apIdCount 作为我的停止点,并使用 1 作为我的起点。

解决方案 1:

Function Get-AllApNames {
    Write-Verbose "Getting all APs for Store $Store"
    $req = "https://cpist/webacs/api/v3/data/AccessPointDetails.json?.group=$Store"
    Write-Verbose "Making request to $storeApReq"
    $idReq = Invoke-RestMethod -uri $storeApReq -method Get -ContentType 'application/json' -headers @{ Authorization = $auth }
    Write-Log "Making Get request to $storeApReq" -Level INFO -logfile $logFile
    $apIdCount =  $apIdListReq.queryResponse."@count"

    $apArray = New-Object System.Collections.ArrayList

    $apLoop = 1..$apIdCount

    foreach($i in $apLoop) {
        $accPt = $Store + 'AP' + $i
        Write-Host $accPt
    }
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您所描述的称为for 循环:

    for ($i=1; $i -le $apIdCount; $i++) {
        $accPt = $Store + 'AP' + $i
        Write-Host $accPt
    }
    

    另请参阅 this Technet article 了解 PowerShell 中的循环控制结构。

    【讨论】:

    • 对我来说有意义,而不是 foreach 循环。
    【解决方案2】:

    在提到1 是我的起点和$apIdCount 是我的终点之后,我实现了一个简单的方法。

    Function Get-AllApNames {
        Write-Verbose "Getting all APs for Store $Store"
        $req = "https://cpist/webacs/api/v3/data/AccessPointDetails.json?.group=$Store"
        Write-Verbose "Making request to $storeApReq"
        $idReq = Invoke-RestMethod -uri $storeApReq -method Get -ContentType 'application/json' -headers @{ Authorization = $auth }
        Write-Log "Making Get request to $storeApReq" -Level INFO -logfile $logFile
        $apIdCount =  $apIdListReq.queryResponse."@count"
    
        $apArray = New-Object System.Collections.ArrayList
    
        $apLoop = 1..$apIdCount
    
        foreach($i in $apLoop) {
            $accPt = $Store + 'AP' + $i
            Write-Host $accPt
        }
    }
    

    这正是我正在寻找的输出。

    0266AP1
    0266AP2
    0266AP3
    0266AP4
    0266AP5
    0266AP6
    0266AP7
    0266AP8
    0266AP9
    0266AP10
    0266AP11
    

    我不确定这是否是处理此问题的最佳方法,因此我暂时将其保留为有经验的意见!

    【讨论】:

      猜你喜欢
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 2013-05-20
      • 2013-03-07
      • 2021-03-20
      • 2013-04-07
      相关资源
      最近更新 更多