%20%ED%99%95%EC%9D%B8%ED%95%98%EA%B3%A0%20%EC%A4%91%EC%A7%80(%EB%98%90%EB%8A%94%20%EC%8B%9C%EC%9E%91)%ED%95%98%EB%8A%94%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A1%9C%20%EC%84%9C%EB%B9%84%EC%8A%A4%EB%A5%BC%20%EC%8B%9C%EC%9E%91%2F%EC%A4%91%EC%A7%80%ED%95%98%EA%B8%B0%20%EC%9C%84%ED%95%B4%20On%2Foff%20%EC%9D%B8%EC%88%98%EB%A5%BC%20%EB%B0%9B%EB%8A%94%20powershell%EC%9D%84%20%EC%88%98%EC%A0%95%ED%95%A9%EB%8B%88%EB%8B%A4..png)
나는이 코드를 가지고 있습니다.여기, 수신된 인수에 따라 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