서비스가 켜져 있는지(또는 꺼져 있는지) 확인하고 중지(또는 시작)하는 스크립트로 서비스를 시작/중지하기 위해 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 

그런 다음 스크립트는 블루투스가 켜져 있는지 꺼져 있는지 "자체적으로" 확인하고 상태를 변경하는 데 필요한 작업을 수행해야 합니다. 켜져 있으면 블루투스를 중지해야 합니다. 꺼져 있으면 시작해야 합니다.

[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 변수는 블루투스 라디오의 현재 상태를 보유합니다.

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

관련 정보