-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBluetooth.ps1
More file actions
95 lines (83 loc) · 3.73 KB
/
Copy pathBluetooth.ps1
File metadata and controls
95 lines (83 loc) · 3.73 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
<#
.SYNOPSIS
Automate actions for turning bluetooth on/off
.DESCRIPTION
Control bluetooth radio device
.PARAMETER Status
Desired state of Bluetooth
.EXAMPLE
On legacy powersehll,
Bluetooth.ps1 Off
Bluetooth.ps1 On
On powershell 7 (or the core versions),
Powershell -NoProfile -File ($PwshScriptDir + '\Bluetooth.ps1') On
.NOTES
**REFS**
- Turn on/off Bluetooth radio/adapter from cmd/powershell in Windows 10
https://superuser.com/q/1168551/
- Not used right now, but, in trouble, can also look at,
http://www.thewindowsclub.com/disable-bluetooth-windows-10
tag: windows-only
#>
[CmdletBinding()] Param (
[Parameter(Mandatory=$true)] [ValidateSet('Off', 'On')] [string] $NewStatus)
##################### Function Definition Ends #####################################
#######################################################################################################
# Start of Main function
function Main() {
$WirelessService = 'bthserv'
If ($NewStatus -eq 'On' -and (Get-Service $WirelessService).Status -eq 'Stopped'
) {
# ref for runas: https://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::`
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] `
"Administrator")) {
$arguments = "Start-Service", $WirelessService
Start-Process powershell -Verb runAs -ArgumentList $arguments
# Delay is a PS Script
# Delay 0 'Please adjust audio level/volume for the headphone.'
'Please adjust audio level/volume for the headphone.'
Start-Sleep 1
}
else {
Start-Service $WirelessService
}
}
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$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
Await ($bluetooth.SetStateAsync($NewStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) |
Out-Null
If ($NewStatus -eq 'Off' -and (Get-Service $WirelessService).Status -eq 'Running') {
# ref for runas
# https://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::`
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] `
"Administrator")) {
# Debug, add following in the beginning of arguments list to make powershell wait after command
# '-NoExit',
$arguments = 'Stop-Service', '-Force', $WirelessService
Start-Process powershell -Verb runAs -ArgumentList $arguments
}
else {
Stop-Service -Force $WirelessService
}
}
}
Main