ForEach循环的输出列表

时间:2018-08-22 15:28:27

标签: powershell powershell-v4.0

我有一个功能,可以从给定商店中的所有AP获取数据。

Function Get-AllAP {
Write-Verbose "Function Start: Get-AllAP"
Write-Host "Getting all Access Points in Store $Store .."
Write-Host " "
Write-Verbose "Getting all APs for Store $Store"
$storeApReq = "https://cpist/webacs/api/v3/data/AccessPointDetails.json?.group=$Store"
Write-Verbose "Making request to $storeApReq"
$Global:apIdListReq = Invoke-RestMethod -uri $storeApReq -method Get -ContentType 'application/json' -headers @{ Authorization = $auth }
$Global:apIdList =  $apIdListReq.queryResponse.entityId
$Global:apIdCount =  $apIdListReq.queryResponse."@count"

Write-Verbose "Found $siteAPCount APs in Sites Database. $apIdCount out of $siteAPCount APs found."
Write-Verbose "Response Received: $apIdList"
$Global:apIdURL =  $apIdListReq.queryResponse.entityId
$Global:apURLs = $apIdListReq.queryResponse.entityId | ForEach-Object -MemberName '@url'
Write-Verbose "Looping through APs."

$Global:apLoop = ForEach($apURL in $apURLs) {
$apFullReq = Invoke-RestMethod -uri $apURL'.json' -Method Get -ContentType 'application/json' -headers @{ Authorization = $auth }
Write-Verbose "Response: $apFullReq"

$Global:allApData = $apFullReq.queryResponse.entity.accessPointDetailsDTO



## Format ALL AP data
$apStatus =$allApData.status
$apName = $allApData.name
$apPing = $allApData.reachabilitystatus

## Format data

Write-Host $apName
Write-Verbose $apStatus
Write-Verbose $apPing

## Output data
Write-Host " "
Write-Host "AP Name: $apName"
Write-Host "AP Status: $apStatus"
Write-Host "AP Ping: $apPing"



}

## Clear 
## Give option to reset AP
$doReset = Read-Host "Type 'reset' to reset all Access Points in Store $Store"

IF($doReset-eq 'reset') {
Manage-APResetStore
} else {
Repeat
}

运行此功能后,可以如下选择用户Repeat或使用Manage-APResetStore进行重置;

Function Manage-APResetStore {
Write-Verbose "Function started: Manage-APResetStore"
## Create our batch job
ForEach($apName in $allAPData) {
Write-Host $apName
}

## Send our job
}

这是当前输出的内容,显示了$allAPData而不是$apName的完整JSON。

VERBOSE: Function started: Manage-APResetStore
Establishing SSH connection to Cisco Controller dc1flexwlc02
@{@displayName=16162527426; @id=16162527426; adminStatus=ENABLE; apType=AP3500I; cdpNeighbors=; clientCount=3; clientC
ount_2_4GHz=0; clientCount_5GHz=3; ethernetMac=50:57:a8:a1:7c:38; ipAddress=10.0.126.69; locationHierarchy=Dicks Sport
ing Goods > 0026 > 1st floor; macAddress=5c:50:15:1c:43:30; mapLocation=default location; model=AIR-CAP3502I-A-K9; nam
e=0026AP5; reachabilityStatus=REACHABLE; serialNumber=FTX1611E55H; softwareVersion=8.5.131.0; status=CLEARED; type=Uni
fiedAp; unifiedApInfo=; upTime=68916194}
VERBOSE: Function start: Check-DevMode
VERBOSE: Function Start: Execute-Application

Manage-APResetStore函数的作用是为该存储中存在的每个AP向控制器发送“重置”命令列表。因此,我想做的是在ForEach循环中列出一个列表,我可以使用该列表来生成要发送给控制器的批处理作业。

2 个答案:

答案 0 :(得分:0)

为了能够在格式化数据之后再次循环,我创建了一个仅包含apName对象的新数组,以便我们可以在其他函数中使用它。

在循环外创建数组:

## Create array object
$apArray = New-Object System.Collections.ArrayList

apName对象添加到数组:

## Format ALL AP data
$apStatus =$allApData.status
$apName = $allApData.name
$apPing = $allApData.reachabilitystatus

## Format data

Write-Output $apName
Write-Output $apStatus
Write-Output $apPing

## Output data to array
$apArray.Add($apName)
}

Write-Host $apArray
## Clear 
## Give option to reset AP
$doReset = Read-Host "Type 'reset' to reset all Access Points in Store $Store"

使用数组:

## Loop through our commands
ForEach($apName in $apArray) {
$stream.WriteLine("show loginsessions $apName")
sleep 1
$stream.WriteLine('y')
sleep 2
$stream.Read()


Write-Host "$apName has been reset"
}

答案 1 :(得分:0)

尝试如下替换您的ForEach部分:

ForEach ($apName in $allApData.ApName)

这将意味着循环中的$ apName只是apName属性,而不是$ allApData的整个部分。