Public/Move-PWSHYBKPIVKey.ps1
|
function Move-PWSHYBKPIVKey { <# .SYNOPSIS Moves the key in a PIV slot of a locally attached YubiKey to another slot. .DESCRIPTION Wraps "yubico-piv-tool.exe --action move-key". Overwrites any key already present in the destination slot with no way to recover it, so this cmdlet supports -WhatIf/-Confirm with ConfirmImpact 'High'. Throws on failure rather than returning a value, matching Move- verb convention. Accepts the "move-key" action's config-declared options (-Slot, -ToSlot, -ManagementKey, -PinPolicy, -TouchPolicy, -Reader) as dynamic parameters built from Config\Posh-YBKPIV.json. .PARAMETER Slot The PIV slot the key currently occupies, e.g. '9a'. .PARAMETER ToSlot The PIV slot to move the key to. .PARAMETER ManagementKey The management key authorizing the operation, as a SecureString. Defaults to yubico-piv-tool.exe's built-in default management key when omitted. .PARAMETER PinPolicy PIN policy to apply to the moved key: never, once, always, matchonce, or matchalways. .PARAMETER TouchPolicy Touch policy to apply to the moved key: never, always, or cached. .PARAMETER Reader Name of the smart card reader to target, when more than one is attached. If omitted, yubico-piv-tool.exe uses its own default reader selection. .INPUTS None. This cmdlet does not accept pipeline input. .OUTPUTS None. Throws a terminating error on failure; produces no output on success. .NOTES -Slot, -ToSlot, -ManagementKey, -PinPolicy, -TouchPolicy, and -Reader are declared dynamically from Config\Posh-YBKPIV.json and therefore do not appear in Get-Help's PARAMETERS/SYNTAX sections. Run `Get-Command Move-PWSHYBKPIVKey -Syntax` for the authoritative, current parameter list. .EXAMPLE Move-PWSHYBKPIVKey -Slot '9a' -ToSlot '82' Moves the key from slot 9a to the retired key slot 82 after confirmation. .LINK https://developers.yubico.com/yubico-piv-tool/Actions/ .LINK New-PWSHYBKPIVKey .LINK Remove-PWSHYBKPIVKey #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] param() DynamicParam { $dynamicConfig = Read-PWSHYBKPIVConfigFile Get-PWSHYBKPIVActionParameter -Action 'move-key' -CmdletWrapping $dynamicConfig.cmdletWrapping } begin { $config = Read-PWSHYBKPIVConfigFile Write-PWSHYBKPIVLog -Config $config -Level Debug -CmdletName $MyInvocation.MyCommand.Name ` -Message 'Cmdlet invoked' -BoundParameters $PSBoundParameters $architecture = Resolve-PWSHYBKPIVArchitecture -Architecture $config.installation.architecture $exePath = Get-PWSHYBKPIVInstallPath -Installation $config.installation -Architecture $architecture if (-not (Test-Path -Path $exePath -PathType Leaf)) { throw "yubico-piv-tool.exe was not found at '$exePath'. Run Install-PWSHYBKPIVTool first." } } end { $moveDescription = "PIV slot $($PSBoundParameters['Slot']) to slot $($PSBoundParameters['ToSlot'])" if (-not $PSCmdlet.ShouldProcess($moveDescription, 'Move key')) { return } try { $null = Invoke-PWSHYBKPIVTool -ExePath $exePath -Action 'move-key' ` -CmdletWrapping $config.cmdletWrapping -BoundParameters $PSBoundParameters Write-PWSHYBKPIVLog -Config $config -Level Information -CmdletName $MyInvocation.MyCommand.Name ` -Message "Key moved from $moveDescription" } catch { Write-PWSHYBKPIVLog -Config $config -Level Error -CmdletName $MyInvocation.MyCommand.Name -Message "Key move failed: $_" throw } } } |