Need to find your switch port? Maybe you’re experiencing network connectivity issues (not ideal) or perhaps you have a compromised device generating too much traffic to your network (not great either). Maybe you just want to know that you’re connected to the right VLAN (we’ve got our fingers crossed).
Way back when your grandparents were walking an hour to school in the snow, troubleshooting networking issues meant tracing the physical cables from device to port. We’re all about you getting your steps in, but with this easy-to-use script, you can save your workouts for the gym!
This script will show you which switch port your network cables are connected to without having to move an inch. First, the PSDiscoveryProtocol module intercepts the network packets (small segments of data sent over a network connection) on local or remote computers. The script then extracts the following information stored in the packets: model number, description, VLAN, port, device, IP Address, computer, and packet type.
Once you have this data, it’s time to start troubleshooting. If the port itself is corrupted, you can physically change the port your device is connected to. If you’re connected to an unexpected VLAN, change the settings of the switch via the associated web user interface.
The Script:
Atera does not guarantee the integrity, availability, security, virus-free, safety, lawfulness, non-infringement, rights’ status, or functionality of the scripts. The use of the shared scripts is at your own risk. Scripts are provided “AS IS”. *
###
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Module -Name PSDiscoveryProtocol
$Packet = Invoke-DiscoveryProtocolCapture -Type LLDP
Get-DiscoveryProtocolData -Packet $Packet
###
How to fix a switch port failure?
The fix for a switch port failure depends on whether you’re having problems with the network switch hardware, the device itself, or the port configurations. But first things first, you need to find your switch port! Use this script to determine which switch port your device is connected to and then troubleshoot by determining whether the port itself isn’t working, the ethernet cable needs to be replaced, or whether you need to change your switch configurations.
How do I know if I am connected to the right VLAN?
Lucky for you, the same script that finds your port switch also returns the VLAN your device is connected to. If you are having networking issues and suspect that perhaps your device is on the wrong VLAN, you can run this script to find out. If it turns out that you need to update your VLAN, this can be done by accessing the web user interface of your network switch and changing your VLAN settings.
Technical Notes:
- Admin permissions needed to run this script as it installs a PowerShell module.
- The network packets used in this script are the CDP (Cisco Discovery Protocol) and LLDP (Link Layer Discovery Protocol packets).
- This script can capture packets simultaneously on multiple computers using the new -Parallel parameter on ForEach-Object. Note that this requires PowerShell version 7. To gather switch port details for multiple computers simultaneously, replace lines 3 and 4 in the script with the following:
COMPUTER1′, ‘COMPUTER2’, ‘COMPUTER3’ | ForEach-Object -Parallel { Invoke-DiscoveryProtocolCapture -ComputerName $_ | Get-DiscoveryProtocolData
} - The PSDiscoveryProtocol command does not return all information available in CDP and LLDP packets. To extract and use all the information available in the packets, you can use the command, Export-Pcap, and open the pcap file in Wireshark or a similar tool. To do this, replace line 4 with Export-Pcap -Path packets.pcap
Explore our shared script library
The all-in-one IT management platform that will take your IT operations to the next level!