-
Notifications
You must be signed in to change notification settings - Fork 0
/
WifiHotspot.ps1
216 lines (189 loc) · 7.5 KB
/
WifiHotspot.ps1
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<#
.SYNOPSIS
Script to turn your PC's Wifi adapter into a personal hotspot that shares your PC's Internet connection.
This allows you to connect additional WIFI devices to your network without requiring a wireless router
or access point. Basically, your PC will act as the WIFI router :-)
This is handy when your only connection point is wired and you want to also connect a WIFI device such
as your mobile phone or tablet, or when depite having WIFI available, the network only allows one device
per user.
These are common scenarios when on the move and stying in hotels.
NOTE: This functionality is only supported on Win 7 and later, and only by certain Wifi adapters.
The script will check if your adapter is supported.
.EXAMPLE
WifiHotspot start
WifiHotspot stop
WifiHotspot show
.AUTHOR
donhector
.VERSION
1.0
.CHANGELOG
1.0 - 2013/02/04 Initial release
#>
Param
(
[Parameter(Position = 0)]
[ValidateSet("start","stop", "show")]
[string]
$command = "start"
)
###############
# Functions
###############
Function CheckAdminCredentials
{
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if(-not($isAdmin))
{
Write-Warning "This script requires administrator rights to run."
Write-Warning "You can right click on the script and select 'Run as Administrator...'"
Write-Warning "Alternatively start a PowerShell console as Administrator and launch the script from there."
Exit
}
}
Function CheckAdapterSupport
{
# Checking if the adapter supports hosted network
Write-Host "Checking if your Wifi adapter supports hotspot mode... " -nonewline
$supported = netsh wlan show drivers | Select-String "Hosted network supported" | Select-String "Yes"
if (!$supported) { Write-Host "Unsupported!" -foregroundcolor red; Exit;}
Write-Host "Supported!" -foregroundcolor green
}
Function ConfigureHostedNetwork
{
# Configuring the hosted network
Write-Host "Enter hotspot details: SSID (username field) and encryption key (password field)"
Try { $credentials = Get-Credential -credential "MyWifiHotspot" }
Catch { Write-Host "Error: Hotspot details window cancelled by user!" -foregroundcolor red; Exit;}
$ssid = $credentials.GetNetworkCredential().UserName
$key = $credentials.GetNetworkCredential().Password
if (!$ssid -or ($ssid.Length -le 2)) { Write-Host "Error: Invalid SSID length. It should be 3 or more characters long!" -foregroundcolor red; Exit;}
if (!$key -or ($key.Length -le 7)) { Write-Host "Error: Invalid key lenght. It should be 8 or more characters long!" -foregroundcolor red; Exit;}
Write-Host "Hotspot details ok!`n" -foregroundcolor green
Write-Host "Configuring hosted network..."
netsh wlan set hostednetwork mode=allow ssid=$ssid key=$key keyUsage=persistent
}
Function GetNetworkAdapterList
{
# Get a list of available Adapters
$hnet = New-Object -ComObject HNetCfg.HNetShare
$netAdapters = @()
foreach ($i in $hnet.EnumEveryConnection)
{
$netconprop = $hnet.NetConnectionProps($i)
$inetconf = $hnet.INetSharingConfigurationForINetConnection($i)
$netAdapters += New-Object PSObject -Property @{
Index = $index
Guid = $netconprop.Guid
Name = $netconprop.Name
DeviceName = $netconprop.DeviceName
Status = $netconprop.Status
MediaType = $netconprop.MediaType
Characteristics = $netconprop.Characteristics
SharingEnabled = $inetconf.SharingEnabled
SharingConnectionType = $inetconf.SharingConnectionType
InternetFirewallEnabled = $inetconf.InternetFirewallEnabled
SharingConfigurationObject = $inetconf
}
$index++
}
return $netAdapters
}
Function GetPublicAdapter($netAdapterListParam)
{
# Get the adapter that is currently connected (status = 2) to the Internet so it can share its connection
# This adapter will be used as the public network connection when setting up ICS.
# As there is no easy way to get which adapter is providing
# Internet connectivity (there could be more than 1) we ask the user.
Write-host
$indexList = @()
$netAdapterListParam | where { $_.Status -eq '2'} | % {$indexList += $_.Index}
$filteredList = $netAdapterListParam | where { $_.Status -eq '2'}
$formatted = $filteredList | fl Name, DeviceName, Index | Out-String
Write-Host "The following network adapters seem to be enabled on the system:"
Write-Host $formatted
$userIndex = Read-Host "Enter the index of the adapter that is providing you Internet connection"
While ($indexList -notcontains $userIndex)
{
Write-Warning "Provided index is invalid! Choose one from the displayed list."
$userIndex = Read-Host "Enter the index of the adapter that is providing you Internet connection"
}
$publicAdapter = $netAdapterListParam[$userIndex]
return $publicAdapter
}
Function GetPrivateAdapter($netAdapterListParam)
{
# Get the adapter that will act as the hotspot (i.e. the Virtual Wifi Mini Port adapter)
# This will be our private network connection when setting up Internet Connection Sharing (ICS).
[string]$virtualWifiAdapterGUID = Get-WMIObject win32_networkadapterconfiguration | where {$_.ServiceName -eq "vwifimp"} | % {$_.GetRelated('win32_networkadapter')} | Select -ExpandProperty Guid
# Now get the adapter that matches that GUID from our list of adapters.
$privateAdapter = $netAdapterListParam | where { $_.Guid -eq $virtualWifiAdapterGUID}
return $privateAdapter
}
Function StartHostedNetwork
{
Write-Host "Starting hosted network... " -nonewline
netsh wlan start hostednetwork | out-null
Write-Host "Started!" -foregroundcolor green
}
Function StopHostedNetwork
{
Write-Host "Stopping hosted network... " -nonewline
netsh wlan set hostednetwork mode=disallow | out-null
netsh wlan stop hostednetwork | out-null
Write-Host "Stopped!" -foregroundcolor red
}
Function StartICS($publicAdapter, $privateAdapter)
{
$public = 0
$private = 1
# Enabling Internet Connection Sharing (ICS) on the public connection via the the internal private adapter
Write-Host "Starting ICS on the selected adapter... " -nonewline
If($publicAdapter.SharingEnabled -eq $false) { $publicAdapter.SharingConfigurationObject.EnableSharing($public) }
If($privateAdapter.SharingEnabled -eq $false) { $privateAdapter.SharingConfigurationObject.EnableSharing($private) }
Write-Host "Started!" -foregroundcolor green
}
Function StopICS ($adapterList)
{
# Disabling Internet Connection Sharing (ICS) on ALL adapters
Write-Host "Stopping ICS... " -nonewline
ForEach ($adapter in $adapterList)
{
If($adapter.SharingEnabled -eq $true)
{
$adapter.SharingConfigurationObject.DisableSharing()
#Write-Host "`t-> Disabled sharing on '$($adapter.Name)'" -foregroundcolor green
}
}
Write-Host "Stopped!" -foregroundcolor red
}
###############
# Code Logic
###############
Write-Host "#############################"
Write-Host " Wifi Hotspot script "
Write-Host "#############################"
if ($command -eq "show")
{
netsh wlan show hostednetwork
}
elseif ($command -eq "stop")
{
CheckAdminCredentials
$netAdapterList = GetNetworkAdapterList
StopICS $netAdapterList
StopHostedNetwork
}
else
{
CheckAdminCredentials
CheckAdapterSupport
ConfigureHostedNetwork
$netAdapterList = GetNetworkAdapterList
StartHostedNetwork
$publicAdapter = GetPublicAdapter $netAdapterList # The adapter with Inet connection
$privateAdapter = GetPrivateAdapter $netAdapterList # The virtual wifi adapter that creates the hotspot
StartICS $publicAdapter $privateAdapter
Write-Host "Hotspot setup is now complete."
Write-Host "Kindly allow a couple of minutes for the hostspot to show up :-)"
}