admin管理员组

文章数量:1625802

个人需要定时开启关闭蓝牙,遂尝试,目前已成功,网络上资料较少,做记录。

首先,定时执行,需要用到windows10定时任务计划程序。

然后,蓝牙的开关使用powershell控制。

网上看了一圈,好像没有很直接的指令直接开启关闭蓝牙。在外网找到了个解决方案。

新建一个ps1脚本

[CmdletBinding()] Param (
    [Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][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
Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null

直接新建文本改名改后缀为

bluetooth.ps1

即可。

然后使用powershell执行试试效果

.\bluetooth.ps1 -BluetoothStatus On

on开启off关闭就不用说了吧

能正常使用后,建立计划程序

脚本或程序选择 powershell.exe

参数如下

-Command "C:\Users\Administrator\bluetooth.ps1 -BluetoothStatus Off"

记得要用绝对路径执行,我这块懂得不多,不知道直接启用是从哪里执行的,所以直接用绝对路径。

搞好了自己运行一下看看有没有什么问题,没有就ok

如果显示被管理员拒绝

打开“控制面板->管理工具->本地安全策略”,选择“安全设置->本地策略->安全选项”,在右边列表中找到“域控制器:允许服务器操作者计划任务”,将状态改为“已启用”。

基本就这样吧。

本文标签: 蓝牙也可Windows