2013-05-10 1 views
8

Quindi ho trovato questo eccellente metodo di persisting history in Windows PowerShell.Cronologia persistente di 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 } 

Tuttavia quando ho incolla questo nel mio $ profilo e ricomincio PowerShell ho la seguente errore

 
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, correzioni PowerShell v3 una serie di questioni che si stai cercando di aggirare. $ MaximumHistoryCount è 4096 per impostazione predefinita e, per impostazione predefinita, Get-History restituisce tutti gli elementi. –

+0

> $ PSVersionTable.PSVersion Major = 3, Minor = 0, Build = -1, Revision = -1 – alexleonard

+1

Cool. Quindi puoi saltare la modifica di $ MaximumHistoryCount e Get-History restituirà tutta la cronologia per te (beh, fino a 4096 voci). A meno che non lo sia, si desidera limitare quanto viene salvato. :-) –

risposta

1

Questo è il codice che uso da utilizzare quando ho perseverato la mia storia

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

Il GetNewClosure() viene utilizzato per acquisire la variabile IIRC $historyPath.

+1

Hey, grazie per la risposta. È tutto il codice che hai usato? Il post del blog che sto seguendo fa qualcosa di non necessario con la sezione in # Carica la cronologia precedente, se esiste? – alexleonard

+0

Spetta a te ma con il tempo la storia diventerà terribilmente grande. Preferisco avere un semplice meccanismo per cercare la mia storia persistente. –

+0

Fresco. Lo proverò domani quando torno in ufficio. Grazie! – alexleonard

6

Molto correlato a questa domanda è "Come posso accedere alla cronologia utilizzando le frecce su/giù?" Il modulo PSReadLine risolve questo:

Install-Package PSReadLine 

Quindi aggiungere:

Import-Module PSReadLine 

Per vostra $profile.

+0

Nota che PSReadLine ora viene fornito con Windows 10. :) – jhclark

+0

Windows PowerShell ISE ha una cronologia premendo 'up' e' down' per impostazione predefinita. –

+0

RSReadline non memorizza i miei comandi dopo un riavvio – aumanjoa

1

Beh, come una combinazione di codice del topicstarter e una risposta leggermente modificato da Steven Penny, questo è il pezzo pieno di codice che sta lavorando per me

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

Funziona alla grande (funziona la storia) ma su/giù non si muove attraverso gli elementi della cronologia - qualche idea su come farlo funzionare? – mikemaccana

+0

Importare PsTeadline per eseguire lo spostamento su/giù nella cronologia. –