-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManage-WirelssNetworks.psm1
113 lines (109 loc) · 3.37 KB
/
Manage-WirelssNetworks.psm1
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
Function Export-WirelessNetworkProfile {
<#
.LINK
https://github.com/si-kotic/Manage-WirelessNetworks
#>
Param (
[Parameter(ValueFromPipeline=$true,Mandatory)]$SSID,
$Path = $PWD,
[Switch]$PlaintextPassword
)
IF ($SSID.GetType().Name -eq "PSCustomObject") {
$ssidName = $SSID.SSID
} ELSEIF ($SSID.GetType().Name -eq "String") {
$SSID = Get-AvailableWirelessNetworks -SSID $SSID
$ssidName = $SSID.SSID
}
IF ($PlaintextPassword) {
netsh wlan export profile name=$ssidName folder="$Path" key=clear
} ELSE {
netsh wlan export profile name=$ssidName folder="$Path"
}
}
Function Import-WirelessNetworkProfile {
<#
.LINK
https://github.com/si-kotic/Manage-WirelessNetworks
#>
Param (
[Parameter(Mandatory)]$Profile
)
netsh wlan add profile filename="$Profile"
}
Function Get-AvailableWirelessNetworks {
<#
.LINK
https://github.com/si-kotic/Manage-WirelessNetworks
#>
Param (
[Parameter(ValueFromPipeline=$true)]$SSID
)
$response = netsh wlan show networks mode=bssid
$wLANs = $response | Where-Object {$_ -match "^SSID"} | Foreach-Object {
$report = "" | Select SSID,NetworkType,Authentication,Encryption
$i = $response.IndexOf($_)
$report.SSID = $_ -replace "^SSID\s\d+\s:\s",""
$report.NetworkType = $response[$i+1].Split(":")[1].Trim()
$report.Authentication = $response[$i+2].Split(":")[1].Trim()
$report.Encryption = $response[$i+3].Split(":")[1].Trim()
$report
}
IF ($SSID) {
$wLANs | Where-Object {$_.SSID -eq $SSID}
} ELSE {
$wLANs
}
}
Function ConnectTo-WirelessNetwork {
<#
.LINK
https://github.com/si-kotic/Manage-WirelessNetworks
#>
Param (
[Parameter(ValueFromPipeline=$true,Mandatory)]$SSID,
$Profile
)
IF ($SSID.GetType().Name -eq "PSCustomObject") {
$ssidName = $SSID.SSID
} ELSEIF ($SSID.GetType().Name -eq "String") {
$SSID = Get-AvailableWirelessNetworks -SSID $SSID
$ssidName = $SSID.SSID
}
$wlanProfile = netsh wlan show profiles name=$ssidName
IF ($wlanProfile -eq ('Profile "' + $ssidName + '" is not found on the system.')) {
Write-Host "New Wireless Network detected. Not yet supported" -ForegroundColor Red
}
IF ($Profile) {
netsh wlan connect ssid=$ssidName name=$Profile
} ELSE {
netsh wlan connect ssid=$ssidName name=$ssidName
}
}
Function Get-WirelessNetworkProfiles {
<#
.LINK
https://github.com/si-kotic/Manage-WirelessNetworks
#>
Param (
[Parameter(ValueFromPipeline=$true)]$SSID
)
IF ($SSID) {
IF ($SSID.GetType().Name -eq "PSCustomObject") {
$ssidName = $SSID.SSID
} ELSEIF ($SSID.GetType().Name -eq "String") {
$SSID = Get-AvailableWirelessNetworks -SSID $SSID
$ssidName = $SSID.SSID
}
netsh wlan show profiles name="$ssidName"
} ELSE {
$response = netsh wlan show profiles
$profiles = $response[9..($response.length-1)].Trim() -replace "\s{5}:\s","," | Foreach-Object {
$report = "" | Select SSID,UserProfile
$obj = $_.Split(",")
$report.SSID = $obj[1]
$report.UserProfile = $obj[0]
$report
}
$profiles
}
}