I am looking to grab the line with the word hostname out of a file. The files I am searching thru are logs from different network switches. I want to pull the host name out of each log, then I want to return each port name, and crc error for the port.
Here is the code and the log sample is below the code. I bolded the lines out i am pulling out of the log files.
# This pulls the port numbers out of each file
[string[]]$port_counter = Select-String -Path C:\Users\tlyle\OneDrive\Documents\Log\*.txt -Pattern 'is up', 'is down'
# this pulls the crc errors out of each file
[string[]]$crc_logs = Select-String -Path C:\Users\tlyle\OneDrive\Documents\Log\*.txt -Pattern 'crc'
# Below I combine the arrays and print them out
$MaxLen = [Math]::Max($port_counter.Length, $crc_logs.Length)
$Result = @()
for ($i = 0; $i -lt $MaxLen; $i++) {
$Result += $port_counter[$i]
$Result += $crc_logs[$i]
}
$Result
This currently pulls this for me.
:GigabitEthernet1/1/1 is up, line protocol is up
:0 input errors, 0 CRC, 0 frame, 0 ignored
:GigabitEthernet1/1/2 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 ignored
then repeats for each file in the folder.
Here is what the input file looks like - Each file has 48 ports - GigabitEthernet1/1/1 to 1/1/48 - I pull 2 lines out for each port in the file- In bold below - and do this for each file in the directory.. I want to pull the first line of the file and return it before the port and crc information -- For each file-- this will show the host name first then the ports and crc errors then go to the next file and show the host-name once, then ports, and crc errors. .
GigabitEthernet1/1/1 is up, line protocol is up
Port up for 490 day(s) 3 hour(s) 48 minute(s) 56 second(s)
Hardware is GigabitEthernet, address is 609c.9f77.2d44 (bia 609c.9f77.2d44)
Configured speed auto, actual 1Gbit, configured duplex fdx, actual fdx
Configured mdi mode AUTO, actual MDI
EEE Feature Disabled
Member of L2 VLAN ID 281, port is untagged, port state is FORWARDING
BPDU guard is Enabled, ROOT protect is Disabled, Designated protect is Disabled
Link Error Dampening is Enabled
STP configured to ON, priority is level0, mac-learning is enabled
Loop Detection is ENABLED
Flow Control is config enabled, oper enabled, negotiation disabled
Mirror disabled, Monitor disabled
Mac-notification is disabled
Not member of any active trunks
Not member of any configured trunks
Port name is dev-wap
IPG MII 0 bits-time, IPG GMII 0 bits-time
MTU 10200 bytes
300 second input rate: 21600 bits/sec, 13 packets/sec, 0.00% utilization
300 second output rate: 126592 bits/sec, 17 packets/sec, 0.01% utilization
1078349 packets input, 213724458 bytes, 0 no buffer
Received 0 broadcasts, 3719 multicasts, 1074630 unicasts
0 input errors, 0 CRC, 0 frame, 0 ignored
0 runts, 0 giants
1884404 packets output, 2300283173 bytes, 0 underruns
Transmitted 85 broadcasts, 138932 multicasts, 1745387 unicasts
0 output errors, 0 collisions
Relay Agent Information option: Disabled
UC Egress queues:
Queue counters Queued packets Dropped Packets
0 1733399 0
1 1381 0
2 14 0
3 6245 0
4 1194 0
5 7 0
6 105 0
7 58847 0
MC Egress queues:
Queue counters Queued packets Dropped Packets
0 82570 0
1 25 0
2 595 0
3 23 0
GigabitEthernet1/1/2 is up, line protocol is up
Port up for 473 day(s) 9 hour(s) 17 minute(s) 50 second(s)
Hardware is GigabitEthernet, address is 609c.9f77.2d45 (bia 609c.9f77.2d45)
Configured speed auto, actual 1Gbit, configured duplex fdx, actual fdx
Configured mdi mode AUTO, actual MDIX
EEE Feature Disabled
Member of L2 VLAN ID 281, port is untagged, port state is FORWARDING
BPDU guard is Enabled, ROOT protect is Disabled, Designated protect is Disabled
Link Error Dampening is Enabled
STP configured to ON, priority is level0, mac-learning is enabled
Loop Detection is ENABLED
Flow Control is config enabled, oper enabled, negotiation disabled
Mirror disabled, Monitor disabled
Mac-notification is disabled
Not member of any active trunks
Not member of any configured trunks
Port name is dev-wap
IPG MII 0 bits-time, IPG GMII 0 bits-time
MTU 10200 bytes
300 second input rate: 4704 bits/sec, 2 packets/sec, 0.00% utilization
300 second output rate: 5320 bits/sec, 3 packets/sec, 0.00% utilization
170203 packets input, 37873738 bytes, 0 no buffer
Received 0 broadcasts, 3720 multicasts, 166483 unicasts
0 input errors, 0 CRC, 0 frame, 0 ignored
0 runts, 0 giants
365548 packets output, 160575794 bytes, 0 underruns
Transmitted 85 broadcasts, 138931 multicasts, 226532 unicasts
0 output errors, 0 collisions
Relay Agent Information option: Disabled
UC Egress queues:
Queue counters Queued packets Dropped Packets
0 220453 0
1 83 0
2 1 0
3 641 0
4 973 0
5 3717 0
6 46 0
7 56422 0
MC Egress queues:
Queue counters Queued packets Dropped Packets
0 82569 0
1 25 0
2 595 0
3 23 0
HERE IS WHAT I WANT THE END RESULT TO BE:
host "10.16.156.76"
:GigabitEthernet1/1/1 is up, line protocol is up
:0 input errors, 0 CRC, 0 frame, 0 ignored
:GigabitEthernet1/1/2 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 ignored
then repeat for each file in the folder.
You are way over complicating this.
You should be able to use RegEx matching or Select-String with a RegEx pattern to just grab the two lines with the text you are after.
I just copied your sample log, saved it as a single txt file and just did the below.
Example:
# Use RegEx matches to match / select the line that contains the
Get-Content -Path 'D:\Temp\Backup\RouterLog.txt' |
%{[regex]::Matches($PSItem,'^.*\b(protocol|CRC)\b.*$').Value}
# Use Select-String RegEx to match / select the line that contains the pattern
Get-Content -Path 'D:\Temp\Backup\RouterLog.txt' |
Select-String '^.*\b(protocol|CRC)\b.*$'
# Results of both is
GigabitEthernet1/1/1 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 ignored
GigabitEthernet1/1/2 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 ignored
Of course you could just ForLoop individual files as you seem to say you are already doing.