I cannot convert this simple string to a psobject using ConvertFrom-String.
What am I missing?
[pre]
$test_string = @’
0025-b3f9-3053 1 LEARNED GigabitEthernet1/0/46 AGING
18a9-05fd-2e15 1 LEARNED GigabitEthernet1/0/40 AGING
18a9-05fe-e65a 1 LEARNED GigabitEthernet1/0/33 AGING
5cb9-01af-b4f6 1 LEARNED GigabitEthernet1/0/17 AGING
'@
$mac_list = $test_string | ConvertFrom-String -PropertyNames MAC,VLAN,TYPE,PORT,AGE
$mac_list | Format-Table -AutoSize
[/pre]
Result:
MAC VLAN TYPE PORT AGE P6 P7 P8 P9 P10
0025-b3f9-3053 1 LEARNED GigabitEthernet1/0/46 AGING 18a9-05fd-2e15 1 LEARNED GigabitEthernet1/0/40 AGING
That’s because $test_string is one big string. You’d need to break up each line and pass them individually. A “here string” is still a single string.
[pre]$test_string = @"
0025-b3f9-3053 1 LEARNED GigabitEthernet1/0/46 AGING
18a9-05fd-2e15 1 LEARNED GigabitEthernet1/0/40 AGING
18a9-05fe-e65a 1 LEARNED GigabitEthernet1/0/33 AGING
5cb9-01af-b4f6 1 LEARNED GigabitEthernet1/0/17 AGING
"@
$Entries = $test_string.Split([Environment]::NewLine)
foreach($Entry in $Entries)
{
$mac_list = $Entry | ConvertFrom-String -PropertyNames MAC,VLAN,TYPE,PORT,AGE
$mac_list | Format-Table -AutoSize
}[/pre]
Olaf
March 31, 2019, 3:34pm
3
Could it be that what you’re actually looking for?
$test_string -split “`r`n” |
ConvertFrom-Csv -Delimiter " " -Header MAC, VLAN, TYPE, PORT, AGE |
Format-Table -AutoSize
Thanks for the tips, I had to modify it a bit to make it work:
[pre]
$final_list = ($test_string -split "`r`n" -replace '\s{1,}', ',' | ConvertFrom-Csv -Delimiter "," -Header MAC, VLAN, TYPE, PORT, AGE)
$final_list | Format-table -AutoSize
[/pre]
I still don't understand why ConvertFrom-String is not working in the first place ... Weird
Olaf
March 31, 2019, 4:46pm
5
Actually it does not matter what convert you use …
$test_string -split "`r`n" |
ConvertFrom-String -Delimiter ' ' -PropertyNames MAC, VLAN, TYPE, PORT, AGE |
Format-Table -AutoSize
… works as well. You just have to split your looooong input string into meaningful little pieces.
BTW: you can simplify your -replace ‘\s{1,}’,‘,’ a tiny little bit to -replace ‘\s+ ’,‘,’