关于powershell:powershell-按F7展开搜索历史记录

3次阅读

共计 1414 个字符,预计需要花费 4 分钟才能阅读完成。

Install-Module Microsoft.PowerShell.ConsoleGuiTools
code $profile
增加一下代码

# 按 f7 显示历史记录
function ocgv_history {
  $line = $null
  $cursor = $null
  [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
  $selection = $input | Out-ConsoleGridView -Title "Select CommandLine from History" -OutputMode Single -Filter $line
  if ($selection) {[Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine()
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert($selection)
    if ($selection.StartsWith($line)) {[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor)
    }
    else {[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selection.Length)
    }
  }
}

$parameters = @{
  Key = 'F7'
  BriefDescription = 'ShowMatchingHistoryOcgv'
  LongDescription = 'Show Matching History using Out-ConsoleGridView'
  ScriptBlock = {param($key, $arg)   # The arguments are ignored in this example
    Get-History | Sort-Object -Descending -Property Id -Unique | Select-Object CommandLine -ExpandProperty CommandLine |  ocgv_history
  }
}
Set-PSReadLineKeyHandler @parameters

$parameters = @{
  Key = 'Shift-F7'
  BriefDescription = 'ShowMatchingGlobalHistoryOcgv'
  LongDescription = 'Show Matching History for all PowerShell instances using Out-ConsoleGridView'
  ScriptBlock = {param($key, $arg)   # The arguments are ignored in this example
    $hist = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems().CommandLine
    # reverse the items so most recent is on top
    [array]::Reverse($hist)
    $hist | Select-Object -Unique -First 1000 | ocgv_history
  }
}
Set-PSReadLineKeyHandler @parameters
正文完
 0