递增直到达到数字

问题描述 投票:0回答:2

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

通过拨打Cisco Prime API,我可以看到Site 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
2个回答
2
投票

你描述的是一个for循环:

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

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


0
投票

在提到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

我不确定这是否是解决这个问题的最佳方式,所以我现在就把它留给更有经验的意见吧!

© www.soinside.com 2019 - 2024. All rights reserved.