Reputation: 175
im trying to get the mac address table from a switch with plink (putty)
i got this code:
$psi = New-Object System.Diagnostics.ProcessStartInfo
...
$plink = [System.Diagnostics.Process]::Start($psi);
...
$output = $plink.StandardOutput.ReadToEnd()
i got the mac-table as string output (table) like:
[1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1H192-168-1-110--2930-48POE# [24;1H[24;28H[24;
1H[?25h[24;28H[1;0H[1M[24;1H[1L[24;28H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H[1;24r[
24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1H192-168-1-110--2930-48POE# [24;1H[24;28H[24;1H[?25h
[24;28H[1;0H[1M[24;1H[1L[24;28H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H[1;24r[24;1H[24
;1H[2K[24;1H[?25h[24;1H[24;1H192-168-1-110--2930-48POE# [24;1H[24;28H[24;1H[?25h[24;28H[
1;0H[1M[24;1H[1L[24;28H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H[1;24r[24;1H[24;1H[2K[
24;1H[?25h[24;1H[24;1H192-168-1-110--2930-48POE# [24;1H[24;28H[24;1H[?25h[24;28H[24;28Hsho
w mac-a[24;28H[?25h[24;38H[24;38Hddress[24;38H[?25h[24;44H[1;0H[1M[24;1H[1L[24;44H[24;
1H[2K[24;1H[?25h[24;1H[1;24r[24;1H
Status and Counters - Port Address Table
MAC Address Port VLAN
----------------- ------------------------------- ----
000f23-b92gc3 Trk1 1
....
Is it possible to get the mac-table as array or at least as raw string (only the table)
Upvotes: 0
Views: 56
Reputation: 440677
Use a switch
statement:
$dataLinesReached = $false
$propNames = 'MacAddress', 'Port', 'Vlan'
switch -Regex ($output.TrimEnd() -split '\r?\n') {
'^\s*-----' { $dataLinesReached = $true; continue }
default {
if (-not $dataLinesReached) { continue }
$aux = [ordered] @{} # aux. ordered hashtable for collecting key-value pairs
$fields = -split $_ # split by whitespace
# Populate the hashtable based on the field values.
foreach ($i in 0..($propNames.Count-1)) { $aux[$propNames[$i]] = $fields[$i] }
[pscustomobject] $aux # convert to [pscustomobject] and output
}
}
This yields [pscustomobject]
instances with .MacAddress
, .Port
and .Vlan
properties; sample display output:
MacAddress Port Vlan
---------- ---- ----
000f23-b92gc3 Trk1 1
Upvotes: 1