We are trying to Pull AP name, bss, and ess values out of text files provided by a network team, in PowerShell 5.1. We have numerous files to go through and multiple AP Targets in each file.
Here's example of Text file. We need to extract the ap name, the ess, and bss values for each ess that matches "trustedwifi" and "guest" items only. The rest of the data is unneeded.
*********************************************************************************************************
9/5/2021 7:42:16 AM Target: AP01 (VC)
*********************************************************************************************************
WIFI AP BSS Table
------------------
bss ess port ip band/ht-mode/bandwidth ch/EIRP/max-EIRP type cur-cl ap name in-t(s) tot-t flags
--- --- ---- -- ---------------------- ---------------- ---- ------ ------- ------- ----- -----
ab:cd:ef:gh:if:jk trustedwifi ?/? A.b.c.d 2.4GHz/HT/20MHz 1/24.0/25.5 ap 0 AP01 0 43d:14h:48m:14s K
ab:cd:ef:gh:if:jk secure ?/? A.b.c.d 2.4GHz/HT/20MHz 1/24.0/25.5 ap 0 AP01 0 43d:14h:47m:53s -
ab:cd:ef:gh:if:jk guest ?/? A.b.c.d 2.4GHz/HT/20MHz 1/24.0/25.5 ap 0 AP01 0 22h:11m:43s -
ab:cd:ef:gh:if:jk trustedwifi ?/? A.b.c.d 5GHz/VHT/40MHz 100+/18.0/30.0 ap 0 AP01 0 43d:14h:48m:15s K
ab:cd:ef:gh:if:jk secure ?/? A.b.c.d 5GHz/VHT/40MHz 100+/18.0/30.0 ap 0 AP01 0 43d:14h:47m:54s -
ab:cd:ef:gh:if:jk guest ?/? A.b.c.d 5GHz/VHT/40MHz 100+/18.0/30.0 ap 0 AP01 0 22h:11m:44s -
Channel followed by "*" indicates channel selected due to unsupported configured channel.
"Spectrum" followed by "^" indicates Local Spectrum Override in effect.
Num APs:6
Num Associations:0
Flags: a = Airslice policy; A = Airslice app monitoring; c = MBO Cellular Data Capable BSS; d = Deferred Delete Pending; D = VLAN Discovered; E = Enhanced-open BSS without transition mode; I = Imminent VAP Down; K = 802.11K Enabled; m = Agile Multiband (MBO) BSS; M = WPA3-SAE mixed mode BSS; o = Enhanced-open transition mode open BSS; O = Enhanced-open BSS with transition mode; r = 802.11r Enabled; t = Broadcast TWT Enabled; T = Individual TWT Enabled; W = 802.11W Enabled; x = MBSSID Tx BSS; 3 = WPA3 BSS;
*********************************************************************************************************
9/5/2021 7:42:18 AM Target: AP02
*********************************************************************************************************
WIFI AP BSS Table
------------------
bss ess port ip band/ht-mode/bandwidth ch/EIRP/max-EIRP type cur-cl ap name in-t(s) tot-t flags
--- --- ---- -- ---------------------- ---------------- ---- ------ ------- ------- ----- -----
ab:cd:ef:gh:if:jk trustedwifi ?/? A.b.c.d 2.4GHz/HT/20MHz 11/24.0/25.5 ap 0 AP02 0 43d:14h:47m:24s K
ab:cd:ef:gh:if:jl secure ?/? A.b.c.d 2.4GHz/HT/20MHz 11/24.0/25.5 ap 0 AP02 0 43d:14h:47m:3s -
ab:cd:ef:gh:if:jm guest ?/? A.b.c.d 2.4GHz/HT/20MHz 11/24.0/25.5 ap 0 AP02 0 22h:11m:43s -
ab:cd:ef:gh:if:j0 rustedwifi ?/? A.b.c.d 5GHz/VHT/40MHz 108+/18.0/30.0 ap 0 AP02 0 43d:14h:47m:24s K
ab:cd:ef:gh:if:jp secure ?/? A.b.c.d 5GHz/VHT/40MHz 108+/18.0/30.0 ap 0 AP02 0 43d:14h:47m:4s -
ab:cd:ef:gh:if:jq guest ?/? A.b.c.d 5GHz/VHT/40MHz 108+/18.0/30.0 ap 0 AP02 0 22h:11m:43s -
Preferred Output would be something like this repeated for each Ap name in the files with ALL records containing trustedwifi and guest. Repeated for every match that includes the two search criteria.
ap name ess bss
AP01 Trustedwifi ab:cd:ef:gh:if:jk
AP01 guest ab:cd:ef:gh:if:jm
AP02 Trustedwifi ab:cd:ef:gh:if:jk
AP02 guest ab:cd:ef:gh:if:jm
.
.
Started with the following to try and solve it, but am stumped on how to get the data extracted and formatted properly to export to a .csv file
# Specify the directory to search
$directory = "C:\Your\Directory"
# Specify the search terms
$searchTerms = "error", "warning", "exception"
# Get all .txt files in the directory
$files = Get-ChildItem -Path $directory -Filter "*.txt"
# Search for each term in each file
foreach ($file in $files) {
foreach ($term in $searchTerms) {
$matches = Select-String -Path $file.FullName -Pattern $term
if ($matches) {
Write-Host "File: $($file.FullName)"
foreach ($match in $matches) {
Write-Host "Line $($match.LineNumber): $($match.Line)"
}
Write-Host ""
}
}
}
But expect to end up with the output example from above.