サービスを開始/停止するために引数 On/off を受け取る PowerShell を、サービスがオン (またはオフ) かどうかをチェックして停止 (または開始) するスクリプトに変更します。

サービスを開始/停止するために引数 On/off を受け取る PowerShell を、サービスがオン (またはオフ) かどうかをチェックして停止 (または開始) するスクリプトに変更します。

このコードはここ受信した引数に基づいて Bluetooth サービスを開始または停止します。

bluetooth.ps1 -BluetoothStatus On

または

bluetooth.ps1 -BluetoothStatus Off

単純な ahk キー バインディングを使用して引数なしで呼び出せるように変更したいと思います。

#b::
Run, C:\Users\user\Desktop\bluetooth.ps1 
return 

次に、スクリプトは「独自に」Bluetooth がオンかオフかを確認し、状態を変更するために必要な処理を実行します。オンの場合は停止し、オフの場合は起動します。

[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

答え1

魔法はコードの最後の数行にあります:

$bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
[Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime]
Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus])

変数 $bluetooth には Bluetooth 無線の現在の状態が保持されます。

PS C:\> $bluetooth

     Kind Name      State
     ---- ----      -----
Bluetooth Bluetooth    On

したがって、Await を再度呼び出して、'Off' または 'On' に設定されたパラメーター $BluetoothStatus を渡す最後の行の前に、値に条件を使用できます。

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

これでパラメータ行を削除できます。

[Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$BluetoothStatus

そして、スクリプトを呼び出すたびに、現在の状態を反転する必要があります。

答え2

両方の長所を兼ね備えた製品です。

  • 通話のオン/オフを切り替えるにはpowershell -Command bluetooth.ps1
  • 通話を明示的にオンにするにはpowershell -Command bluetooth.ps1 -BluetoothStatus On
  • 通話を明示的にオフにするにはpowershell -Command bluetooth.ps1 -BluetoothStatus Off
[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

関連情報