-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwht.ps1
More file actions
241 lines (204 loc) · 9.61 KB
/
wht.ps1
File metadata and controls
241 lines (204 loc) · 9.61 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<#
.SYNOPSIS
Hotspot Toggle with Persistent WiFi Adapter Selection
.DESCRIPTION
A modern PowerShell script to toggle the WiFi hotspot on Windows 10/11
.NOTES
added tagging automation
#>
# ==== Script version ====
$scriptVersion = "1.0.18"
# ==== Configuration ====
$configFile = "$PSScriptRoot\adapter.config"
$logFile = $true
$logReverse = $false
$logFilePath = "$PSScriptRoot\script.log"
$restartWiFi = $true
$forceAsAdmin = $true
$delayViaScript = $true
$delay = 90
# FORCE POWERSHELL 5.1 HANDOFF (Must come first to avoid Type errors in PS7)
if ($PSVersionTable.PSVersion.Major -gt 5) {
# Check if we can even log this yet
Write-Host "Detected PowerShell 7+. Relaunching in PowerShell 5.1 for WinRT compatibility..." -ForegroundColor Cyan
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
Exit
}
# LOAD ASSEMBLIES & NAMESPACES
try {
Add-Type -AssemblyName "System.Runtime.WindowsRuntime" -ErrorAction Stop
$WinRTAssemblies = @(
"Windows.Networking.Connectivity.NetworkInformation, Windows.Networking.Connectivity, ContentType=WindowsRuntime",
"Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager, Windows.Networking.NetworkOperators, ContentType=WindowsRuntime",
"Windows.Devices.Radios.Radio, Windows.System.Devices, ContentType=WindowsRuntime",
"Windows.Devices.Radios.RadioAccessStatus, Windows.System.Devices, ContentType=WindowsRuntime",
"Windows.Devices.Radios.RadioState, Windows.System.Devices, ContentType=WindowsRuntime"
)
foreach ($asm in $WinRTAssemblies) {
[void][System.Runtime.InteropServices.Marshal]::PrelinkAll([type]::GetType($asm))
}
} catch {
Write-Warning "WinRT Type loading encountered an issue: $_"
}
# ==== WinRT Helper Logic ====
$RuntimeExtensions = [AppDomain]::CurrentDomain.GetAssemblies() |
ForEach-Object { $_.GetType("System.WindowsRuntimeSystemExtensions") } |
Where-Object { $_ -ne $null } | Select-Object -First 1
if ($RuntimeExtensions) {
$asTaskGeneric = $RuntimeExtensions.GetMethods() | Where-Object {
$_.Name -eq 'AsTask' -and
$_.GetParameters().Count -eq 1 -and
$_.GetParameters()[0].ParameterType.Name -like 'IAsyncOperation*'
} | Select-Object -First 1
}
# Helper to await WinRT async operations
function Await-WinRT {
param ($winRtOperation, [Type]$resultType)
try {
$asTask = $asTaskGeneric.MakeGenericMethod($resultType)
$netTask = $asTask.Invoke($null, @($winRtOperation))
$netTask.Wait(-1) | Out-Null
return $netTask.Result
} catch {
Write-Warning "Await-WinRT failed: $_"
return $null
}
}
# ==== FUNCTIONS =====
# BurnToast Notification Functionality
function Send-Notification {
param ([string]$Title, [string]$Message)
if (-not (IsRunningFromTerminal)) {
if (Get-Module -ListAvailable -Name BurntToast) {
New-BurntToastNotification -Text $Title, $Message
}
}
}
# Admin Check & Relaunch
function IsAdmin {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# Relaunch script as admin if not already
function RunAsAdmin {
if ($env:SYSTEMROOT -and $env:USERNAME -eq "SYSTEM") { return }
if (-Not (IsAdmin)) {
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
Exit 0
}
}
# Logging Functionality
function LogThis {
param ([string]$Message, [string]$Color = "White")
Write-Host $Message -ForegroundColor $Color
if ($logFile) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMsg = "[$timestamp] $Message"
if ($logReverse) {
$existing = if (Test-Path $logFilePath) { Get-Content $logFilePath -Raw } else { "" }
"$logMsg`n$existing" | Set-Content $logFilePath -Encoding UTF8
} else {
$logMsg | Out-File -Append $logFilePath -Encoding UTF8
}
}
}
# Check if running from a terminal
function IsRunningFromTerminal {
$proc = Get-CimInstance Win32_Process -Filter "ProcessId = $pid"
$parent = Get-CimInstance Win32_Process -Filter "ProcessId = $($proc.ParentProcessId)"
return -not ($parent.Name -eq "svchost.exe" -or $parent.CommandLine -like "*Schedule*")
}
# Get or select WiFi adapter
function Get-WiFiAdapter {
if (Test-Path $configFile) {
try {
$saved = Get-Content $configFile | ConvertFrom-Json
$adapter = Get-NetAdapter | Where-Object { $_.Name -eq $saved.Name -and $_.InterfaceDescription -eq $saved.Description }
if ($adapter) { return $adapter }
} catch { LogThis "Config error: $_" -Color "Yellow" }
}
$adapter = Get-NetAdapter | Where-Object { $_.PhysicalMediaType -eq 'Native 802.11' } | Select-Object -First 1
if ($adapter) { return $adapter }
$selected = Get-NetAdapter | Out-GridView -Title "Select WiFi Adapter" -PassThru
if ($selected) {
[PSCustomObject]@{Name=$selected.Name; Description=$selected.InterfaceDescription} | ConvertTo-Json | Out-File $configFile
return $selected
}
throw "No WiFi adapter found"
}
# Set WiFi Radio State
function Set-WifiRadioState {
param ([string]$TargetState)
try {
Await-WinRT ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await-WinRT ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$wifiRadio = $radios | Where-Object { $_.Kind -eq 'WiFi' }
if ($wifiRadio) {
$state = if ($TargetState -eq "On") { [Windows.Devices.Radios.RadioState]::On } else { [Windows.Devices.Radios.RadioState]::Off }
LogThis "Radio: $TargetState" -Color "Yellow"
Await-WinRT ($wifiRadio.SetStateAsync($state)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
return $true
}
} catch { LogThis "Radio error: $_" -Color "Red" }
return $false
}
# Main Hotspot Toggle Logic
function Switch-Hotspot {
param($adapter)
try {
$connectionProfile = $null
$retryCount = 0
$maxRetries = 12 # Up to 60 seconds
while ($null -eq $connectionProfile -and $retryCount -lt $maxRetries) {
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()
if ($null -eq $connectionProfile) {
LogThis "Waiting for Internet profile (Attempt $($retryCount + 1)/$maxRetries)..." -Color "Yellow"
Start-Sleep -Seconds 5
$retryCount++
}
}
if ($null -eq $connectionProfile) {
LogThis "Error: No Internet profile found. Aborting." -Color "Red"
return $false
}
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager]::CreateFromConnectionProfile($connectionProfile)
if ($tetheringManager.TetheringOperationalState -eq 'On') {
LogThis "Hotspot ON -> Disabling..." -Color "Yellow"
$null = Await-WinRT ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
Send-Notification -Title "Hotspot" -Message "Disabled"
return $true
}
LogThis "Resetting Radio for Win10 Routing Fix..." -Color "Cyan"
Set-WifiRadioState -TargetState "Off"
Start-Sleep -Seconds 2
Set-WifiRadioState -TargetState "On"
Start-Sleep -Seconds 6 # Added more time for Win10 DHCP stability
if ($restartWiFi) {
LogThis "Restarting Adapter: $($adapter.Name)" -Color "Yellow"
Restart-NetAdapter -Name $adapter.Name -Confirm:$false
Start-Sleep -Seconds 3
}
LogThis "Enabling Hotspot..." -Color "Yellow"
$result = Await-WinRT ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
if ($result.Status -eq "Success") {
LogThis "Success! Hotspot is active." -Color "Green"
Send-Notification -Title "Hotspot" -Message "Enabled"
} else {
LogThis "Failed. Status: $($result.Status)" -Color "Red"
}
} catch { LogThis "Critical error: $_" -Color "Red" }
}
# ==== RUNTIME EXECUTION ====
LogThis "==== Script Execution Initiated ====" -Color "Cyan"
LogThis "Script Version: $scriptVersion"
if ($forceAsAdmin) { RunAsAdmin }
$targetAdapter = Get-WiFiAdapter
# You can delay here, or via Task Scheduler
if ($delayViaScript -and -not (IsRunningFromTerminal)) {
LogThis "Background mode: Delaying $delay seconds."
Start-Sleep -Seconds $delay
}
# Main Hotspot Toggle
Switch-Hotspot -adapter $targetAdapter
LogThis "==== Done ===="