PoshCode Archive  Artifact Content

Artifact 28ab30b85521b7779000ff501e5c7e431a71d8cce64f5c020b276938fbb02468:

  • File wlanscan-Win10-Support.ps1 — part of check-in [e7fd960cc1] at 2018-06-10 14:18:44 on branch trunk — Simple script that uses netsh to show wireless networks. (user: Kris Cieslak size: 3693)

# encoding: ascii
# api: powershell
# title: wlanscan (Win10 Support)
# description: Simple script that uses netsh to show wireless networks.
# version: 0.1
# type: script
# author: Kris Cieslak 
# license: CC0
# x-poshcode-id: 6636
# x-archived: 2017-04-01T02:43:15
# x-published: 2017-11-26T01:01:00
#
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=
#          Name: wlanscan
#        Author: Kris Cieslak (defaultset.blogspot.com)
#          Date: 2010-04-03
#   Description: Simple script that uses netsh to show wireless networks.
#
#    Parameters: wireless interface name (optional,but recommended if you have
#                more than one card)
#        Result: $ActiveNetworks
# Usage example: wlanscan WiFi
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-
PARAM ($ifname = "")

# Windows Vista/2008/7/10
# RV 2016-11-25 Fixed minor bug where it was treating the version number as text by converting it to int
if  ([int](gwmi win32_operatingsystem).Version.Split(".")[0] -lt 6) {
	throw "This script works on Windows Vista or higher."
}
if ((gsv "wlansvc").Status -ne "Running" ) {
	throw "WLAN AutoConfig service must be running."
}
$GLOBAL:ActiveNetworks = @();
$CurrentIfName = "";	
$n = -1;
$iftest = $false;

netsh wlan show network mode=bssid | % {
	if ( $_ -match "Interface") {
		$CurrentIfName = [regex]::match($_.Replace("Interface name : ","")
			                            ,"\w{1,}").ToString();
	    if (($CurrentIfName.ToLower() -eq $ifname.ToLower()) -or ($ifname.length -eq 0)) {
		    $iftest=$true;
		} else { $iftest=$false }
	}	 
	
	$buf = [regex]::replace($_,"[ ]","");
	if ([regex]::IsMatch($buf,"^SSID\d{1,}(.)*") -and $iftest) {
	   	$item = "" | Select-Object SSID,NetType,Auth,Encryption,BSSID,Signal,Radiotype,Channel;
		$n+=1;
       	$item.SSID = [regex]::Replace($buf,"^SSID\d{1,}:","");
		$GLOBAL:ActiveNetworks+=$item;
	}
  	if ([regex]::IsMatch($buf,"Networktype") -and $iftest) {
	   	$GLOBAL:ActiveNetworks[$n].NetType=$buf.Replace("Networktype:","");
	}
	if ([regex]::IsMatch($buf,"Authentication") -and $iftest) {
	   	$GLOBAL:ActiveNetworks[$n].Auth=$buf.Replace("Authentication:","");
	}
	if ([regex]::IsMatch($buf,"Encryption") -and $iftest) {
	   	$GLOBAL:ActiveNetworks[$n].Encryption=$buf.Replace("Encryption:","");
	 	}
        if ([regex]::IsMatch($buf,"BSSID1") -and $iftest) {
	   	$GLOBAL:ActiveNetworks[$n].BSSID=$buf.Replace("BSSID1:","");
	}
	if ([regex]::IsMatch($buf,"Signal") -and $iftest) {
	   	$GLOBAL:ActiveNetworks[$n].Signal=$buf.Replace("Signal:","");
	}
	if ([regex]::IsMatch($buf,"Radiotype") -and $iftest) {
	   	$GLOBAL:ActiveNetworks[$n].Radiotype=$buf.Replace("Radiotype:","");
	}
	if ([regex]::IsMatch($buf,"Channel") -and $iftest) {
	  	$GLOBAL:ActiveNetworks[$n].Channel=$buf.Replace("Channel:","");
	}
}
if ( ($CurrentIfName.ToLower() -eq $ifname.ToLower()) -or ($ifname.length -eq 0) ) {
	write-host -ForegroundColor Yellow "`nInterface: "$CurrentIfName;
	if (($GLOBAL:ActiveNetworks.length -gt 0)) {
   		$GLOBAL:ActiveNetworks | Sort-Object Signal -Descending | 
			ft @{Label = "BSSID"; Expression={$_.BSSID };width=18},
               @{Label = "Channel"; Expression={$_.Channel};width=8},
			   @{Label = "Signal"; Expression={$_.Signal};width=7},
			   @{Label = "Encryption"; Expression={$_.Encryption};width=11},
   			   @{Label = "Authentication"; Expression={$_.Auth};width=15},
			   SSID
	} else {
	   Write-host "`n No active networks found.`n";
	}
} else {
  Write-host -ForegroundColor Red "`n Could not find interface: "$ifname"`n";
}