【问题标题】:Output list from ForEach LoopForEach 循环的输出列表
【发布时间】:2018-08-23 05:09:15
【问题描述】:

我有一个函数可以从给定商店中的所有 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 循环中列出一个列表,我可以用它来生成一个批处理作业发送到控制器。

【问题讨论】:

    标签: powershell powershell-4.0


    【解决方案1】:

    为了能够在数据格式化后再次循环,我创建了一个仅包含 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"
    }
    

    【讨论】:

      【解决方案2】:

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

      ForEach ($apName in $allApData.ApName)
      

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

      【讨论】:

        猜你喜欢
        • 2022-01-09
        • 2020-04-15
        • 2013-07-24
        • 1970-01-01
        • 1970-01-01
        • 2021-06-01
        • 2016-02-15
        • 2013-06-04
        • 2017-02-18
        相关资源
        最近更新 更多