Created
April 15, 2020 00:16
-
-
Save zemerick1/86723067223cb6fad0eacad405ce49a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Probes a set or range of VLANs for an IP Address | |
.DESCRIPTION | |
Probes a set or range of VLANs to see if the port you're connected to allows traffic for that particular VLAN. | |
.PARAMETER Vlans | |
Specify a range of VLANs in Powershell range format (n..m), or a comma-delimited list. | |
.PARAMETER Interface | |
Interface name of your Ethernet adapter. | |
.NOTES | |
This Prober is not particularly fast (4s/VLAN) because to effectively test if you can receive an IP on the VLAN | |
the interface must restart. I've tackled this the best way that I know how by adding a sleep timer tied | |
to the interface status. | |
Also, you may have to change the name of the VLAN ID property as it can differ from NIC driver to NIC driver. | |
Get-NetAdapterAdvancedProperty -AllProperties -Name "Ethernet 21" | Format-List | |
#> | |
# Interface Name as shown by IPCONFIG | |
[string]$Interface = "Ethernet 21" | |
# Enter comma delimited list of VLANs, OR a range using (1..20) (20..199) etc | |
[array]$Vlans = 1,2,300,199 | |
## Build Necessary variables for reuse | |
$NetAdapter = Get-NetAdapter -Name $Interface | |
$CurrentVlanID = (Get-NetAdapter -Name $Interface).VlanID | |
## This may be hit or miss. Tested on NIC: ASIX AX88179 USB 3.0 to Gigabit | |
$VlanProperty = (Get-NetAdapterAdvancedProperty -AllProperties -Name $Interface | Where-Object DisplayName -Like "VLAN*").DisplayName | |
## By default Windows returns this as NULL value if not set explicitly | |
if (!$CurrentVlanID) { [int]$CurrentVlanID = 0 } | |
## More variables to use later | |
[array]$VlanObjects = @() | |
## Count of VLANs that you specified so we can loop through them | |
[int]$VlanCount = $Vlans.Count | |
## Loop counter | |
[int]$c = 0 | |
while ($c -lt $VlanCount) { | |
echo "Trying VLAN: $($Vlans[$c])" | |
## Set the VLAN of the NIC. | |
Set-NetAdapterAdvancedProperty -Name $Interface -DisplayName $VlanProperty -DisplayValue $Vlans[$c] | |
## Grab status of the NIC | |
$Status = (Get-NetAdapter -Name $Interface -ErrorAction SilentlyContinue).Status | |
## Keep checking the status of the NIC so we can continue as soon as possible. | |
## It would be perhaps better, to check if the NIC is up and there is an IP available. | |
while (!$Status -or $Status -eq 'Disconnected') { | |
Sleep 1 | |
$Status = (Get-NetAdapter -Name $Interface -ErrorAction SilentlyContinue).Status | |
} | |
## Grab the IP of the NIC | |
$TestIP = (Get-NetIPAddress -InterfaceAlias $Interface -ErrorAction SilentlyContinue).IPAddress | |
## Test if the NIC has an APIPA address. If so, it fails. Build objects to prettify output. | |
if ($TestIP -match '169.254' -or !$TestIP) { | |
$VlanObj = [PSCustomObject]@{ | |
Status = 'Fail' | |
IP = $TestIP | |
VLAN = $Vlans[$c] | |
} | |
$VlanObjects += $VlanObj | |
} | |
else { | |
$VlanObj = [PSCustomObject]@{ | |
Status = 'Success' | |
IP = $TestIP | |
VLAN = $Vlans[$c] | |
} | |
$VlanObjects += $VlanObj | |
} | |
$c++ | |
} | |
echo "Setting VLAN back to $CurrentVlanID" | |
## Output prettified output | |
echo $VlanObjects | |
## Reset VLAN of NIC to what it was originally. | |
Set-NetAdapterAdvancedProperty -Name $Interface -DisplayName "VLAN ID" -DisplayValue $CurrentVlanID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment