-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallHotspotTask.ps1
More file actions
151 lines (124 loc) · 6.22 KB
/
InstallHotspotTask.ps1
File metadata and controls
151 lines (124 loc) · 6.22 KB
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
# InstallHotspotTask.ps1
# Run as Administrator - creates the keepalive script and sets everything up
# ── 1. Allow local scripts to run ────────────────────────────────────────────
Write-Host ""
Write-Host "[ 1/6 ] Setting execution policy..." -ForegroundColor Cyan
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
Write-Host " Execution policy set to RemoteSigned." -ForegroundColor Green
# ── 2. Ask for hotspot name and password ─────────────────────────────────────
Write-Host ""
Write-Host "[ 2/6 ] Hotspot configuration..." -ForegroundColor Cyan
$hotspotName = Read-Host " Enter hotspot name (SSID)"
do {
$hotspotPass = Read-Host " Enter hotspot password (min 8 characters)"
if ($hotspotPass.Length -lt 8) {
Write-Host " Password must be at least 8 characters. Try again." -ForegroundColor Red
}
} while ($hotspotPass.Length -lt 8)
Write-Host " Hotspot name: $hotspotName" -ForegroundColor Green
Write-Host " Password set successfully." -ForegroundColor Green
# ── 3. Create the keepalive script ───────────────────────────────────────────
Write-Host ""
Write-Host "[ 3/6 ] Creating HotspotKeepAlive.ps1 in C:\Scripts..." -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path "C:\Scripts" | Out-Null
$keepAliveScript = @"
# HotspotKeepAlive.ps1 - Auto-generated. Do not delete.
Add-Type -AssemblyName System.Runtime.WindowsRuntime
function Await(`$task) {
`$task.GetAwaiter().GetResult()
}
[Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager, Windows.Networking.NetworkOperators, ContentType = WindowsRuntime] | Out-Null
[Windows.Networking.Connectivity.NetworkInformation, Windows.Networking.Connectivity, ContentType = WindowsRuntime] | Out-Null
function Set-HotspotConfig {
try {
`$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()
if (`$null -eq `$connectionProfile) { return }
`$manager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager]::CreateFromConnectionProfile(`$connectionProfile)
`$config = `$manager.GetCurrentAccessPointConfiguration()
`$config.Ssid = "$hotspotName"
`$config.Passphrase = "$hotspotPass"
Await(`$manager.ConfigureAccessPointAsync(`$config))
} catch {
Write-Host "`$(Get-Date): Could not set hotspot config - `$_"
}
}
function Get-TetheringManager {
`$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()
if (`$null -eq `$connectionProfile) { return `$null }
return [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager]::CreateFromConnectionProfile(`$connectionProfile)
}
function Enable-Hotspot {
try {
`$manager = Get-TetheringManager
if (`$null -eq `$manager) {
Write-Host "`$(Get-Date): No internet connection profile found. Retrying..."
return
}
`$status = `$manager.TetheringOperationalState
if (`$status -ne 1) {
Write-Host "`$(Get-Date): Hotspot is OFF (state: `$status). Turning ON..."
Await(`$manager.StartTetheringAsync())
Write-Host "`$(Get-Date): Hotspot turned ON successfully."
} else {
Write-Host "`$(Get-Date): Hotspot is already ON."
}
} catch {
Write-Host "`$(Get-Date): Error - `$_"
}
}
Write-Host "HotspotKeepAlive started. SSID: $hotspotName | Checking every 30 seconds..."
Set-HotspotConfig
while (`$true) {
Enable-Hotspot
Start-Sleep -Seconds 30
}
"@
Set-Content -Path "C:\Scripts\HotspotKeepAlive.ps1" -Value $keepAliveScript -Encoding UTF8
Write-Host " HotspotKeepAlive.ps1 created." -ForegroundColor Green
# ── 4. Register the scheduled task ───────────────────────────────────────────
Write-Host ""
Write-Host "[ 4/6 ] Registering scheduled task..." -ForegroundColor Cyan
$action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File `"C:\Scripts\HotspotKeepAlive.ps1`""
$trigger = New-ScheduledTaskTrigger -AtStartup
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-ExecutionTimeLimit 0 `
-RestartCount 3 `
-RestartInterval (New-TimeSpan -Minutes 1)
$principal = New-ScheduledTaskPrincipal `
-UserId "SYSTEM" `
-LogonType ServiceAccount `
-RunLevel Highest
Register-ScheduledTask `
-TaskName "HotspotKeepAlive" `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Principal $principal `
-Description "Keeps Windows Mobile Hotspot always enabled" `
-Force | Out-Null
Write-Host " Scheduled task registered." -ForegroundColor Green
# ── 5. Start the task immediately ────────────────────────────────────────────
Write-Host ""
Write-Host "[ 5/6 ] Starting task now..." -ForegroundColor Cyan
Start-ScheduledTask -TaskName "HotspotKeepAlive"
Start-Sleep -Seconds 2
Write-Host " Task started." -ForegroundColor Green
# ── 6. Show current status ───────────────────────────────────────────────────
Write-Host ""
Write-Host "[ 6/6 ] Task status:" -ForegroundColor Cyan
$state = (Get-ScheduledTask -TaskName "HotspotKeepAlive").State
if ($state -eq "Running") {
Write-Host " HotspotKeepAlive is ACTIVE and running." -ForegroundColor Green
} else {
Write-Host " HotspotKeepAlive state: $state" -ForegroundColor Yellow
}
Write-Host ""
Write-Host " Hotspot Name : $hotspotName" -ForegroundColor White
Write-Host " Password : $hotspotPass" -ForegroundColor White
Write-Host ""
Write-Host "All done! The hotspot will auto-enable on every boot." -ForegroundColor White
Write-Host ""