As I mentioned in my post a week ago, I’m commuting each day and there is a 200MB Quota on the Wireless Network. Luckily it’s based on the MAC Address of the WiFi Card, so it’s quite easy to get another 200MB Quota if you want 😉
Here is my small powershell script that automatically Releases the IP Address, set’s a new random MAC Address and Re-Connects to the SSID, all done in a second or two.
Yay! Another 200MB Quota to burn.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
function random-mac { $mac = "02" while ($mac.length -lt 12) { $mac += "{0:X}" -f $(get-random -min 0 -max 16) } $Delimiter = "-" for ($i = 0 ; $i -le 10 ; $i += 2) { $newmac += $mac.substring($i,2) + $Delimiter } $setmac = $newmac.substring(0,$($newmac.length - $Delimiter.length)) $setmac } function disconnect-wifi { $CurrentSSID = (& netsh wlan show profiles | Select-String 'Current User Profile' | Foreach-Object {$_.ToString()}).replace(" Current User Profile : ","$null") Write-Host "Current WLAN SSID: $CurrentSSID" -ForegroundColor Yellow $WIFI = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} Write-Host "Releasing IP addresses:" ($WIFI.IPAddress | select -first 1) -ForegroundColor Yellow $WIFI.ReleaseDHCPLease() | out-Null # Make sure the Release have happened, else it give it 2 sec extra. $WIFI = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} if ($WIFI.DefaultIPGateway -ne $Null) { Write-Output "Release of IP Address had not completed, waiting 1 Seconds" sleep -Seconds 2 } Write-Host "Disconnecting from WiFi" -ForegroundColor Yellow & netsh wlan disconnect | Out-Null } function new-wifimac ($wifiadapter, $ssid, $newmac){ # Write-Output "Wifi AdapterName: $wifiadapter" # Write-Output "SSID: $ssid" # Write-Output "New MAC Address to set: $newmac" $oldmac = (Get-NetAdapter -Name $wifiadapter).MACAddress Write-Output "OLD MAC Address: $oldmac" if ($oldmac -like $newmac) { Write-Host "Old MAC and New MAC are identical, generating a new MAC Address" -ForegroundColor Red $newmac = random-mac Write-Output "New MAC Address to set: $newmac" } Get-NetAdapter -Name $wifiadapter | Set-NetAdapter -MACAddress $newmac -Confirm:$false Get-NetAdapter -Name $wifiadapter | Disable-NetAdapter -Confirm:$false Get-NetAdapter -Name $wifiadapter | Enable-NetAdapter -Confirm:$false $currentmac = (Get-NetAdapter -Name $wifiadapter).MACAddress Write-Output "NEW MAC Address: $currentmac" Write-Host "Connecting to SSID: $ssid" -ForegroundColor Yellow & netsh wlan connect name=$ssid ssid=$ssid $NoIP = 0 Do { $WIFI = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} if ($WIFI.DefaultIPGateway -ne $null) { $NoIP = 5 } else { sleep -Seconds 2 Write-Host "Waiting for IP Address" $NoIP += 1 } } While ($NoIP -lt 5) Write-Host "New IP addresses" ($WIFI.IPAddress | select -first 1) -ForegroundColor Yellow } function test-wifi ($probe){ if (Test-NetConnection -ComputerName $probe -CommonTCPPort HTTP -InformationLevel Quiet) { $result = "Working" } else { $result = "NotWorking" } $result } # Specify $SSID manually # $ssid = 'SSID-to-Connect-to' # # Or use the currently used SSID to reconnect to. $ssid = (& netsh wlan show interfaces | Select-String ' SSID ' | Foreach-Object {$_.ToString()}).replace(" SSID : ","$null") # Specify WLAN Adapter Name Manually # $wifiadapter = 'vEthernet (External Wi-Fi)' # # Or Try to identify the Wi-Fi Adapter $wifiadapter = (Get-NetAdapter | where Status -EQ "Up" | where MediaType -EQ "802.3" | where MacAddress -EQ (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | select *).MACAddress.replace(":","-")).Name # Specify a MAC Address manually # $newmac = "02-F4-D7-B2-FE-D8" # # Or generate a new Random MAC Address $newmac = random-mac disconnect-wifi new-wifimac -wifiadapter $wifiadapter -ssid $ssid -newmac $newmac test-wifi -probe www.msftncsi.com |
I’m using a Window 10 client with Hyper-V, and I’ve created a Virtual NIC for the WiFi adapter, that’s why it’s called ‘vEthernet (External Wi-Fi)’. But you should be able to use the script with a normal WiFi Adapter too.
I’m using a Virtual WiFi Adapter, to be able to give my Virtual Machines access to internet also when I’m without a LAN.
Here is the script for creating a Virtual WiFi NIC;
1 2 3 4 5 6 |
Get-WindowsOptionalFeature -Online -FeatureName *hyper-v*all | Enable-WindowsOptionalFeature –Online -NoRestart -Verbose Get-NetAdapter -Name "Wi-Fi" | New-VMSwitch -Name "Uplink Switch WiFi" -Verbose -AllowManagementOS:$false -MinimumBandwidthMode Weight -ComputerName $ENV:COMPUTERNAME sleep -Seconds 5 Add-VMNetworkAdapter -ManagementOS -SwitchName "Uplink Switch WiFi" -Name "External Wi-Fi" -verbose -ComputerName $ENV:COMPUTERNAME Set-VMnetworkAdapter -ManagementOS -Name "External Wi-Fi" -MinimumBandwidthWeight 20 -verbose -ComputerName $ENV:COMPUTERNAME |