2013-05-10 15 views
8

Bu mükemmel görünümlü yöntemi persisting history in Windows PowerShell buldum. Benim $ PROFİLDEN içine bu yapıştırın ve PowerShell yeniden başlattığınızdaWindows PowerShell Kalıcı Tarihçesi

# 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 } 

Ancak bu benim geçmişini kalıcı kullanılacak kullanmak koddur

 
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

Bilginize benim için bir takım konularda çalışıyor kod tam parçasıdır sen Çalışmaya çalışıyoruz. $ MaximumHistoryCount varsayılan olarak 4096'dır ve varsayılan olarak Get-History tüm öğeleri döndürür. –

+0

> $ PSVersionTable.PSVersion Major = 3, Minor = 0, Yapı = -1, Sürüm = -1 – alexleonard

+1

Serin. Ardından, $ MaximumHistoryCount ve Get-History öğelerini değiştirerek tüm geçmişinizi sizin için geri gönderebilirsiniz (4096 ürüne kadar). Öyle değilse, ne kadar tasarruf edildiğini sınırlamak istersiniz. :-) –

cevap

1

aşağıdaki hatayı olsun

$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() 

$historyPath değişken IIRC'yi yakalamak için GetNewClosure() kullanılır.

+1

Hey cevabı için teşekkürler. Bu kullandığınız bütün kod mu? Blog gönderisi, # Geçmiş geçmişini yükle bölümündeki bölümle ilgili bir şeyleri yapmaya devam ediyor muyum? – alexleonard

+0

Size kalmış ama zaman geçtikçe tarih çok büyük olacak. Kalıcı tarihimi aramak için basit bir mekanizmayı tercih ederim. –

+0

Serin. Ofise döndüğümde yarın test ederim. Teşekkürler! – alexleonard

6

Bu soruyla çok ilgili olan "Yukarı/aşağı okları kullanarak geçmişime nasıl erişebilirim?" PSReadLine modül bu çözer:

Install-Package PSReadLine 

Sonra ekleyin:

Import-Module PSReadLine 

sizin $profile belirleyin.

+0

Not: PSReadLine şimdi Windows 10 ile birlikte geliyor. :) – jhclark

+0

Windows PowerShell ISE, varsayılan olarak 'yukarı' ve 'aşağı' basarak geçmişe sahiptir. –

+0

RSReadline, yeniden başlatıldıktan sonra komutlarımı saklamıyor – aumanjoa

1

Eh, topicstarter kodunun bir kombinasyonu ve Steven Penny tarafından biraz değişmiş cevap olarak, bu PowerShell v3 düzeltmeleri,

################# 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

Bu, harika çalışıyor (get-history çalışmaları), ancak yukarı/aşağı, geçmiş öğeler arasında hareket etmiyor - bu işin nasıl yapıldığına dair herhangi bir fikir var mı? – mikemaccana

+0

Import PsReadline ile yukarı/aşağı hareket ettirin. –