Tuesday, March 22, 2016

Determine if a computer is connected via Wired or Wireless network (or both)

This is not the first time this has come up for me and it is not a trivial thing to determine.  So here's the PS script to do it. Please give me a thumbs up, a comment, or a link back to this blog if you use it.

There are quite a few lines commented out, they can be uncommented to give you more information about the variables within the script (or you can just delete them if you don't care).  I left them there mostly for myself in case I find a use case that I need to know more about the adapters.
#Get-NetConnectionType.ps1
#by Mark Randol
#randoltech.blogspot.net
cls
#PhysicalAdapter cannot be only criteria because, for no apparent reason, some virtual adapters set this to true.  Especially VPN adapters.
#AdapterType eliminates anything that is not TCP/IP
#NetConnectionStatus of 2 = connected
$AdapterTypes = (Get-WmiObject -Namespace "root/WMI" -Query "SELECT * FROM MSNdis_PhysicalMediumType")
$Adapters = (Get-WmiObject -class win32_networkadapter | Where-Object {$_.PhysicalAdapter -eq "True" -and $_.AdapterType -eq "Ethernet 802.3" -and $_.NetConnectionStatus -eq "2"})
$NetworkConnected = $false
$WiredConnected = $false
$WirelessConnected = $false

#Spit out a header line with how many adapters are connected
$AdaptersCount = ($Adapters | measure).count
if ($AdaptersCount -lt 1)
    {
        $HeaderString = "There are no adapters connected"
    }
if ($AdaptersCount -eq 1)
    {
        $HeaderString = "There is $AdaptersCount adapter connected"
    }
if ($AdaptersCount -gt 1)
    {
        #Two or more connected adapters means one of two things:
        #  a. Both your wired and wireless NICs are currently connected.
        #  b. You have multiple NICs (generally a server like a Virtual Server Host may have multiple NICs on multiple networks simultaneously).
        $HeaderString = "There are $AdaptersCount adapters connected"
    }

#Loop through all of the adapters and for each one check its type to see if it is wired or wireless
#Only return those that are connected and are TCP/IP wired or wireless adapters
foreach ($Adapter in $Adapters)
{
    foreach ($AdapterType in $AdapterTypes)
    {
        if ($AdapterType.InstanceName -eq $Adapter.Name)
        {
            if ($AdapterType.NdisPhysicalMediumType -eq 0)
            {
                #$AdapterType | format-list -Property [a-z]*
                <#write-host ("-------------------------------------------")
                $AdapterSettings = (Get-WmiObject -Class win32_NetworkAdapterSetting | where-object {$_.Element -eq $Adapter.Path})
                $AdapterOutput = $Adapter.ProductName
                $AdapterOutput = $AdapterOutput + ", "
                $AdapterOutput = $AdapterOutput + $Adapter.NetConnectionID
                $AdapterOutput = $AdapterOutput + ", Wired"
                write-host $AdapterOutput
                write-host ("-------------------------------------------")#>
                $NetworkConnected = $true
                $WiredConnected = $true
            }
            if ($AdapterType.NdisPhysicalMediumType -eq 9)
            {
                #$AdapterType | format-list -Property [a-z]*
                <#write-host ("-------------------------------------------")
                $AdapterSettings = (Get-WmiObject -Class win32_NetworkAdapterSetting | where-object {$_.Element -eq $Adapter.Path})
                $AdapterOutput = $Adapter.ProductName
                $AdapterOutput = $AdapterOutput + ", "
                $AdapterOutput = $AdapterOutput + $Adapter.NetConnectionID
                $AdapterOutput = $AdapterOutput + ", Wireless"
                write-host $AdapterOutput
                write-host ("-------------------------------------------")#>
                $NetworkConnected = $true
                $WirelessConnected = $true
            }
        }
    }
}
write-host ("Network connected = $NetworkConnected - $HeaderString")
write-host ("Connected via a wire = $WiredConnected")
write-host ("Connected wirelessly = $WirelessConnected")

No comments:

Post a Comment