2

I'm want to switch on/off Bluetooth using a shortcut. So I found this script here.

But my problem is that it doesn't work for me. In fact, it seems to do nothing. I tried to run the script without parameters and with parameters, but each time nothing happened. BT stays in the same state.

Do I need to do something before trying this script? (Including a package or a library in the script? Activating something in Windows?)

For information, I'm using Windows 10 with the last update available.

NEW: I tried something: showing some results by removing some | Out-Null. Then I have this message DeniedByUser when the script goes to the Await() function.

Here is the script:

[CmdletBinding()] Param ( [Parameter()][ValidateSet('On', 'Off')][string]$BluetoothStatus )

If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }

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 }

[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null [Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus])# | Out-Null $radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]]) $bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' } [Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null

if (!$BluetoothStatus) { if ($bluetooth.state -eq 'On') { $BluetoothStatus = 'Off' } else { $BluetoothStatus = 'On' } }

Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus])# | Out-Null

Information: the line $netTask.Wait(-1) shows a good result if I remove the | Out-Null.

Here is the result after running this script:

True
DeniedByUser
True
DeniedByUser

Any idea on where can be the problem?

Gourbish
  • 21
  • 4

0 Answers0