CodeSpent
CodeSpent

Reputation: 1904

Incrementing until number is reached

I have a number, lets use 11 as the example value. I have access points that are named (in this instance) 0266AP1, 0266AP2, 0266AP3, 0266AP4 and so forth. 0266 is the site/store number.

By making a call to the Cisco Prime API, I can see that Site 0266 has 11 Access Points. What I want to do to make a real quick list to pass to my controller is increment until I reach 11 or the value of @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
}

I've removed my attempts as they've all kind of come up empty, but I essentially want to use $apIdCount as my stopping point, and 1 as my starting point.

Solution 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
    }
}

Upvotes: 0

Views: 73

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

What you describe is called a for loop:

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

See also this Technet article on loop control structures in PowerShell.

Upvotes: 2

CodeSpent
CodeSpent

Reputation: 1904

After making mention of 1 being my starting point and $apIdCount being my stop point, I realized a simple method of achieving this.

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
    }
}

This outputs exactly what I'm looking for.

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

I'm not sure if this is the best way to handle this, so I'll leave this open for more experienced opinions for now!

Upvotes: 0

Related Questions