Advertisement
gmknowles

Hotspot Start

Mar 20th, 2023 (edited)
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://superuser.com/a/1434648
  2.  
  3. Add-Type -AssemblyName System.Runtime.WindowsRuntime
  4. $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
  5.  
  6. Function Await($WinRtTask, $ResultType) {
  7.     $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
  8.     $netTask = $asTask.Invoke($null, @($WinRtTask))
  9.     $netTask.Wait(-1) | Out-Null
  10.     $netTask.Result
  11. }
  12.  
  13. Function AwaitAction($WinRtAction) {
  14.     $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
  15.     $netTask = $asTask.Invoke($null, @($WinRtAction))
  16.     $netTask.Wait(-1) | Out-Null
  17. }
  18.  
  19. Function SetHotspot($Enable) {
  20.     $connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
  21.     $tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
  22.  
  23.     if ($Enable -eq 1) {
  24.         if ($tetheringManager.TetheringOperationalState -eq 1)
  25.         {
  26.             "Hotspot is already On!"
  27.         }
  28.         else{
  29.             "Hotspot is off! Turning it on"
  30.             Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
  31.         }
  32.     }
  33.     else {
  34.         if ($tetheringManager.TetheringOperationalState -eq 0)
  35.         {
  36.             "Hotspot is already Off!"
  37.         }
  38.         else{
  39.             "Hotspot is on! Turning it off"
  40.             Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
  41.         }
  42.     }
  43. }
  44.  
  45. Function ToggleHotspot($Delay) {
  46.     SetHotspot(1)
  47.     sleep -seconds $Delay
  48.     SetHotspot(0)
  49.     sleep -seconds $Delay
  50.     SetHotspot(1)
  51. }
  52.  
  53. ToggleHotspot(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement