Last active
August 22, 2019 17:46
-
-
Save MyITGuy/c03459827d0ff75d9f7fc05131a62a40 to your computer and use it in GitHub Desktop.
Turn Bluetooth on/off or reset.
Set-Bluetooth
Disable-Bluetooth
Enable-Bluetooth
Reset-Bluetooth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-Bluetooth { | |
[CmdletBinding()] | |
Param ( | |
[Parameter(Mandatory = $true)] | |
[ValidateSet('On', 'Off', 'Reset')] | |
[string] | |
$State | |
) | |
begin { | |
Add-Type -AssemblyName System.Runtime.WindowsRuntime | |
} | |
process { | |
if ((Get-Service -Name bthserv).Status -eq 'Stopped') { Start-Service -Name bthserv } | |
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.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 | Where-Object { $_.Kind -eq 'Bluetooth' } | |
if ($bluetooth.State -ne $State) { | |
switch($State) { | |
'Reset' { | |
[Windows.Devices.Radios.RadioState, Windows.System.Devices, ContentType = WindowsRuntime] | Out-Null | |
try { | |
if ((Await ($bluetooth.SetStateAsync('Off')) ([Windows.Devices.Radios.RadioAccessStatus])) -eq 'Allowed') { | |
if ((Await ($bluetooth.SetStateAsync('On')) ([Windows.Devices.Radios.RadioAccessStatus])) -eq 'Allowed') { | |
Write-Host "Bluetooth reset." | |
} | |
else { | |
Write-Host "Bluetooth could not be turned on." | |
} | |
} | |
else { | |
Write-Host "Bluetooth could not be turned off." | |
} | |
} | |
catch { | |
Throw $_ | |
} | |
break | |
} | |
default { | |
[Windows.Devices.Radios.RadioState, Windows.System.Devices, ContentType = WindowsRuntime] | Out-Null | |
try { | |
if ((Await ($bluetooth.SetStateAsync($State)) ([Windows.Devices.Radios.RadioAccessStatus])) -eq 'Allowed') { | |
Write-Host "Bluetooth turned $($State.ToLower())." | |
} | |
else { | |
Write-Host "Bluetooth could not be turned $($State.ToLower())." | |
} | |
} | |
catch { | |
Throw $_ | |
} | |
} | |
} | |
} | |
else { | |
Write-Host "Bluetooth is already $($State.ToLower())." | |
} | |
} | |
end { | |
} | |
} | |
function Disable-Bluetooth { | |
[CmdletBinding()] | |
Param () | |
begin { | |
Add-Type -AssemblyName System.Runtime.WindowsRuntime | |
$State = 'Off' | |
} | |
process { | |
if ((Get-Service -Name bthserv).Status -eq 'Stopped') { Start-Service -Name bthserv } | |
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.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 | Where-Object { $_.Kind -eq 'Bluetooth' } | |
if ($bluetooth.State -ne $State) { | |
[Windows.Devices.Radios.RadioState, Windows.System.Devices, ContentType = WindowsRuntime] | Out-Null | |
try { | |
if ((Await ($bluetooth.SetStateAsync($State)) ([Windows.Devices.Radios.RadioAccessStatus])) -eq 'Allowed') { | |
Write-Host "Bluetooth turned $($State.ToLower())." | |
} | |
else { | |
Write-Host "Bluetooth could not be turned $($State.ToLower())." | |
} | |
} | |
catch { | |
Throw $_ | |
} | |
} | |
else { | |
Write-Host "Bluetooth is already $($State.ToLower())." | |
} | |
} | |
end { | |
} | |
} | |
function Enable-Bluetooth { | |
[CmdletBinding()] | |
Param () | |
begin { | |
Add-Type -AssemblyName System.Runtime.WindowsRuntime | |
$State = 'On' | |
} | |
process { | |
if ((Get-Service -Name bthserv).Status -eq 'Stopped') { Start-Service -Name bthserv } | |
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.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 | Where-Object { $_.Kind -eq 'Bluetooth' } | |
if ($bluetooth.State -ne $State) { | |
[Windows.Devices.Radios.RadioState, Windows.System.Devices, ContentType = WindowsRuntime] | Out-Null | |
try { | |
if ((Await ($bluetooth.SetStateAsync($State)) ([Windows.Devices.Radios.RadioAccessStatus])) -eq 'Allowed') { | |
Write-Host "Bluetooth turned $($State.ToLower())." | |
} | |
else { | |
Write-Host "Bluetooth could not be turned $($State.ToLower())." | |
} | |
} | |
catch { | |
Throw $_ | |
} | |
} | |
else { | |
Write-Host "Bluetooth is already $($State.ToLower())." | |
} | |
} | |
end { | |
} | |
} | |
function Reset-Bluetooth { | |
[CmdletBinding()] | |
Param () | |
begin { | |
Add-Type -AssemblyName System.Runtime.WindowsRuntime | |
$State = 'Reset' | |
} | |
process { | |
if ((Get-Service -Name bthserv).Status -eq 'Stopped') { Start-Service -Name bthserv } | |
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.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 | Where-Object { $_.Kind -eq 'Bluetooth' } | |
[Windows.Devices.Radios.RadioState, Windows.System.Devices, ContentType = WindowsRuntime] | Out-Null | |
try { | |
if ((Await ($bluetooth.SetStateAsync('Off')) ([Windows.Devices.Radios.RadioAccessStatus])) -eq 'Allowed') { | |
if ((Await ($bluetooth.SetStateAsync('On')) ([Windows.Devices.Radios.RadioAccessStatus])) -eq 'Allowed') { | |
Write-Host "Bluetooth reset." | |
} | |
else { | |
Write-Host "Bluetooth could not be turned on." | |
} | |
} | |
else { | |
Write-Host "Bluetooth could not be turned off." | |
} | |
} | |
catch { | |
Throw $_ | |
} | |
} | |
end { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment