2009-11-12 84 views
3

我想用posh脚本自动打开ISE中的最后一个opend文件,因此我尝试保存这些文件的文件路径,如下所示。如何打开ISE中最后打开的文件的起始

$action = { $psISE.CurrentPowerShellTab.Files | select -ExpandProperty FullPath | ? { Test-Path $_ } |
Set-Content -Encoding String -Path$PSHOME\psISElastOpenedFiles.txt
Set-Content -Encoding String -Value "Now exiting..." -Path c:\exitingtest.log
}
Register-EngineEvent -SourceIdentifier Exit -SupportEvent -Action $action

当我关闭ISE

,exitingtest.log创建,并已 “现在退出...”, 但不创建psISElastOpenedFiles.txt。 ISE似乎在执行退出事件之前关闭所有打开的文件。

我应该使用Timer事件吗?

回答

1

几个月前我尝试过这样做,发现竞赛条件阻止95%的时间工作。在处理powershell.exiting事件之前,ISE对象模型中的选项卡集合通常会被丢弃。哑,是的。可修复,没有。

-Oisin

2

而不是保存退出,保存MRU信息时CurrentTabs和文件对象提高CollectionChanged事件。这是我正在使用的MRU ISE插件:

# Add to profile 
if (test-path $env:TMP\ise_mru.txt) 
{ 
    $global:recentFiles = gc $env:TMP\ise_mru.txt | ?{$_} 
} 

else 
{ 
    $global:recentFiles = @() 
} 

function Update-MRU($newfile) 
{ 
    $global:recentFiles = @($newfile) + ($global:recentFiles -ne $newfile) | Select-Object -First 10 

    $psISE.PowerShellTabs | %{ 
     $pstab = $_ 
     @($pstab.AddOnsMenu.Submenus) | ?{$_.DisplayName -eq 'MRU'} | %{$pstab.AddOnsMenu.Submenus.Remove($_)} 
     $menu = $pstab.AddOnsMenu.Submenus.Add("MRU", $null, $null) 
     $global:recentFiles | ?{$_} | %{ 
      $null = $menu.Submenus.Add($_, [ScriptBlock]::Create("psEdit '$_'"), $null) 
     } 
    } 
    $global:recentFiles | Out-File $env:TMP\ise_mru.txt 
} 

$null = Register-ObjectEvent -InputObject $psISE.PowerShellTabs -EventName CollectionChanged -Action { 
    if ($eventArgs.Action -ne 'Add') 
    { 
     return 
    } 

    Register-ObjectEvent -InputObject $eventArgs.NewItems[0].Files -EventName CollectionChanged -Action { 
     if ($eventArgs.Action -ne 'Add') 
     { 
      return 
     } 
     Update-MRU ($eventArgs.NewItems | ?{-not $_.IsUntitled}| %{$_.FullPath}) 
    } 
} 

$null = Register-ObjectEvent -InputObject $psISE.CurrentPowerShellTab.Files -EventName CollectionChanged -Action { 
    if ($eventArgs.Action -ne 'Add') 
    { 
     return 
    } 
    Update-MRU ($eventArgs.NewItems | ?{-not $_.IsUntitled}| %{$_.FullPath}) 

} 

Update-MRU