Bluetooth.psm1
复制代码
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 {
}
}