Skip to content

Instantly share code, notes, and snippets.

@ggorlen
Created March 5, 2021 15:32
Show Gist options
  • Select an option

  • Save ggorlen/22e4b10a91fdafffaf46b4e9be4fe0bd to your computer and use it in GitHub Desktop.

Select an option

Save ggorlen/22e4b10a91fdafffaf46b4e9be4fe0bd to your computer and use it in GitHub Desktop.
auto-restart windows 10 wifi hotspot
# Be sure to include Ben N.'s await for IAsyncOperation:
# https://superuser.com/questions/1341997/using-a-uwp-api-namespace-in-powershell
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
# ------------------------------------------- #
# https://stackoverflow.com/questions/45833873/enable-windows-10-built-in-hotspot-by-cmd-batch-powershell
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
for (;;) {
# Check whether Mobile Hotspot is enabled
if ($tetheringManager.TetheringOperationalState -eq "Off") {
echo ("[{0}] Turning on hotspot" -f (Get-Date -f "yyyy-MM-dd HH:mm:ss"))
# Start Mobile Hotspot
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
Start-Sleep -Seconds (1 * 60)
}
# Stop Mobile Hotspot
#Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment