Public/Set-BluetoothState.ps1
|
function Set-BluetoothState { <# .SYNOPSIS Sets or toggles the Bluetooth radio state. .DESCRIPTION If BluetoothState is omitted, the current state is toggled. .PARAMETER BluetoothState Desired Bluetooth state. If omitted, the current state is toggled. .EXAMPLE Set-BluetoothState -BluetoothState On Turns Bluetooth on. .EXAMPLE Set-BluetoothState Toggles Bluetooth state. .OUTPUTS None #> [CmdletBinding(SupportsShouldProcess)] Param( [Parameter(Position = 0)] [ValidateSet('On', 'Off')] [Alias('State', 'NewState', 'TargetState')] [string]$BluetoothState ) $target = if ($BluetoothState) { "Set state to $BluetoothState" } else { 'Toggle state' } if ($PSCmdlet.ShouldProcess('Bluetooth radio', $target)) { $scriptParams = @{ Action = 'Set' } if ($PSBoundParameters.ContainsKey('BluetoothState')) { $scriptParams.BluetoothState = $BluetoothState } $output = Invoke-WindowsPowerShell -ScriptPath $script:BluetoothWinPSScriptPath -ScriptParameters $scriptParams $json = ($output | Where-Object { $_ -and $_.ToString().Trim() } | Select-Object -Last 1) if (-not $json) { throw 'Windows PowerShell bridge did not return a Bluetooth state payload.' } [void]($json | ConvertFrom-Json -ErrorAction Stop) } } |