2013-05-10 13 views
8

Vì vậy, tôi đã tìm thấy phương pháp tìm kiếm tuyệt vời này là persisting history in Windows PowerShell.Lịch sử liên tục Windows PowerShell

# Persistent History 
# Save last 200 history items on exit 
$MaximumHistoryCount = 200  
$historyPath = Join-Path (split-path $profile) history.clixml 

# Hook powershell's exiting event & hide the registration with -supportevent (from nivot.org) 
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { 
    Get-History -Count $MaximumHistoryCount | Export-Clixml (Join-Path (split-path $profile) history.clixml) 
} 

# Load previous history, if it exists 
if ((Test-Path $historyPath)) { 
    Import-Clixml $historyPath | ? {$count++;$true} | Add-History 
    Write-Host -Fore Green "`nLoaded $count history item(s).`n" 
} 

# Aliases and functions to make it useful 
New-Alias -Name i -Value Invoke-History -Description "Invoke history alias" 
Rename-Item Alias:\h original_h -Force 
function h { Get-History -c $MaximumHistoryCount } 
function hg($arg) { Get-History -c $MaximumHistoryCount | out-string -stream | select-string $arg } 

Tuy nhiên khi tôi dán vào $ PROFILE của tôi và khởi động lại PowerShell tôi nhận được lỗi sau

 
Register-EngineEvent : Missing an argument for parameter 'Action'. Specify a parameter of type 
'System.Management.Automation.ScriptBlock' and try again. 
At D:\path\to\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:20 char:73 
+ Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action 
+                   ~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (:) [Register-EngineEvent], ParameterBindingException 
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.RegisterEngineEventCommand 
+0

FYI, PowerShell v3 sửa chữa một số vấn đề bạn đang cố gắng làm việc xung quanh. $ MaximumHistoryCount là 4096 theo mặc định và theo mặc định Get-History trả về tất cả các mục. –

+0

> $ PSVersionTable.PSVersion Major = 3, Nhỏ = 0, Xây dựng = -1, Revision = -1 – alexleonard

+1

Tuyệt. Sau đó, bạn có thể bỏ qua sửa đổi $ MaximumHistoryCount và Get-History sẽ trả lại tất cả lịch sử cho bạn (tốt, lên đến 4096 bài). Trừ khi đó là, bạn muốn giới hạn số tiền được lưu. :-) –

Trả lời

1

Đây là mã tôi sử dụng để sử dụng khi tôi vẫn kiên trì lịch sử của tôi

$historyPath = Join-Path (split-path $profile) "history-$(Get-Date -f o).clixml" 
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { 
    Get-History | Export-Clixml $historyPath 
}.GetNewClosure() 

GetNewClosure() được sử dụng để ghi lại số $historyPath biến IIRC.

+1

Xin cảm ơn vì câu trả lời. Đó có phải là toàn bộ mã mà bạn đã sử dụng không? Là bài đăng trên blog tôi đang làm một điều gì đó không cần thiết với phần dưới # Tải lịch sử trước đó, nếu nó tồn tại? – alexleonard

+0

Điều đó tùy thuộc vào bạn nhưng theo thời gian, lịch sử sẽ trở nên quá lớn. Tôi thích có một cơ chế đơn giản để tìm kiếm lịch sử tồn tại của tôi. –

+0

Tuyệt. Tôi sẽ kiểm tra nó vào ngày mai khi tôi trở lại văn phòng. Cảm ơn! – alexleonard

6

Rất liên quan đến câu hỏi này là "Làm cách nào để truy cập lịch sử của tôi bằng các mũi tên lên/xuống?" Các module PSReadLine giải quyết này:

Install-Package PSReadLine 

Sau đó thêm:

Import-Module PSReadLine 

Để $profile của bạn.

+0

Lưu ý rằng PSReadLine hiện được giao với Windows 10. :) – jhclark

+0

Windows PowerShell ISE có lịch sử bằng cách nhấn 'up' và' down' theo mặc định. –

+0

RSReadline không lưu trữ các lệnh của tôi sau khi khởi động lại – aumanjoa

1

Vâng, như một sự kết hợp của mã của topicstarter và một câu trả lời hơi thay đổi bởi Steven Penny, đây là mảnh đầy đủ các mã mà đang làm việc cho tôi

################# Persistent History ############ 
# Save last 200 history items on exit 
$MaximumHistoryCount = 200  
$historyPath = Join-Path (split-path $profile) history.clixml 

# Hook powershell's exiting event & hide the registration with -supportevent (from nivot.org) 
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { 
     Get-History | Export-Clixml $historyPath 
}.GetNewClosure() 

# Load previous history, if it exists 
if ((Test-Path $historyPath)) { 
    Import-Clixml $historyPath | ? {$count++;$true} | Add-History 
    Write-Host -Fore Green "`nLoaded $count history item(s).`n" 
} 

# Aliases and functions to make it useful 
New-Alias -Name i -Value Invoke-History -Description "Invoke history alias" 
Rename-Item Alias:\h original_h -Force 
function h { Get-History -c $MaximumHistoryCount } 
function hg($arg) { Get-History -c $MaximumHistoryCount | out-string -stream | select-string $arg } 
+0

Điều này làm việc tuyệt vời (công việc có lịch sử) nhưng lên/xuống không di chuyển qua các mục lịch sử - bất kỳ ý tưởng nào để thực hiện công việc đó? – mikemaccana

+0

Nhập PsReadline để thực hiện lên/xuống di chuyển qua lịch sử. –